298 lines
7.6 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
2019-12-12 10:15:25 +01:00
const { registerAndLogin } = require('../../../../test/helpers/auth');
const createModelsUtils = require('../../../../test/helpers/models');
const { createAuthRequest } = require('../../../../test/helpers/request');
let modelsUtils;
let rq;
2019-12-13 11:14:40 +01:00
let authRq;
2019-12-13 11:14:40 +01:00
const uploadImg = () => {
return authRq.post('/upload', {
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
describe.each([
[
'CONTENT MANAGER',
'/content-manager/collection-types/application::withdynamiczonemedia.withdynamiczonemedia',
2019-12-12 10:15:25 +01:00
],
['GENERATED API', '/withdynamiczonemedias'],
2019-12-12 10:15:25 +01:00
])('[%s] => Not required dynamiczone', (_, path) => {
beforeAll(async () => {
const token = await registerAndLogin();
2019-12-13 11:14:40 +01:00
authRq = createAuthRequest(token);
2019-12-12 10:15:25 +01:00
modelsUtils = createModelsUtils({ rq: authRq });
await modelsUtils.createComponent({
2019-12-13 11:14:40 +01:00
name: 'single-media',
2019-12-12 10:15:25 +01:00
attributes: {
2019-12-13 11:14:40 +01:00
media: {
2019-12-12 10:15:25 +01:00
type: 'media',
},
},
});
await modelsUtils.createComponent({
2019-12-13 11:14:40 +01:00
name: 'multiple-media',
2019-12-12 10:15:25 +01:00
attributes: {
2019-12-13 11:14:40 +01:00
media: {
2019-12-12 10:15:25 +01:00
type: 'media',
multiple: true,
},
},
});
await modelsUtils.createComponent({
name: 'with-nested',
attributes: {
2019-12-13 11:14:40 +01:00
singleMedia: {
2019-12-12 10:15:25 +01:00
type: 'component',
2019-12-13 11:14:40 +01:00
component: 'default.single-media',
2019-12-12 10:15:25 +01:00
},
2019-12-13 11:14:40 +01:00
multipleMedia: {
2019-12-12 10:15:25 +01:00
type: 'component',
2019-12-13 11:14:40 +01:00
component: 'default.multiple-media',
2019-12-12 10:15:25 +01:00
},
},
});
await modelsUtils.createContentTypeWithType('withdynamiczonemedia', 'dynamiczone', {
components: ['default.single-media', 'default.multiple-media', 'default.with-nested'],
});
2019-12-12 10:15:25 +01:00
rq = authRq.defaults({
baseUrl: `http://localhost:1337${path}`,
});
}, 60000);
afterAll(async () => {
await modelsUtils.deleteComponent('default.with-nested');
2019-12-13 11:14:40 +01:00
await modelsUtils.deleteComponent('default.single-media');
await modelsUtils.deleteComponent('default.multiple-media');
await modelsUtils.deleteContentType('withdynamiczonemedia');
2019-12-12 10:15:25 +01:00
}, 60000);
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.post('/', {
body: {
field: [
{
__component: 'default.single-media',
media: mediaId,
},
{
__component: 'default.multiple-media',
media: [mediaId, mediaId],
},
],
},
});
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.post('/', {
body: {
field: [
{
__component: 'default.single-media',
media: mediaId,
},
{
__component: 'default.multiple-media',
media: [mediaId, mediaId],
},
],
},
});
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.put(`/${res.body.id}`, {
body: {
field: [
{
__component: 'default.single-media',
media: newMediaId,
},
{
__component: 'default.multiple-media',
media: [newMediaId, newMediaId],
},
],
},
});
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.post('/', {
body: {
field: [
{
__component: 'default.single-media',
media: mediaId,
},
{
__component: 'default.multiple-media',
media: [mediaId, mediaId],
},
],
},
});
expect(res.statusCode).toBe(200);
const getRes = await rq.get(`/${res.body.id}`);
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.post('/', {
body: {
field: [
{
__component: 'default.with-nested',
singleMedia: {
media: mediaId,
},
multipleMedia: {
media: [mediaId, mediaId],
},
},
],
},
});
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
});
});