| 
									
										
										
										
											2020-10-27 11:27:17 +01:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  | const _ = require('lodash'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-29 11:11:46 +02:00
										 |  |  | const { createTestBuilder } = require('../../../../../test/helpers/builder'); | 
					
						
							|  |  |  | const { createStrapiInstance } = require('../../../../../test/helpers/strapi'); | 
					
						
							|  |  |  | const { createAuthRequest } = require('../../../../../test/helpers/request'); | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  | const builder = createTestBuilder(); | 
					
						
							|  |  |  | let strapi; | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  | let rq; | 
					
						
							|  |  |  | let data = { | 
					
						
							|  |  |  |   productsWithDz: [], | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const compo = { | 
					
						
							|  |  |  |   name: 'compo', | 
					
						
							|  |  |  |   attributes: { | 
					
						
							|  |  |  |     name: { | 
					
						
							|  |  |  |       type: 'string', | 
					
						
							|  |  |  |       required: true, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     description: { | 
					
						
							|  |  |  |       type: 'text', | 
					
						
							|  |  |  |       minLength: 3, | 
					
						
							|  |  |  |       maxLength: 10, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const productWithDz = { | 
					
						
							|  |  |  |   attributes: { | 
					
						
							|  |  |  |     name: { | 
					
						
							|  |  |  |       type: 'string', | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     description: { | 
					
						
							|  |  |  |       type: 'text', | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     dz: { | 
					
						
							|  |  |  |       components: ['default.compo'], | 
					
						
							|  |  |  |       type: 'dynamiczone', | 
					
						
							|  |  |  |       required: true, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   name: 'product with dz', | 
					
						
							|  |  |  |   description: '', | 
					
						
							|  |  |  |   collectionName: '', | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe('Core API - Basic + dz', () => { | 
					
						
							|  |  |  |   beforeAll(async () => { | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |     await builder | 
					
						
							|  |  |  |       .addComponent(compo) | 
					
						
							|  |  |  |       .addContentType(productWithDz) | 
					
						
							|  |  |  |       .build(); | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-30 20:20:36 +01:00
										 |  |  |     strapi = await createStrapiInstance(); | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |     rq = await createAuthRequest({ strapi }); | 
					
						
							| 
									
										
										
										
											2021-03-26 20:15:38 +01:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   afterAll(async () => { | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |     await strapi.destroy(); | 
					
						
							|  |  |  |     await builder.cleanup(); | 
					
						
							| 
									
										
										
										
											2021-03-26 20:15:38 +01:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   test('Create product with compo', async () => { | 
					
						
							|  |  |  |     const product = { | 
					
						
							|  |  |  |       name: 'Product 1', | 
					
						
							|  |  |  |       description: 'Product description', | 
					
						
							|  |  |  |       dz: [ | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           __component: 'default.compo', | 
					
						
							|  |  |  |           name: 'compo name', | 
					
						
							|  |  |  |           description: 'short', | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       ], | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     const res = await rq({ | 
					
						
							|  |  |  |       method: 'POST', | 
					
						
							| 
									
										
										
										
											2021-08-06 18:09:49 +02:00
										 |  |  |       url: '/content-manager/collection-types/api::product-with-dz.product-with-dz', | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  |       body: product, | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(res.statusCode).toBe(200); | 
					
						
							|  |  |  |     expect(res.body).toMatchObject(product); | 
					
						
							|  |  |  |     expect(res.body.published_at).toBeUndefined(); | 
					
						
							|  |  |  |     data.productsWithDz.push(res.body); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   test('Read product with compo', async () => { | 
					
						
							|  |  |  |     const res = await rq({ | 
					
						
							|  |  |  |       method: 'GET', | 
					
						
							| 
									
										
										
										
											2021-08-06 18:09:49 +02:00
										 |  |  |       url: `/content-manager/collection-types/api::product-with-dz.product-with-dz/${data.productsWithDz[0].id}`, | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(res.statusCode).toBe(200); | 
					
						
							| 
									
										
										
										
											2021-07-30 21:15:58 +02:00
										 |  |  |     expect(res.body).toMatchObject(data.productsWithDz[0]); | 
					
						
							|  |  |  |     expect(res.body.published_at).toBeUndefined(); | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   test('Update product with compo', async () => { | 
					
						
							|  |  |  |     const product = { | 
					
						
							|  |  |  |       name: 'Product 1 updated', | 
					
						
							|  |  |  |       description: 'Updated Product description', | 
					
						
							|  |  |  |       dz: [ | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           __component: 'default.compo', | 
					
						
							|  |  |  |           name: 'compo name updated', | 
					
						
							|  |  |  |           description: 'update', | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       ], | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     const res = await rq({ | 
					
						
							|  |  |  |       method: 'PUT', | 
					
						
							| 
									
										
										
										
											2021-08-06 18:09:49 +02:00
										 |  |  |       url: `/content-manager/collection-types/api::product-with-dz.product-with-dz/${data.productsWithDz[0].id}`, | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  |       body: product, | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(res.statusCode).toBe(200); | 
					
						
							|  |  |  |     expect(res.body).toMatchObject(product); | 
					
						
							|  |  |  |     expect(res.body.id).toEqual(data.productsWithDz[0].id); | 
					
						
							|  |  |  |     expect(res.body.published_at).toBeUndefined(); | 
					
						
							|  |  |  |     data.productsWithDz[0] = res.body; | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   test('Delete product with compo', async () => { | 
					
						
							|  |  |  |     const res = await rq({ | 
					
						
							|  |  |  |       method: 'DELETE', | 
					
						
							| 
									
										
										
										
											2021-08-06 18:09:49 +02:00
										 |  |  |       url: `/content-manager/collection-types/api::product-with-dz.product-with-dz/${data.productsWithDz[0].id}`, | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(res.statusCode).toBe(200); | 
					
						
							|  |  |  |     expect(res.body).toMatchObject(data.productsWithDz[0]); | 
					
						
							|  |  |  |     expect(res.body.id).toEqual(data.productsWithDz[0].id); | 
					
						
							|  |  |  |     expect(res.body.published_at).toBeUndefined(); | 
					
						
							|  |  |  |     data.productsWithDz.shift(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('validation', () => { | 
					
						
							|  |  |  |     test('Cannot create product with compo - compo required', async () => { | 
					
						
							|  |  |  |       const product = { | 
					
						
							|  |  |  |         name: 'Product 1', | 
					
						
							|  |  |  |         description: 'Product description', | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |       const res = await rq({ | 
					
						
							|  |  |  |         method: 'POST', | 
					
						
							| 
									
										
										
										
											2021-08-06 18:09:49 +02:00
										 |  |  |         url: '/content-manager/collection-types/api::product-with-dz.product-with-dz', | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  |         body: product, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(res.statusCode).toBe(400); | 
					
						
							|  |  |  |       expect(_.get(res.body.data, ['errors', 'dz', '0'])).toBe('dz must be defined.'); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     test('Cannot create product with compo - minLength', async () => { | 
					
						
							|  |  |  |       const product = { | 
					
						
							|  |  |  |         name: 'Product 1', | 
					
						
							|  |  |  |         description: 'Product description', | 
					
						
							|  |  |  |         dz: [ | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             __component: 'default.compo', | 
					
						
							|  |  |  |             name: 'compo name', | 
					
						
							|  |  |  |             description: '', | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |       const res = await rq({ | 
					
						
							|  |  |  |         method: 'POST', | 
					
						
							| 
									
										
										
										
											2021-08-06 18:09:49 +02:00
										 |  |  |         url: '/content-manager/collection-types/api::product-with-dz.product-with-dz', | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  |         body: product, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(res.statusCode).toBe(400); | 
					
						
							|  |  |  |       expect(_.get(res.body.data, ['errors', 'dz[0].description', '0'])).toBe( | 
					
						
							|  |  |  |         'dz[0].description must be at least 3 characters' | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     test('Cannot create product with compo - maxLength', async () => { | 
					
						
							|  |  |  |       const product = { | 
					
						
							|  |  |  |         name: 'Product 1', | 
					
						
							|  |  |  |         description: 'Product description', | 
					
						
							|  |  |  |         dz: [ | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             __component: 'default.compo', | 
					
						
							|  |  |  |             name: 'compo name', | 
					
						
							|  |  |  |             description: 'A very long description that exceed the min length.', | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |       const res = await rq({ | 
					
						
							|  |  |  |         method: 'POST', | 
					
						
							| 
									
										
										
										
											2021-08-06 18:09:49 +02:00
										 |  |  |         url: '/content-manager/collection-types/api::product-with-dz.product-with-dz', | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  |         body: product, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(res.statusCode).toBe(400); | 
					
						
							|  |  |  |       expect(_.get(res.body.data, ['errors', 'dz[0].description', '0'])).toBe( | 
					
						
							|  |  |  |         'dz[0].description must be at most 10 characters' | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     test('Cannot create product with compo - required', async () => { | 
					
						
							|  |  |  |       const product = { | 
					
						
							|  |  |  |         name: 'Product 1', | 
					
						
							|  |  |  |         description: 'Product description', | 
					
						
							|  |  |  |         dz: [ | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             __component: 'default.compo', | 
					
						
							|  |  |  |             description: 'short', | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |       const res = await rq({ | 
					
						
							|  |  |  |         method: 'POST', | 
					
						
							| 
									
										
										
										
											2021-08-06 18:09:49 +02:00
										 |  |  |         url: '/content-manager/collection-types/api::product-with-dz.product-with-dz', | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  |         body: product, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(res.statusCode).toBe(400); | 
					
						
							| 
									
										
										
										
											2020-11-02 12:40:35 +01:00
										 |  |  |       expect(_.get(res.body.data, ['errors', 'dz[0].name', '0'])).toBe( | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  |         'dz[0].name must be defined.' | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     test('Cannot create product with compo - missing __component', async () => { | 
					
						
							|  |  |  |       const product = { | 
					
						
							|  |  |  |         name: 'Product 1', | 
					
						
							|  |  |  |         description: 'Product description', | 
					
						
							|  |  |  |         dz: [ | 
					
						
							|  |  |  |           { | 
					
						
							|  |  |  |             name: 'Product 1', | 
					
						
							|  |  |  |             description: 'short', | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         ], | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |       const res = await rq({ | 
					
						
							|  |  |  |         method: 'POST', | 
					
						
							| 
									
										
										
										
											2021-08-06 18:09:49 +02:00
										 |  |  |         url: '/content-manager/collection-types/api::product-with-dz.product-with-dz', | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  |         body: product, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(res.statusCode).toBe(400); | 
					
						
							|  |  |  |       expect(_.get(res.body.data, ['errors', 'dz[0].__component', '0'])).toBe( | 
					
						
							|  |  |  |         'dz[0].__component is a required field' | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); |