2020-10-27 11:27:17 +01: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');
|
2019-12-12 10:15:25 +01:00
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
let strapi;
|
2019-12-12 10:15:25 +01:00
|
|
|
let rq;
|
|
|
|
|
2019-12-12 14:34:18 +01:00
|
|
|
const defaultBody = {
|
|
|
|
field: [
|
|
|
|
{
|
|
|
|
__component: 'default.simple-compo',
|
|
|
|
name: 'someString',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
__component: 'default.compo-with-other-compo',
|
|
|
|
compo: {
|
|
|
|
name: 'someString',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
const models = {
|
|
|
|
ct: {
|
2021-09-13 16:57:04 +02:00
|
|
|
displayName: 'withdynamiczone',
|
|
|
|
singularName: 'withdynamiczone',
|
|
|
|
pluralName: 'withdynamiczones',
|
2020-11-17 15:38:41 +01:00
|
|
|
attributes: {
|
|
|
|
field: {
|
|
|
|
type: 'dynamiczone',
|
|
|
|
components: ['default.compo-with-other-compo', 'default.simple-compo'],
|
|
|
|
required: false,
|
|
|
|
min: 2,
|
|
|
|
max: 5,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
simpleCompo: {
|
2021-11-02 18:27:49 +01:00
|
|
|
displayName: 'simple-compo',
|
2020-11-17 15:38:41 +01:00
|
|
|
attributes: {
|
|
|
|
name: {
|
|
|
|
type: 'string',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
otherCompo: {
|
2021-11-02 18:27:49 +01:00
|
|
|
displayName: 'compo-with-other-compo',
|
2020-11-17 15:38:41 +01:00
|
|
|
attributes: {
|
|
|
|
compo: {
|
|
|
|
type: 'component',
|
|
|
|
component: 'default.simple-compo',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-12-12 14:34:18 +01:00
|
|
|
const createEntry = () => {
|
2020-11-17 15:38:41 +01:00
|
|
|
return rq({
|
|
|
|
method: 'POST',
|
|
|
|
url: '/',
|
2019-12-12 14:34:18 +01:00
|
|
|
body: defaultBody,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const createEmpty = () => {
|
2020-11-17 15:38:41 +01:00
|
|
|
return rq({
|
|
|
|
method: 'POST',
|
|
|
|
url: '/',
|
2019-12-12 14:34:18 +01:00
|
|
|
body: {
|
|
|
|
field: [],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-08-10 18:58:18 +02:00
|
|
|
describe('Not required dynamiczone', () => {
|
2020-11-17 15:38:41 +01:00
|
|
|
const builder = createTestBuilder();
|
2020-11-03 13:42:01 +01:00
|
|
|
|
2019-12-12 10:15:25 +01:00
|
|
|
beforeAll(async () => {
|
2020-11-17 15:38:41 +01:00
|
|
|
await builder
|
|
|
|
.addComponent(models.simpleCompo)
|
|
|
|
.addComponent(models.otherCompo)
|
|
|
|
.addContentType(models.ct)
|
|
|
|
.build();
|
|
|
|
|
2020-11-30 20:20:36 +01:00
|
|
|
strapi = await createStrapiInstance();
|
2020-11-17 15:38:41 +01:00
|
|
|
rq = await createAuthRequest({ strapi });
|
2021-08-13 15:43:04 +02:00
|
|
|
rq.setURLPrefix('/content-manager/collection-types/api::withdynamiczone.withdynamiczone');
|
2021-03-26 20:15:38 +01:00
|
|
|
});
|
2019-12-12 10:15:25 +01: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
|
|
|
});
|
2019-12-12 10:15:25 +01:00
|
|
|
|
|
|
|
describe('Creation', () => {
|
|
|
|
test('Can create an entry with a dynamic zone and a nested compo', async () => {
|
2020-11-17 15:38:41 +01:00
|
|
|
const res = await rq({
|
|
|
|
method: 'POST',
|
|
|
|
url: '/',
|
2019-12-12 10:15:25 +01:00
|
|
|
body: {
|
|
|
|
field: [
|
|
|
|
{
|
|
|
|
__component: 'default.simple-compo',
|
|
|
|
name: 'someString',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
__component: 'default.compo-with-other-compo',
|
|
|
|
compo: {
|
|
|
|
name: 'someString',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2021-07-26 17:52:59 +02:00
|
|
|
qs: {
|
|
|
|
populate: ['field'],
|
|
|
|
},
|
2019-12-12 10:15:25 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(Array.isArray(res.body.field)).toBe(true);
|
|
|
|
expect(res.body).toMatchObject({
|
|
|
|
field: [
|
|
|
|
{
|
|
|
|
id: expect.anything(),
|
|
|
|
__component: 'default.simple-compo',
|
|
|
|
name: 'someString',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: expect.anything(),
|
|
|
|
__component: 'default.compo-with-other-compo',
|
|
|
|
compo: {
|
|
|
|
id: expect.anything(),
|
|
|
|
name: 'someString',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Can create entry with empty dynamiczone if it is not required', async () => {
|
2020-11-17 15:38:41 +01:00
|
|
|
const res = await rq({
|
|
|
|
method: 'POST',
|
|
|
|
url: '/',
|
2019-12-12 10:15:25 +01:00
|
|
|
body: {
|
|
|
|
field: [],
|
|
|
|
},
|
2021-07-26 17:52:59 +02:00
|
|
|
qs: {
|
|
|
|
populate: ['field'],
|
|
|
|
},
|
2019-12-12 10:15:25 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(Array.isArray(res.body.field)).toBe(true);
|
|
|
|
expect(res.body.field.length).toBe(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Throw if min items is not respected', async () => {
|
2020-11-17 15:38:41 +01:00
|
|
|
const res = await rq({
|
|
|
|
method: 'POST',
|
|
|
|
url: '/',
|
2019-12-12 10:15:25 +01:00
|
|
|
body: {
|
|
|
|
field: [
|
|
|
|
{
|
|
|
|
__component: 'default.simple-compo',
|
|
|
|
name: 'someString',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(400);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Throws if max items is not respected', async () => {
|
2020-11-17 15:38:41 +01:00
|
|
|
const res = await rq({
|
|
|
|
method: 'POST',
|
|
|
|
url: '/',
|
2019-12-12 10:15:25 +01:00
|
|
|
body: {
|
|
|
|
field: Array(10).fill({
|
|
|
|
__component: 'default.simple-compo',
|
|
|
|
name: 'someString',
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(400);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Getting one entry', () => {
|
|
|
|
test('The entry has its dynamic zone populated', async () => {
|
2019-12-12 14:58:39 +01:00
|
|
|
const createRes = await createEntry();
|
|
|
|
const entryId = createRes.body.id;
|
2019-12-12 10:15:25 +01:00
|
|
|
|
2021-07-26 17:52:59 +02:00
|
|
|
const res = await rq({
|
|
|
|
method: 'GET',
|
|
|
|
url: `/${entryId}`,
|
|
|
|
qs: {
|
|
|
|
populate: ['field'],
|
|
|
|
},
|
|
|
|
});
|
2019-12-12 10:15:25 +01:00
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(Array.isArray(res.body.field)).toBe(true);
|
|
|
|
expect(res.body).toMatchObject({
|
|
|
|
field: [
|
|
|
|
{
|
|
|
|
id: expect.anything(),
|
|
|
|
__component: 'default.simple-compo',
|
|
|
|
name: 'someString',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: expect.anything(),
|
|
|
|
__component: 'default.compo-with-other-compo',
|
|
|
|
compo: {
|
|
|
|
id: expect.anything(),
|
|
|
|
name: 'someString',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Listing entries', () => {
|
2019-12-12 14:07:48 +01:00
|
|
|
test('The entries have their dynamic zones populated', async () => {
|
2021-07-26 17:52:59 +02:00
|
|
|
const res = await rq({
|
|
|
|
method: 'GET',
|
|
|
|
url: '/',
|
|
|
|
qs: {
|
|
|
|
populate: ['field'],
|
|
|
|
},
|
|
|
|
});
|
2019-12-12 14:07:48 +01:00
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
2020-11-03 13:42:01 +01:00
|
|
|
|
2021-08-10 18:58:18 +02:00
|
|
|
expect(res.body.pagination).toBeDefined();
|
|
|
|
expect(Array.isArray(res.body.results)).toBe(true);
|
|
|
|
expect(res.body.results).toEqual(
|
2019-12-12 14:07:48 +01:00
|
|
|
expect.arrayContaining([
|
|
|
|
expect.objectContaining({
|
|
|
|
field: expect.arrayContaining([
|
|
|
|
expect.objectContaining({
|
|
|
|
id: expect.anything(),
|
|
|
|
__component: expect.any(String),
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
}),
|
|
|
|
])
|
|
|
|
);
|
|
|
|
});
|
2019-12-12 10:15:25 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Edition', () => {
|
2019-12-12 14:34:18 +01:00
|
|
|
test('Can empty non required dynamic zone', async () => {
|
2019-12-12 14:58:39 +01:00
|
|
|
const createRes = await createEntry();
|
2019-12-12 14:34:18 +01:00
|
|
|
|
2019-12-12 14:58:39 +01:00
|
|
|
expect(createRes.statusCode).toBe(200);
|
|
|
|
const entryId = createRes.body.id;
|
2019-12-12 14:34:18 +01:00
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
const res = await rq({
|
|
|
|
method: 'PUT',
|
|
|
|
url: `/${entryId}`,
|
2019-12-12 14:34:18 +01:00
|
|
|
body: {
|
|
|
|
field: [],
|
|
|
|
},
|
2021-07-26 17:52:59 +02:00
|
|
|
qs: {
|
|
|
|
populate: ['field'],
|
|
|
|
},
|
2019-12-12 14:34:18 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(Array.isArray(res.body.field)).toBe(true);
|
|
|
|
expect(res.body.field).toEqual([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Can add items to empty dynamic zone', async () => {
|
2019-12-12 14:58:39 +01:00
|
|
|
const createRes = await createEmpty();
|
2019-12-12 14:34:18 +01:00
|
|
|
|
2019-12-12 14:58:39 +01:00
|
|
|
expect(createRes.statusCode).toBe(200);
|
|
|
|
const entryId = createRes.body.id;
|
2019-12-12 14:34:18 +01:00
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
const res = await rq({
|
|
|
|
method: 'PUT',
|
|
|
|
url: `/${entryId}`,
|
2019-12-12 14:34:18 +01:00
|
|
|
body: defaultBody,
|
2021-07-26 17:52:59 +02:00
|
|
|
qs: {
|
|
|
|
populate: ['field'],
|
|
|
|
},
|
2019-12-12 14:34:18 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(Array.isArray(res.body.field)).toBe(true);
|
|
|
|
expect(res.body).toMatchObject({
|
|
|
|
field: [
|
|
|
|
{
|
|
|
|
id: expect.anything(),
|
|
|
|
__component: 'default.simple-compo',
|
|
|
|
name: 'someString',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: expect.anything(),
|
|
|
|
__component: 'default.compo-with-other-compo',
|
|
|
|
compo: {
|
|
|
|
id: expect.anything(),
|
|
|
|
name: 'someString',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Can remove items from dynamic zone', async () => {
|
2019-12-12 14:58:39 +01:00
|
|
|
const createRes = await createEntry();
|
2019-12-12 14:34:18 +01:00
|
|
|
|
2019-12-12 14:58:39 +01:00
|
|
|
expect(createRes.statusCode).toBe(200);
|
|
|
|
const entryId = createRes.body.id;
|
2019-12-12 14:34:18 +01:00
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
const res = await rq({
|
|
|
|
method: 'PUT',
|
|
|
|
url: `/${entryId}`,
|
2019-12-12 14:34:18 +01:00
|
|
|
body: {
|
|
|
|
field: [
|
|
|
|
{
|
|
|
|
__component: 'default.simple-compo',
|
|
|
|
name: 'otherString',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
__component: 'default.simple-compo',
|
|
|
|
name: 'secondString',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2021-07-26 17:52:59 +02:00
|
|
|
qs: {
|
|
|
|
populate: ['field'],
|
|
|
|
},
|
2019-12-12 14:34:18 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(Array.isArray(res.body.field)).toBe(true);
|
|
|
|
expect(res.body).toMatchObject({
|
|
|
|
field: [
|
|
|
|
{
|
|
|
|
id: expect.anything(),
|
|
|
|
__component: 'default.simple-compo',
|
|
|
|
name: 'otherString',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: expect.anything(),
|
|
|
|
__component: 'default.simple-compo',
|
|
|
|
name: 'secondString',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Respects min items', async () => {
|
2019-12-12 14:58:39 +01:00
|
|
|
const createRes = await createEntry();
|
2019-12-12 14:34:18 +01:00
|
|
|
|
2019-12-12 14:58:39 +01:00
|
|
|
expect(createRes.statusCode).toBe(200);
|
|
|
|
const entryId = createRes.body.id;
|
2019-12-12 14:34:18 +01:00
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
const res = await rq({
|
|
|
|
method: 'PUT',
|
|
|
|
url: `/${entryId}`,
|
2019-12-12 14:34:18 +01:00
|
|
|
body: {
|
|
|
|
field: [
|
|
|
|
{
|
|
|
|
__component: 'default.simple-compo',
|
|
|
|
name: 'otherString',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(400);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Respects max items', async () => {
|
2019-12-12 14:58:39 +01:00
|
|
|
const createRes = await createEntry();
|
2019-12-12 14:34:18 +01:00
|
|
|
|
2019-12-12 14:58:39 +01:00
|
|
|
expect(createRes.statusCode).toBe(200);
|
|
|
|
const entryId = createRes.body.id;
|
2019-12-12 14:34:18 +01:00
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
const res = await rq({
|
|
|
|
method: 'PUT',
|
|
|
|
url: `/${entryId}`,
|
2019-12-12 14:34:18 +01:00
|
|
|
body: {
|
|
|
|
field: Array(10).fill({
|
|
|
|
__component: 'default.simple-compo',
|
|
|
|
name: 'otherString',
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(400);
|
|
|
|
});
|
2019-12-12 10:15:25 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Deletion', () => {
|
2019-12-12 14:58:39 +01:00
|
|
|
test('Returns the entry with its paths populated', async () => {
|
|
|
|
const createRes = await createEntry();
|
|
|
|
|
|
|
|
expect(createRes.statusCode).toBe(200);
|
|
|
|
const entryId = createRes.body.id;
|
|
|
|
|
2021-07-26 17:52:59 +02:00
|
|
|
const res = await rq({
|
|
|
|
method: 'DELETE',
|
|
|
|
url: `/${entryId}`,
|
|
|
|
qs: {
|
|
|
|
populate: ['field'],
|
|
|
|
},
|
|
|
|
});
|
2019-12-12 14:58:39 +01:00
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(Array.isArray(res.body.field)).toBe(true);
|
|
|
|
expect(res.body).toEqual(
|
|
|
|
expect.objectContaining({
|
|
|
|
field: expect.arrayContaining([
|
|
|
|
expect.objectContaining({
|
|
|
|
id: expect.anything(),
|
|
|
|
__component: expect.any(String),
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
2019-12-12 10:15:25 +01:00
|
|
|
});
|
|
|
|
});
|