2020-10-27 11:27:17 +01:00
|
|
|
'use strict';
|
|
|
|
|
2019-12-13 11:14:40 +01:00
|
|
|
const fs = require('fs');
|
2020-10-27 11:27:17 +01:00
|
|
|
const path = require('path');
|
2019-12-13 11:14:40 +01:00
|
|
|
|
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');
|
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;
|
2020-11-17 15:38:41 +01:00
|
|
|
let baseRq;
|
2020-11-03 13:42:01 +01:00
|
|
|
|
2019-12-13 11:14:40 +01:00
|
|
|
const uploadImg = () => {
|
2020-11-17 15:38:41 +01:00
|
|
|
return baseRq({
|
|
|
|
method: 'POST',
|
|
|
|
url: '/upload',
|
2019-12-13 11:14:40 +01:00
|
|
|
formData: {
|
2020-10-27 11:27:17 +01:00
|
|
|
files: fs.createReadStream(path.join(__dirname, 'rec.jpg')),
|
2019-12-13 11:14:40 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
2019-12-12 10:15:25 +01:00
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
const components = {
|
|
|
|
singleMedia: {
|
|
|
|
name: 'single-media',
|
|
|
|
attributes: {
|
|
|
|
media: {
|
|
|
|
type: 'media',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
multipleMedia: {
|
|
|
|
name: 'multiple-media',
|
|
|
|
attributes: {
|
|
|
|
media: {
|
|
|
|
type: 'media',
|
|
|
|
multiple: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
withNested: {
|
|
|
|
name: 'with-nested',
|
|
|
|
attributes: {
|
|
|
|
singleMedia: {
|
|
|
|
type: 'component',
|
|
|
|
component: 'default.single-media',
|
|
|
|
},
|
|
|
|
multipleMedia: {
|
|
|
|
type: 'component',
|
|
|
|
component: 'default.multiple-media',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const ct = {
|
|
|
|
name: 'withdynamiczonemedia',
|
|
|
|
attributes: {
|
|
|
|
field: {
|
|
|
|
type: 'dynamiczone',
|
|
|
|
components: ['default.single-media', 'default.multiple-media', 'default.with-nested'],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2021-08-10 18:58:18 +02:00
|
|
|
describe('Not required dynamiczone', () => {
|
2020-11-17 15:38:41 +01:00
|
|
|
const builder = createTestBuilder();
|
2019-12-12 10:15:25 +01:00
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
beforeAll(async () => {
|
|
|
|
await builder
|
|
|
|
.addComponent(components.singleMedia)
|
|
|
|
.addComponent(components.multipleMedia)
|
|
|
|
.addComponent(components.withNested)
|
|
|
|
.addContentType(ct)
|
|
|
|
.build();
|
2019-12-12 10:15:25 +01:00
|
|
|
|
2020-11-30 20:20:36 +01:00
|
|
|
strapi = await createStrapiInstance();
|
2019-12-12 10:15:25 +01:00
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
baseRq = await createAuthRequest({ strapi });
|
2019-12-12 10:15:25 +01:00
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
rq = await createAuthRequest({ strapi });
|
2021-08-10 18:58:18 +02:00
|
|
|
rq.setURLPrefix(
|
2021-08-13 15:43:04 +02:00
|
|
|
'/content-manager/collection-types/api::withdynamiczonemedia.withdynamiczonemedia'
|
2021-08-10 18:58:18 +02:00
|
|
|
);
|
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-29 12:33:08 +02:00
|
|
|
});
|
2019-12-12 10:15:25 +01:00
|
|
|
|
|
|
|
describe('Contains components with medias', () => {
|
2019-12-13 11:14:40 +01:00
|
|
|
test('The medias are correctly related to the components on creation', async () => {
|
|
|
|
const imgRes = await uploadImg();
|
|
|
|
|
|
|
|
expect(imgRes.statusCode).toBe(200);
|
|
|
|
const mediaId = imgRes.body[0].id;
|
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
const res = await rq({
|
|
|
|
method: 'POST',
|
|
|
|
url: '/',
|
2019-12-13 11:14:40 +01:00
|
|
|
body: {
|
|
|
|
field: [
|
|
|
|
{
|
|
|
|
__component: 'default.single-media',
|
|
|
|
media: mediaId,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
__component: 'default.multiple-media',
|
|
|
|
media: [mediaId, mediaId],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2021-07-28 15:32:21 +02:00
|
|
|
qs: {
|
|
|
|
populate: ['field'],
|
|
|
|
},
|
2019-12-13 11:14:40 +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.single-media',
|
|
|
|
media: {
|
|
|
|
id: mediaId,
|
|
|
|
url: expect.any(String),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: expect.anything(),
|
|
|
|
__component: 'default.multiple-media',
|
|
|
|
media: expect.arrayContaining([
|
|
|
|
expect.objectContaining({
|
|
|
|
id: mediaId,
|
|
|
|
url: expect.any(String),
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('The medias are correctly related to the components on edition', async () => {
|
|
|
|
const imgRes = await uploadImg();
|
|
|
|
|
|
|
|
expect(imgRes.statusCode).toBe(200);
|
|
|
|
const mediaId = imgRes.body[0].id;
|
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
const res = await rq({
|
|
|
|
method: 'POST',
|
|
|
|
url: '/',
|
2019-12-13 11:14:40 +01:00
|
|
|
body: {
|
|
|
|
field: [
|
|
|
|
{
|
|
|
|
__component: 'default.single-media',
|
|
|
|
media: mediaId,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
__component: 'default.multiple-media',
|
|
|
|
media: [mediaId, mediaId],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2021-07-28 15:32:21 +02:00
|
|
|
qs: {
|
|
|
|
populate: ['field'],
|
|
|
|
},
|
2019-12-13 11:14:40 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(Array.isArray(res.body.field)).toBe(true);
|
|
|
|
|
|
|
|
const newImgRes = await uploadImg();
|
|
|
|
|
|
|
|
expect(newImgRes.statusCode).toBe(200);
|
|
|
|
const newMediaId = newImgRes.body[0].id;
|
2020-11-17 15:38:41 +01:00
|
|
|
const updateRes = await rq({
|
|
|
|
method: 'PUT',
|
|
|
|
url: `/${res.body.id}`,
|
2019-12-13 11:14:40 +01:00
|
|
|
body: {
|
|
|
|
field: [
|
|
|
|
{
|
|
|
|
__component: 'default.single-media',
|
|
|
|
media: newMediaId,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
__component: 'default.multiple-media',
|
|
|
|
media: [newMediaId, newMediaId],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2021-07-28 15:32:21 +02:00
|
|
|
qs: {
|
|
|
|
populate: ['field'],
|
|
|
|
},
|
2019-12-13 11:14:40 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(updateRes.body).toMatchObject({
|
|
|
|
field: [
|
|
|
|
{
|
|
|
|
id: expect.anything(),
|
|
|
|
__component: 'default.single-media',
|
|
|
|
media: {
|
|
|
|
id: newMediaId,
|
|
|
|
url: expect.any(String),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: expect.anything(),
|
|
|
|
__component: 'default.multiple-media',
|
|
|
|
media: expect.arrayContaining([
|
|
|
|
expect.objectContaining({
|
|
|
|
id: newMediaId,
|
|
|
|
url: expect.any(String),
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('The media are populated on the components', async () => {
|
|
|
|
const imgRes = await uploadImg();
|
|
|
|
|
|
|
|
expect(imgRes.statusCode).toBe(200);
|
|
|
|
const mediaId = imgRes.body[0].id;
|
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
const res = await rq({
|
|
|
|
method: 'POST',
|
|
|
|
url: '/',
|
2019-12-13 11:14:40 +01:00
|
|
|
body: {
|
|
|
|
field: [
|
|
|
|
{
|
|
|
|
__component: 'default.single-media',
|
|
|
|
media: mediaId,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
__component: 'default.multiple-media',
|
|
|
|
media: [mediaId, mediaId],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2021-07-28 15:32:21 +02:00
|
|
|
qs: {
|
|
|
|
populate: ['field'],
|
|
|
|
},
|
2019-12-13 11:14:40 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
|
2021-07-28 15:32:21 +02:00
|
|
|
const getRes = await rq({
|
|
|
|
method: 'GET',
|
|
|
|
url: `/${res.body.id}`,
|
|
|
|
qs: {
|
|
|
|
populate: ['field'],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2019-12-13 11:14:40 +01:00
|
|
|
expect(getRes.body).toMatchObject({
|
|
|
|
field: [
|
|
|
|
{
|
|
|
|
id: expect.anything(),
|
|
|
|
__component: 'default.single-media',
|
|
|
|
media: {
|
|
|
|
id: mediaId,
|
|
|
|
url: expect.any(String),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: expect.anything(),
|
|
|
|
__component: 'default.multiple-media',
|
|
|
|
media: expect.arrayContaining([
|
|
|
|
expect.objectContaining({
|
|
|
|
id: mediaId,
|
|
|
|
url: expect.any(String),
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
});
|
2019-12-12 10:15:25 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Contains components with nested components having medias', () => {
|
2019-12-13 11:14:40 +01:00
|
|
|
test('The medias are correctly related to the nested components on creation', async () => {
|
|
|
|
const imgRes = await uploadImg();
|
|
|
|
|
|
|
|
expect(imgRes.statusCode).toBe(200);
|
|
|
|
const mediaId = imgRes.body[0].id;
|
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
const res = await rq({
|
|
|
|
method: 'POST',
|
|
|
|
url: '/',
|
2019-12-13 11:14:40 +01:00
|
|
|
body: {
|
|
|
|
field: [
|
|
|
|
{
|
|
|
|
__component: 'default.with-nested',
|
|
|
|
singleMedia: {
|
|
|
|
media: mediaId,
|
|
|
|
},
|
|
|
|
multipleMedia: {
|
|
|
|
media: [mediaId, mediaId],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2021-07-28 15:32:21 +02:00
|
|
|
qs: {
|
|
|
|
populate: ['field'],
|
|
|
|
},
|
2019-12-13 11:14:40 +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.with-nested',
|
|
|
|
singleMedia: {
|
|
|
|
media: {
|
|
|
|
id: mediaId,
|
|
|
|
url: expect.any(String),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
multipleMedia: {
|
|
|
|
media: expect.arrayContaining([
|
|
|
|
expect.objectContaining({
|
|
|
|
id: mediaId,
|
|
|
|
url: expect.any(String),
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
});
|
2019-12-12 10:15:25 +01:00
|
|
|
});
|
|
|
|
});
|