'use strict'; const _ = require('lodash'); const { createTestBuilder } = require('../../../../test/helpers/builder'); const { createStrapiInstance } = require('../../../../test/helpers/strapi'); const { createAuthRequest } = require('../../../../test/helpers/request'); const builder = createTestBuilder(); let strapi; let rq; let data = { productsWithCompo: [], }; const compo = { name: 'compo', attributes: { name: { type: 'string', required: true, }, description: { type: 'text', minLength: 4, maxLength: 30, }, }, }; const productWithCompo = { attributes: { name: { type: 'string', }, description: { type: 'text', }, compo: { component: 'default.compo', type: 'component', required: true, }, }, connection: 'default', name: 'product with compo', description: '', collectionName: '', }; describe('CM API - Basic + compo', () => { beforeAll(async () => { await builder .addComponent(compo) .addContentType(productWithCompo) .build(); strapi = await createStrapiInstance(); rq = await createAuthRequest({ strapi }); }, 60000); afterAll(async () => { await strapi.destroy(); await builder.cleanup(); }, 60000); test('Create product with compo', async () => { const product = { name: 'Product 1', description: 'Product description', compo: { name: 'compo name', description: 'short', }, }; const res = await rq({ method: 'POST', url: '/content-manager/collection-types/application::product-with-compo.product-with-compo', body: product, }); expect(res.statusCode).toBe(200); expect(res.body).toMatchObject(product); expect(res.body.published_at).toBeUndefined(); data.productsWithCompo.push(res.body); }); test('Read product with compo', async () => { const res = await rq({ method: 'GET', url: '/content-manager/collection-types/application::product-with-compo.product-with-compo', }); expect(res.statusCode).toBe(200); expect(Array.isArray(res.body.results)).toBe(true); expect(res.body.results).toHaveLength(1); expect(res.body.results[0]).toMatchObject(data.productsWithCompo[0]); res.body.results.forEach(p => expect(p.published_at).toBeUndefined()); }); test('Update product with compo', async () => { const product = { name: 'Product 1 updated', description: 'Updated Product description', compo: { name: 'compo name updated', description: 'update', }, }; const res = await rq({ method: 'PUT', url: `/content-manager/collection-types/application::product-with-compo.product-with-compo/${data.productsWithCompo[0].id}`, body: product, }); expect(res.statusCode).toBe(200); expect(res.body).toMatchObject(product); expect(res.body.id).toEqual(data.productsWithCompo[0].id); expect(res.body.published_at).toBeUndefined(); data.productsWithCompo[0] = res.body; }); test('Delete product with compo', async () => { const res = await rq({ method: 'DELETE', url: `/content-manager/collection-types/application::product-with-compo.product-with-compo/${data.productsWithCompo[0].id}`, }); expect(res.statusCode).toBe(200); expect(res.body).toMatchObject(data.productsWithCompo[0]); expect(res.body.id).toEqual(data.productsWithCompo[0].id); expect(res.body.published_at).toBeUndefined(); data.productsWithCompo.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', url: '/content-manager/collection-types/application::product-with-compo.product-with-compo', body: product, }); expect(res.statusCode).toBe(400); expect(_.get(res, 'body.data.errors.compo.0')).toBe('compo must be defined.'); }); test('Cannot create product with compo - minLength', async () => { const product = { name: 'Product 1', description: 'Product description', compo: { name: 'compo name', description: '', }, }; const res = await rq({ method: 'POST', url: '/content-manager/collection-types/application::product-with-compo.product-with-compo', body: product, }); expect(res.statusCode).toBe(400); expect(_.get(res.body.data, ['errors', 'compo.description', '0'])).toBe( 'compo.description must be at least 4 characters' ); }); test('Cannot create product with compo - maxLength', async () => { const product = { name: 'Product 1', description: 'Product description', compo: { name: 'compo name', description: 'A very long description that exceed the min length.', }, }; const res = await rq({ method: 'POST', url: '/content-manager/collection-types/application::product-with-compo.product-with-compo', body: product, }); expect(res.statusCode).toBe(400); expect(_.get(res.body.data, ['errors', 'compo.description', '0'])).toBe( 'compo.description must be at most 30 characters' ); }); test('Cannot create product with compo - required', async () => { const product = { name: 'Product 1', description: 'Product description', compo: { description: 'short', }, }; const res = await rq({ method: 'POST', url: '/content-manager/collection-types/application::product-with-compo.product-with-compo', body: product, }); expect(res.statusCode).toBe(400); expect(_.get(res.body.data, ['errors', 'compo.name', '0'])).toBe( 'compo.name must be defined.' ); }); }); });