| 
									
										
										
										
											2021-03-29 17:49:31 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const { pick } = require('lodash/fp'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-05 10:32:20 +02:00
										 |  |  | const { createTestBuilder } = require('api-tests/builder'); | 
					
						
							|  |  |  | const { createStrapiInstance } = require('api-tests/strapi'); | 
					
						
							|  |  |  | const { createAuthRequest } = require('api-tests/request'); | 
					
						
							| 
									
										
										
										
											2021-03-29 17:49:31 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | let strapi; | 
					
						
							|  |  |  | let rq; | 
					
						
							| 
									
										
										
										
											2022-08-08 15:50:34 +02:00
										 |  |  | const data = { | 
					
						
							| 
									
										
										
										
											2021-03-29 17:49:31 +02:00
										 |  |  |   products: [], | 
					
						
							|  |  |  |   shops: [], | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const productModel = { | 
					
						
							|  |  |  |   pluginOptions: { | 
					
						
							|  |  |  |     i18n: { | 
					
						
							|  |  |  |       localized: true, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   attributes: { | 
					
						
							|  |  |  |     name: { | 
					
						
							|  |  |  |       type: 'string', | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2021-09-13 16:57:04 +02:00
										 |  |  |   displayName: 'Product', | 
					
						
							|  |  |  |   singularName: 'product', | 
					
						
							|  |  |  |   pluralName: 'products', | 
					
						
							| 
									
										
										
										
											2021-03-29 17:49:31 +02:00
										 |  |  |   description: '', | 
					
						
							|  |  |  |   collectionName: '', | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const shopModel = { | 
					
						
							|  |  |  |   pluginOptions: { | 
					
						
							|  |  |  |     i18n: { | 
					
						
							|  |  |  |       localized: true, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   attributes: { | 
					
						
							|  |  |  |     name: { | 
					
						
							|  |  |  |       type: 'string', | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     products: { | 
					
						
							| 
									
										
										
										
											2021-07-08 18:15:32 +02:00
										 |  |  |       type: 'relation', | 
					
						
							|  |  |  |       relation: 'manyToMany', | 
					
						
							| 
									
										
										
										
											2021-08-06 18:09:49 +02:00
										 |  |  |       target: 'api::product.product', | 
					
						
							| 
									
										
										
										
											2021-03-29 17:49:31 +02:00
										 |  |  |       targetAttribute: 'shops', | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2021-09-13 16:57:04 +02:00
										 |  |  |   displayName: 'Shop', | 
					
						
							|  |  |  |   singularName: 'shop', | 
					
						
							|  |  |  |   pluralName: 'shops', | 
					
						
							| 
									
										
										
										
											2021-03-29 17:49:31 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const shops = [ | 
					
						
							| 
									
										
										
										
											2022-10-04 10:54:18 +02:00
										 |  |  |   { | 
					
						
							|  |  |  |     name: 'mercato', | 
					
						
							|  |  |  |     locale: 'it', | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2021-03-29 17:49:31 +02:00
										 |  |  |   { | 
					
						
							|  |  |  |     name: 'market', | 
					
						
							|  |  |  |     locale: 'en', | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-04 10:54:18 +02:00
										 |  |  | const products = ({ shop: shops }) => { | 
					
						
							| 
									
										
										
										
											2021-03-29 17:49:31 +02:00
										 |  |  |   const entries = [ | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       name: 'pomodoro', | 
					
						
							| 
									
										
										
										
											2022-10-04 10:54:18 +02:00
										 |  |  |       shops: [shops[0].id], | 
					
						
							| 
									
										
										
										
											2021-03-29 17:49:31 +02:00
										 |  |  |       locale: 'it', | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       name: 'apple', | 
					
						
							| 
									
										
										
										
											2022-10-04 10:54:18 +02:00
										 |  |  |       shops: [shops[1].id], | 
					
						
							| 
									
										
										
										
											2021-03-29 17:49:31 +02:00
										 |  |  |       locale: 'en', | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return entries; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-01 17:24:34 +02:00
										 |  |  | describe('i18n - Find existing relations', () => { | 
					
						
							| 
									
										
										
										
											2021-03-29 17:49:31 +02:00
										 |  |  |   const builder = createTestBuilder(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   beforeAll(async () => { | 
					
						
							|  |  |  |     await builder | 
					
						
							|  |  |  |       .addContentTypes([productModel, shopModel]) | 
					
						
							| 
									
										
										
										
											2021-08-09 11:06:34 +02:00
										 |  |  |       .addFixtures('plugin::i18n.locale', [ | 
					
						
							| 
									
										
										
										
											2021-08-06 18:46:30 +02:00
										 |  |  |         { | 
					
						
							|  |  |  |           name: 'It', | 
					
						
							|  |  |  |           code: 'it', | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       ]) | 
					
						
							| 
									
										
										
										
											2021-09-13 16:57:04 +02:00
										 |  |  |       .addFixtures(shopModel.singularName, shops) | 
					
						
							|  |  |  |       .addFixtures(productModel.singularName, products) | 
					
						
							| 
									
										
										
										
											2021-03-29 17:49:31 +02:00
										 |  |  |       .build(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     strapi = await createStrapiInstance(); | 
					
						
							|  |  |  |     rq = await createAuthRequest({ strapi }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-04 15:47:53 +01:00
										 |  |  |     data.shops = await builder.sanitizedFixturesFor(shopModel.singularName, strapi); | 
					
						
							|  |  |  |     data.products = await builder.sanitizedFixturesFor(productModel.singularName, strapi); | 
					
						
							| 
									
										
										
										
											2021-03-29 17:49:31 +02:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   afterAll(async () => { | 
					
						
							|  |  |  |     await strapi.destroy(); | 
					
						
							|  |  |  |     await builder.cleanup(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-04 10:54:18 +02:00
										 |  |  |   test('Get Italian product for italian shop filter on any locale', async () => { | 
					
						
							| 
									
										
										
										
											2021-03-29 17:49:31 +02:00
										 |  |  |     const res = await rq({ | 
					
						
							| 
									
										
										
										
											2022-08-10 19:14:04 +02:00
										 |  |  |       method: 'GET', | 
					
						
							| 
									
										
										
										
											2022-10-04 16:25:36 +02:00
										 |  |  |       url: `/content-manager/relations/api::shop.shop/${data.shops[0].id}/products`, | 
					
						
							| 
									
										
										
										
											2021-03-29 17:49:31 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-10 19:14:04 +02:00
										 |  |  |     expect(res.body.results).toHaveLength(1); | 
					
						
							| 
									
										
										
										
											2022-10-04 10:54:18 +02:00
										 |  |  |     expect(res.body.results[0]).toStrictEqual(pick(['id', 'name'], data.products[0])); | 
					
						
							| 
									
										
										
										
											2021-03-29 17:49:31 +02:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-04 10:54:18 +02:00
										 |  |  |   test('Get english product for english shop', async () => { | 
					
						
							| 
									
										
										
										
											2021-03-29 17:49:31 +02:00
										 |  |  |     const res = await rq({ | 
					
						
							| 
									
										
										
										
											2022-08-10 19:14:04 +02:00
										 |  |  |       method: 'GET', | 
					
						
							| 
									
										
										
										
											2022-10-04 16:25:36 +02:00
										 |  |  |       url: `/content-manager/relations/api::shop.shop/${data.shops[1].id}/products`, | 
					
						
							| 
									
										
										
										
											2021-03-29 17:49:31 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-10 19:14:04 +02:00
										 |  |  |     expect(res.body.results).toHaveLength(1); | 
					
						
							| 
									
										
										
										
											2022-10-04 10:54:18 +02:00
										 |  |  |     expect(res.body.results[0]).toStrictEqual(pick(['id', 'name'], data.products[1])); | 
					
						
							| 
									
										
										
										
											2021-03-29 17:49:31 +02:00
										 |  |  |   }); | 
					
						
							|  |  |  | }); |