| 
									
										
										
										
											2020-12-02 15:20:12 +01:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const { prop, difference, map, uniq } = require('lodash/fp'); | 
					
						
							| 
									
										
										
										
											2023-04-05 10:32:20 +02:00
										 |  |  | const { createAuthRequest } = require('api-tests/request'); | 
					
						
							|  |  |  | const { createStrapiInstance } = require('api-tests/strapi'); | 
					
						
							|  |  |  | const { createTestBuilder } = require('api-tests/builder'); | 
					
						
							| 
									
										
										
										
											2020-12-02 15:20:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  | const toIds = (arr) => uniq(map(prop('id'))(arr)); | 
					
						
							| 
									
										
										
										
											2020-12-04 16:07:18 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-29 16:24:57 +01:00
										 |  |  | let strapi; | 
					
						
							| 
									
										
										
										
											2020-12-02 15:20:12 +01:00
										 |  |  | let rq; | 
					
						
							| 
									
										
										
										
											2020-12-29 16:24:57 +01:00
										 |  |  | const builder = createTestBuilder(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-02 15:20:12 +01:00
										 |  |  | const data = { | 
					
						
							|  |  |  |   product: [], | 
					
						
							|  |  |  |   category: [], | 
					
						
							|  |  |  |   shop: [], | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const productModel = { | 
					
						
							|  |  |  |   attributes: { | 
					
						
							|  |  |  |     name: { | 
					
						
							|  |  |  |       type: 'string', | 
					
						
							|  |  |  |       unique: true, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     categories: { | 
					
						
							| 
									
										
										
										
											2021-07-08 18:15:32 +02:00
										 |  |  |       type: 'relation', | 
					
						
							|  |  |  |       relation: 'oneToMany', | 
					
						
							| 
									
										
										
										
											2021-08-06 18:09:49 +02:00
										 |  |  |       target: 'api::category.category', | 
					
						
							| 
									
										
										
										
											2020-12-02 15:20:12 +01:00
										 |  |  |       targetAttribute: 'product', | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     shops: { | 
					
						
							| 
									
										
										
										
											2021-07-08 18:15:32 +02:00
										 |  |  |       type: 'relation', | 
					
						
							|  |  |  |       relation: 'oneToMany', | 
					
						
							| 
									
										
										
										
											2021-08-06 18:09:49 +02:00
										 |  |  |       target: 'api::shop.shop', | 
					
						
							| 
									
										
										
										
											2020-12-02 15:20:12 +01:00
										 |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2021-09-13 16:57:04 +02:00
										 |  |  |   displayName: 'Product', | 
					
						
							|  |  |  |   singularName: 'product', | 
					
						
							|  |  |  |   pluralName: 'products', | 
					
						
							| 
									
										
										
										
											2020-12-02 15:20:12 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const categoryModel = { | 
					
						
							|  |  |  |   attributes: { | 
					
						
							|  |  |  |     name: { | 
					
						
							|  |  |  |       type: 'string', | 
					
						
							|  |  |  |       unique: true, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2021-09-13 16:57:04 +02:00
										 |  |  |   displayName: 'Category', | 
					
						
							|  |  |  |   singularName: 'category', | 
					
						
							|  |  |  |   pluralName: 'categories', | 
					
						
							| 
									
										
										
										
											2020-12-02 15:20:12 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const shopModel = { | 
					
						
							|  |  |  |   attributes: { | 
					
						
							|  |  |  |     name: { | 
					
						
							|  |  |  |       type: 'string', | 
					
						
							|  |  |  |       unique: true, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     metadata: { | 
					
						
							|  |  |  |       type: 'string', | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							| 
									
										
										
										
											2021-09-13 16:57:04 +02:00
										 |  |  |   displayName: 'Shop', | 
					
						
							|  |  |  |   singularName: 'shop', | 
					
						
							|  |  |  |   pluralName: 'shops', | 
					
						
							| 
									
										
										
										
											2020-12-02 15:20:12 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-08 18:15:32 +02:00
										 |  |  | const PRODUCT_SHOP_COUNT = 12; | 
					
						
							|  |  |  | const PRODUCT_CATEGORY_COUNT = 5; | 
					
						
							| 
									
										
										
										
											2020-12-02 15:20:12 +01:00
										 |  |  | const fixtures = { | 
					
						
							|  |  |  |   shop: [ | 
					
						
							|  |  |  |     { name: 'SH.A', metadata: 'foobar' }, | 
					
						
							|  |  |  |     { name: 'SH.B', metadata: 'foobar' }, | 
					
						
							|  |  |  |     { name: 'SH.C', metadata: 'foobar' }, | 
					
						
							|  |  |  |     { name: 'SH.D', metadata: 'foobar' }, | 
					
						
							|  |  |  |     { name: 'SH.E', metadata: 'foobar' }, | 
					
						
							|  |  |  |     { name: 'SH.F', metadata: 'foobar' }, | 
					
						
							|  |  |  |     { name: 'SH.G', metadata: 'foobar' }, | 
					
						
							|  |  |  |     { name: 'SH.H', metadata: 'foobar' }, | 
					
						
							|  |  |  |     { name: 'SH.I', metadata: 'foobar' }, | 
					
						
							|  |  |  |     { name: 'SH.J', metadata: 'foobar' }, | 
					
						
							|  |  |  |     { name: 'SH.K', metadata: 'foobar' }, | 
					
						
							|  |  |  |     { name: 'SH.L', metadata: 'foobar' }, | 
					
						
							|  |  |  |   ], | 
					
						
							|  |  |  |   category: [ | 
					
						
							|  |  |  |     { name: 'CT.A' }, | 
					
						
							|  |  |  |     { name: 'CT.B' }, | 
					
						
							|  |  |  |     { name: 'CT.C' }, | 
					
						
							|  |  |  |     { name: 'CT.D' }, | 
					
						
							|  |  |  |     { name: 'CT.E' }, | 
					
						
							|  |  |  |     { name: 'CT.F' }, | 
					
						
							|  |  |  |     { name: 'CT.G' }, | 
					
						
							|  |  |  |     { name: 'CT.H' }, | 
					
						
							|  |  |  |     { name: 'CT.I' }, | 
					
						
							|  |  |  |     { name: 'CT.J' }, | 
					
						
							|  |  |  |     { name: 'CT.K' }, | 
					
						
							|  |  |  |     { name: 'CT.L' }, | 
					
						
							|  |  |  |   ], | 
					
						
							| 
									
										
										
										
											2020-12-29 16:24:57 +01:00
										 |  |  |   product: ({ shop, category }) => [ | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       name: 'PD.A', | 
					
						
							| 
									
										
										
										
											2021-07-08 18:15:32 +02:00
										 |  |  |       categories: category.slice(0, PRODUCT_CATEGORY_COUNT).map(prop('id')), | 
					
						
							|  |  |  |       shops: shop.slice(0, PRODUCT_SHOP_COUNT).map(prop('id')), | 
					
						
							| 
									
										
										
										
											2020-12-29 16:24:57 +01:00
										 |  |  |     }, | 
					
						
							|  |  |  |   ], | 
					
						
							| 
									
										
										
										
											2020-12-02 15:20:12 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  | const getUID = (modelName) => `api::${modelName}.${modelName}`; | 
					
						
							| 
									
										
										
										
											2022-10-04 16:25:36 +02:00
										 |  |  | const getCMPrefixUrl = (modelName) => `/content-manager/relations/${getUID(modelName)}`; | 
					
						
							| 
									
										
										
										
											2020-12-02 15:20:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | describe('x-to-many RF Preview', () => { | 
					
						
							| 
									
										
										
										
											2021-09-13 16:57:04 +02:00
										 |  |  |   const cmProductUrl = getCMPrefixUrl(productModel.singularName); | 
					
						
							| 
									
										
										
										
											2020-12-02 15:20:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   beforeAll(async () => { | 
					
						
							| 
									
										
										
										
											2020-12-29 16:24:57 +01:00
										 |  |  |     await builder | 
					
						
							|  |  |  |       .addContentTypes([shopModel, categoryModel, productModel]) | 
					
						
							| 
									
										
										
										
											2021-09-13 16:57:04 +02:00
										 |  |  |       .addFixtures(shopModel.singularName, fixtures.shop) | 
					
						
							|  |  |  |       .addFixtures(categoryModel.singularName, fixtures.category) | 
					
						
							|  |  |  |       .addFixtures(productModel.singularName, fixtures.product) | 
					
						
							| 
									
										
										
										
											2020-12-29 16:24:57 +01:00
										 |  |  |       .build(); | 
					
						
							| 
									
										
										
										
											2020-12-02 15:20:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-29 16:24:57 +01:00
										 |  |  |     strapi = await createStrapiInstance(); | 
					
						
							|  |  |  |     rq = await createAuthRequest({ strapi }); | 
					
						
							| 
									
										
										
										
											2020-12-02 15:20:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-04 15:47:53 +01:00
										 |  |  |     Object.assign(data, await builder.sanitizedFixtures(strapi)); | 
					
						
							| 
									
										
										
										
											2021-03-26 20:15:38 +01:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-12-02 15:20:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   afterAll(async () => { | 
					
						
							| 
									
										
										
										
											2020-12-29 16:24:57 +01:00
										 |  |  |     await strapi.destroy(); | 
					
						
							|  |  |  |     await builder.cleanup(); | 
					
						
							| 
									
										
										
										
											2021-03-26 20:15:38 +01:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-12-02 15:20:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe('Entity Misc', () => { | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  |     test.each(['foobar', 'name'])(`Throws if the targeted field is invalid (%s)`, async (field) => { | 
					
						
							| 
									
										
										
										
											2020-12-02 15:20:12 +01:00
										 |  |  |       const product = data.product[0]; | 
					
						
							|  |  |  |       const { body, statusCode } = await rq.get(`${cmProductUrl}/${product.id}/${field}`); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(statusCode).toBe(400); | 
					
						
							| 
									
										
										
										
											2021-10-20 17:30:05 +02:00
										 |  |  |       expect(body).toMatchObject({ | 
					
						
							|  |  |  |         data: null, | 
					
						
							|  |  |  |         error: { | 
					
						
							|  |  |  |           status: 400, | 
					
						
							|  |  |  |           name: 'BadRequestError', | 
					
						
							| 
									
										
										
										
											2022-09-01 11:05:18 +02:00
										 |  |  |           message: "This relational field doesn't exist", | 
					
						
							| 
									
										
										
										
											2021-10-20 17:30:05 +02:00
										 |  |  |           details: {}, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2020-12-02 15:20:12 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     test('Throws if the entity does not exist', async () => { | 
					
						
							| 
									
										
										
										
											2020-12-29 17:44:14 +01:00
										 |  |  |       const { body, statusCode } = await rq.get(`${cmProductUrl}/${data.shop[11].id}/categories`); | 
					
						
							| 
									
										
										
										
											2020-12-02 15:20:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       expect(statusCode).toBe(404); | 
					
						
							| 
									
										
										
										
											2021-10-20 17:30:05 +02:00
										 |  |  |       expect(body).toMatchObject({ | 
					
						
							|  |  |  |         data: null, | 
					
						
							|  |  |  |         error: { | 
					
						
							|  |  |  |           status: 404, | 
					
						
							|  |  |  |           name: 'NotFoundError', | 
					
						
							|  |  |  |           message: 'Not Found', | 
					
						
							|  |  |  |           details: {}, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2020-12-02 15:20:12 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('Default Behavior', () => { | 
					
						
							| 
									
										
										
										
											2021-07-08 18:15:32 +02:00
										 |  |  |     test('Should return a preview for the shops field', async () => { | 
					
						
							| 
									
										
										
										
											2020-12-02 15:20:12 +01:00
										 |  |  |       const product = data.product[0]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-08 18:15:32 +02:00
										 |  |  |       const { body, statusCode } = await rq.get(`${cmProductUrl}/${product.id}/shops`); | 
					
						
							| 
									
										
										
										
											2020-12-02 15:20:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-08 18:15:32 +02:00
										 |  |  |       expect(statusCode).toBe(200); | 
					
						
							|  |  |  |       expect(body.results).toHaveLength(10); | 
					
						
							|  |  |  |       expect(difference(toIds(body.results), toIds(data.shop))).toHaveLength(0); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     test('Should return a preview for the categories field', async () => { | 
					
						
							|  |  |  |       const product = data.product[0]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const { body, statusCode } = await rq.get(`${cmProductUrl}/${product.id}/categories`); | 
					
						
							| 
									
										
										
										
											2020-12-02 15:20:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       expect(statusCode).toBe(200); | 
					
						
							| 
									
										
										
										
											2021-07-08 18:15:32 +02:00
										 |  |  |       expect(body.results).toHaveLength(5); | 
					
						
							|  |  |  |       expect(difference(toIds(body.results), toIds(data.category))).toHaveLength(0); | 
					
						
							| 
									
										
										
										
											2020-12-02 15:20:12 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('Pagination', () => { | 
					
						
							| 
									
										
										
										
											2022-03-29 14:20:03 +02:00
										 |  |  |     test.each([ | 
					
						
							|  |  |  |       [1, 10], | 
					
						
							|  |  |  |       [2, 10], | 
					
						
							|  |  |  |       [5, 1], | 
					
						
							|  |  |  |       [4, 2], | 
					
						
							|  |  |  |       [1, 100], | 
					
						
							|  |  |  |     ])('Custom pagination (%s, %s)', async (page, pageSize) => { | 
					
						
							|  |  |  |       const product = data.product[0]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const { body, statusCode } = await rq.get( | 
					
						
							|  |  |  |         `${cmProductUrl}/${product.id}/shops?page=${page}&pageSize=${pageSize}` | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(statusCode).toBe(200); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const { pagination, results } = body; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(pagination.page).toBe(page); | 
					
						
							|  |  |  |       expect(pagination.pageSize).toBe(pageSize); | 
					
						
							|  |  |  |       expect(results).toHaveLength(Math.min(pageSize, PRODUCT_SHOP_COUNT - pageSize * (page - 1))); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-12-02 15:20:12 +01:00
										 |  |  |   }); | 
					
						
							|  |  |  | }); |