| 
									
										
										
										
											2022-07-26 15:38:19 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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'); | 
					
						
							| 
									
										
										
										
											2022-07-26 15:38:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | const builder = createTestBuilder(); | 
					
						
							|  |  |  | let strapi; | 
					
						
							|  |  |  | let rq; | 
					
						
							| 
									
										
										
										
											2022-08-08 15:50:34 +02:00
										 |  |  | const data = { | 
					
						
							| 
									
										
										
										
											2022-07-26 15:38:19 +02:00
										 |  |  |   productsWithDz: [], | 
					
						
							|  |  |  |   categories: [], | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const compo1 = { | 
					
						
							|  |  |  |   displayName: 'compo1', | 
					
						
							|  |  |  |   attributes: { | 
					
						
							|  |  |  |     title: { | 
					
						
							|  |  |  |       type: 'string', | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     category: { | 
					
						
							|  |  |  |       type: 'relation', | 
					
						
							|  |  |  |       relation: 'oneToOne', | 
					
						
							|  |  |  |       target: 'api::category.category', | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const compo2 = { | 
					
						
							|  |  |  |   displayName: 'compo2', | 
					
						
							|  |  |  |   attributes: { | 
					
						
							|  |  |  |     name: { | 
					
						
							|  |  |  |       type: 'string', | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2022-08-09 12:26:54 +02:00
										 |  |  |     category: { | 
					
						
							|  |  |  |       // same field name as in compo1 but different type
 | 
					
						
							|  |  |  |       type: 'string', | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2022-07-26 15:38:19 +02:00
										 |  |  |     category_diff: { | 
					
						
							|  |  |  |       type: 'relation', | 
					
						
							|  |  |  |       relation: 'oneToOne', | 
					
						
							|  |  |  |       target: 'api::category.category', | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const compoA = { | 
					
						
							|  |  |  |   displayName: 'compoA', | 
					
						
							|  |  |  |   attributes: { | 
					
						
							|  |  |  |     items: { | 
					
						
							|  |  |  |       type: 'component', | 
					
						
							|  |  |  |       repeatable: false, | 
					
						
							|  |  |  |       component: 'default.compo1', | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const compoB = { | 
					
						
							|  |  |  |   displayName: 'compoB', | 
					
						
							|  |  |  |   attributes: { | 
					
						
							|  |  |  |     items: { | 
					
						
							|  |  |  |       type: 'component', | 
					
						
							|  |  |  |       repeatable: false, | 
					
						
							|  |  |  |       component: 'default.compo2', | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const category = { | 
					
						
							|  |  |  |   attributes: { | 
					
						
							|  |  |  |     name: { | 
					
						
							|  |  |  |       type: 'string', | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   displayName: 'Category', | 
					
						
							|  |  |  |   singularName: 'category', | 
					
						
							|  |  |  |   pluralName: 'categories', | 
					
						
							|  |  |  |   description: '', | 
					
						
							|  |  |  |   collectionName: '', | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const productWithDz = { | 
					
						
							|  |  |  |   attributes: { | 
					
						
							|  |  |  |     name: { | 
					
						
							|  |  |  |       type: 'string', | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     dz: { | 
					
						
							|  |  |  |       components: ['default.compo-a', 'default.compo-b'], | 
					
						
							|  |  |  |       type: 'dynamiczone', | 
					
						
							|  |  |  |       required: true, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   displayName: 'Product with dz', | 
					
						
							|  |  |  |   singularName: 'product-with-dz', | 
					
						
							|  |  |  |   pluralName: 'product-with-dzs', | 
					
						
							|  |  |  |   description: '', | 
					
						
							|  |  |  |   collectionName: '', | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe('CM API - Populate dz', () => { | 
					
						
							|  |  |  |   beforeAll(async () => { | 
					
						
							|  |  |  |     await builder | 
					
						
							|  |  |  |       .addContentType(category) | 
					
						
							|  |  |  |       .addComponent(compo1) | 
					
						
							|  |  |  |       .addComponent(compo2) | 
					
						
							|  |  |  |       .addComponent(compoA) | 
					
						
							|  |  |  |       .addComponent(compoB) | 
					
						
							|  |  |  |       .addContentType(productWithDz) | 
					
						
							|  |  |  |       .build(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     strapi = await createStrapiInstance(); | 
					
						
							|  |  |  |     rq = await createAuthRequest({ strapi }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   afterAll(async () => { | 
					
						
							|  |  |  |     await strapi.destroy(); | 
					
						
							|  |  |  |     await builder.cleanup(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   test('Populate works in dz even with same names in different components', async () => { | 
					
						
							|  |  |  |     const categoryRes = await rq({ | 
					
						
							|  |  |  |       method: 'POST', | 
					
						
							|  |  |  |       url: '/content-manager/collection-types/api::category.category', | 
					
						
							|  |  |  |       body: { name: 'name' }, | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(categoryRes.status).toBe(200); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     data.categories.push(categoryRes.body); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const productRes = await rq({ | 
					
						
							|  |  |  |       method: 'POST', | 
					
						
							|  |  |  |       url: '/content-manager/collection-types/api::product-with-dz.product-with-dz', | 
					
						
							|  |  |  |       body: { | 
					
						
							|  |  |  |         name: 'name', | 
					
						
							|  |  |  |         dz: [ | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             __component: 'default.compo-a', | 
					
						
							|  |  |  |             items: { id: 2, title: 'AAAA', category: data.categories[0].id }, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             __component: 'default.compo-b', | 
					
						
							| 
									
										
										
										
											2022-08-09 12:26:54 +02:00
										 |  |  |             items: { id: 2, name: 'BBBB', category_diff: data.categories[0].id, category: 'smthg' }, | 
					
						
							| 
									
										
										
										
											2022-07-26 15:38:19 +02:00
										 |  |  |           }, | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(productRes.status).toBe(200); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     data.productsWithDz.push(productRes.body); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(productRes.body).toMatchObject({ | 
					
						
							|  |  |  |       name: 'name', | 
					
						
							|  |  |  |       dz: [ | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           __component: 'default.compo-a', | 
					
						
							|  |  |  |           items: { | 
					
						
							|  |  |  |             title: 'AAAA', | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           __component: 'default.compo-b', | 
					
						
							|  |  |  |           items: { | 
					
						
							|  |  |  |             name: 'BBBB', | 
					
						
							| 
									
										
										
										
											2022-08-09 12:26:54 +02:00
										 |  |  |             category: 'smthg', | 
					
						
							| 
									
										
										
										
											2022-07-26 15:38:19 +02:00
										 |  |  |           }, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       ], | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); |