341 lines
8.0 KiB
JavaScript
Raw Normal View History

'use strict';
2019-12-13 11:14:40 +01:00
const fs = require('fs');
const path = require('path');
2019-12-13 11:14:40 +01: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
let strapi;
2019-12-12 10:15:25 +01:00
let rq;
let baseRq;
2019-12-13 11:14:40 +01:00
const uploadImg = () => {
return baseRq({
method: 'POST',
url: '/upload',
2019-12-13 11:14:40 +01:00
formData: {
files: fs.createReadStream(path.join(__dirname, 'rec.jpg')),
2019-12-13 11:14:40 +01:00
},
});
};
2019-12-12 10:15:25 +01:00
const components = {
singleMedia: {
2021-11-02 18:27:49 +01:00
displayName: 'single-media',
attributes: {
media: {
type: 'media',
},
},
},
multipleMedia: {
2021-11-02 18:27:49 +01:00
displayName: 'multiple-media',
attributes: {
media: {
type: 'media',
multiple: true,
},
},
},
withNested: {
2021-11-02 18:27:49 +01:00
displayName: 'with-nested',
attributes: {
singleMedia: {
type: 'component',
component: 'default.single-media',
},
multipleMedia: {
type: 'component',
component: 'default.multiple-media',
},
},
},
};
const ct = {
2021-09-13 16:57:04 +02:00
displayName: 'withdynamiczonemedia',
singularName: 'withdynamiczonemedia',
pluralName: 'withdynamiczonemedias',
attributes: {
field: {
type: 'dynamiczone',
components: ['default.single-media', 'default.multiple-media', 'default.with-nested'],
},
},
};
describe('Not required dynamiczone', () => {
const builder = createTestBuilder();
2019-12-12 10:15:25 +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
strapi = await createStrapiInstance();
2019-12-12 10:15:25 +01:00
baseRq = await createAuthRequest({ strapi });
2019-12-12 10:15:25 +01:00
rq = await createAuthRequest({ strapi });
rq.setURLPrefix(
'/content-manager/collection-types/api::withdynamiczonemedia.withdynamiczonemedia'
);
});
2019-12-12 10:15:25 +01:00
afterAll(async () => {
await strapi.destroy();
await builder.cleanup();
});
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;
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;
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;
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;
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;
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
});
});