strapi/packages/core/content-manager/tests/components/single-required.test.e2e.js

434 lines
9.5 KiB
JavaScript
Raw Normal View History

'use strict';
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-08-09 10:44:24 +02:00
let strapi;
2019-08-09 10:44:24 +02:00
let rq;
const component = {
name: 'somecomponent',
attributes: {
name: {
type: 'string',
},
},
};
const ct = {
name: 'withcomponent',
attributes: {
field: {
type: 'component',
component: 'default.somecomponent',
repeatable: false,
required: true,
},
},
};
2019-08-09 10:44:24 +02:00
describe.each([
['CONTENT MANAGER', '/content-manager/collection-types/application::withcomponent.withcomponent'],
2019-10-22 18:01:03 +02:00
['GENERATED API', '/withcomponents'],
])('[%s] => Non repeatable and required component', (_, path) => {
const builder = createTestBuilder();
2020-12-29 16:24:57 +01:00
const hasPagination = path.includes('/content-manager');
2019-08-09 10:44:24 +02:00
beforeAll(async () => {
await builder
.addComponent(component)
.addContentType(ct)
.build();
2019-08-09 10:44:24 +02:00
strapi = await createStrapiInstance();
rq = await createAuthRequest({ strapi });
rq.setURLPrefix(path);
});
2019-08-09 10:44:24 +02:00
afterAll(async () => {
await strapi.destroy();
await builder.cleanup();
});
2019-08-09 10:44:24 +02:00
describe('POST new entry', () => {
test('Creating entry with JSON works', async () => {
const res = await rq.post('/', {
body: {
field: {
name: 'someString',
},
},
2021-07-08 21:53:30 +02:00
qs: {
populate: ['field'],
},
2019-08-09 10:44:24 +02:00
});
expect(res.statusCode).toBe(200);
expect(res.body.field).toEqual(
expect.objectContaining({
id: expect.anything(),
name: 'someString',
})
);
});
test('Creating a second entry works', async () => {
2019-08-09 10:44:24 +02:00
const res = await rq.post('/', {
body: {
field: {
name: 'someValue',
},
2019-08-09 10:44:24 +02:00
},
2021-07-08 21:53:30 +02:00
qs: {
populate: ['field'],
},
2019-08-09 10:44:24 +02:00
});
expect(res.statusCode).toBe(200);
expect(res.body.field).toEqual(
expect.objectContaining({
id: expect.anything(),
name: 'someValue',
})
);
});
test.each([[], 'someString', 128219, false])(
'Throws if the field is not an object %p',
async value => {
const res = await rq.post('/', {
body: {
field: value,
},
2021-07-08 21:53:30 +02:00
qs: {
populate: ['field'],
},
2019-08-09 10:44:24 +02:00
});
expect(res.statusCode).toBe(400);
}
);
test('Throws when sending a null value', async () => {
const res = await rq.post('/', {
body: {
field: null,
},
2021-07-08 21:53:30 +02:00
qs: {
populate: ['field'],
},
2019-08-09 10:44:24 +02:00
});
expect(res.statusCode).toBe(400);
});
2019-10-22 18:01:03 +02:00
test('Throws when the component is not provided', async () => {
2019-08-09 10:44:24 +02:00
const res = await rq.post('/', {
body: {},
2021-07-08 21:53:30 +02:00
qs: {
populate: ['field'],
},
2019-08-09 10:44:24 +02:00
});
expect(res.statusCode).toBe(400);
});
});
describe('GET entries', () => {
2019-10-22 18:01:03 +02:00
test('Should return entries with their nested components', async () => {
2021-07-08 21:53:30 +02:00
const res = await rq.get('/', {
qs: {
populate: ['field'],
},
});
2019-08-09 10:44:24 +02:00
expect(res.statusCode).toBe(200);
if (hasPagination) {
expect(res.body.pagination).toBeDefined();
expect(Array.isArray(res.body.results)).toBe(true);
res.body.results.forEach(entry => {
if (entry.field === null) return;
expect(entry.field).toMatchObject({
name: expect.any(String),
});
});
return;
}
2019-08-09 10:44:24 +02:00
expect(Array.isArray(res.body)).toBe(true);
res.body.forEach(entry => {
if (entry.field === null) return;
expect(entry.field).toMatchObject({
name: expect.any(String),
});
});
});
});
describe('PUT entry', () => {
test.each([[], 'someString', 128219, false])(
'Throws when sending invalid updated field %p',
async value => {
const res = await rq.post('/', {
body: {
field: {
name: 'someString',
},
},
2021-07-08 21:53:30 +02:00
qs: {
populate: ['field'],
},
2019-08-09 10:44:24 +02:00
});
const updateRes = await rq.put(`/${res.body.id}`, {
body: {
field: value,
},
2021-07-08 21:53:30 +02:00
qs: {
populate: ['field'],
},
2019-08-09 10:44:24 +02:00
});
expect(updateRes.statusCode).toBe(400);
// shouldn't have been updated
2021-07-08 21:53:30 +02:00
const getRes = await rq.get(`/${res.body.id}`, {
qs: {
populate: ['field'],
},
});
2019-08-09 10:44:24 +02:00
expect(getRes.statusCode).toBe(200);
2019-08-09 11:45:31 +02:00
expect(getRes.body).toMatchObject({
id: res.body.id,
field: res.body.field,
});
2019-08-09 10:44:24 +02:00
}
);
2019-10-22 18:01:03 +02:00
test('Keeps the previous value if component not sent', async () => {
2019-08-09 10:44:24 +02:00
const res = await rq.post('/', {
body: {
field: {
name: 'someString',
},
},
2021-07-08 21:53:30 +02:00
qs: {
populate: ['field'],
},
2019-08-09 10:44:24 +02:00
});
const updateRes = await rq.put(`/${res.body.id}`, {
body: {},
2021-07-08 21:53:30 +02:00
qs: {
populate: ['field'],
},
2019-08-09 10:44:24 +02:00
});
expect(updateRes.statusCode).toBe(200);
2019-08-09 11:45:31 +02:00
expect(updateRes.body).toMatchObject({
id: res.body.id,
field: res.body.field,
});
2019-08-09 10:44:24 +02:00
2021-07-08 21:53:30 +02:00
const getRes = await rq.get(`/${res.body.id}`, {
qs: {
populate: ['field'],
},
});
2019-08-09 10:44:24 +02:00
expect(getRes.statusCode).toBe(200);
2019-08-09 11:45:31 +02:00
expect(getRes.body).toMatchObject({
id: res.body.id,
field: res.body.field,
});
2019-08-09 10:44:24 +02:00
});
2019-10-22 18:01:03 +02:00
test('Throws if component is null', async () => {
2019-08-09 10:44:24 +02:00
const res = await rq.post('/', {
body: {
field: {
name: 'someString',
},
},
2021-07-08 21:53:30 +02:00
qs: {
populate: ['field'],
},
2019-08-09 10:44:24 +02:00
});
const updateRes = await rq.put(`/${res.body.id}`, {
body: {
field: null,
},
2021-07-08 21:53:30 +02:00
qs: {
populate: ['field'],
},
2019-08-09 10:44:24 +02:00
});
expect(updateRes.statusCode).toBe(400);
2021-07-08 21:53:30 +02:00
const getRes = await rq.get(`/${res.body.id}`, {
qs: {
populate: ['field'],
},
});
2019-08-09 10:44:24 +02:00
expect(getRes.statusCode).toBe(200);
expect(getRes.body).toMatchObject(res.body);
});
2019-10-22 18:01:03 +02:00
test('Replaces the previous component if sent without id', async () => {
2019-08-09 10:44:24 +02:00
const res = await rq.post('/', {
body: {
field: {
name: 'someString',
},
},
2021-07-08 21:53:30 +02:00
qs: {
populate: ['field'],
},
2019-08-09 10:44:24 +02:00
});
const updateRes = await rq.put(`/${res.body.id}`, {
body: {
field: {
name: 'new String',
},
},
2021-07-08 21:53:30 +02:00
qs: {
populate: ['field'],
},
2019-08-09 10:44:24 +02:00
});
expect(updateRes.statusCode).toBe(200);
expect(updateRes.body.field.id).not.toBe(res.body.field.id);
expect(updateRes.body).toMatchObject({
id: res.body.id,
field: {
name: 'new String',
},
});
2021-07-08 21:53:30 +02:00
const getRes = await rq.get(`/${res.body.id}`, {
qs: {
populate: ['field'],
},
});
2019-08-09 10:44:24 +02:00
expect(getRes.statusCode).toBe(200);
expect(getRes.body).toMatchObject({
id: res.body.id,
field: {
name: 'new String',
},
});
});
2019-10-22 18:01:03 +02:00
test('Throws on invalid id in component', async () => {
2019-08-09 10:44:24 +02:00
const res = await rq.post('/', {
body: {
field: {
name: 'someString',
},
},
2021-07-08 21:53:30 +02:00
qs: {
populate: ['field'],
},
2019-08-09 10:44:24 +02:00
});
const updateRes = await rq.put(`/${res.body.id}`, {
body: {
field: {
id: 'invalid_id',
name: 'new String',
},
},
});
expect(updateRes.statusCode).toBe(400);
});
2021-07-19 21:17:34 +02:00
test('Updates component if previous component id is sent', async () => {
2019-08-09 10:44:24 +02:00
const res = await rq.post('/', {
body: {
field: {
name: 'someString',
},
},
2021-07-08 21:53:30 +02:00
qs: {
populate: ['field'],
},
2019-08-09 10:44:24 +02:00
});
const updateRes = await rq.put(`/${res.body.id}`, {
body: {
field: {
2019-10-22 18:01:03 +02:00
id: res.body.field.id, // send old id to update the previous component
2019-08-09 10:44:24 +02:00
name: 'new String',
},
},
2021-07-08 21:53:30 +02:00
qs: {
populate: ['field'],
},
2019-08-09 10:44:24 +02:00
});
const expectedResult = {
id: res.body.id,
field: {
id: res.body.field.id,
name: 'new String',
},
};
expect(updateRes.statusCode).toBe(200);
expect(updateRes.body).toMatchObject(expectedResult);
2021-07-19 21:17:34 +02:00
const getRes = await rq.get(`/${res.body.id}`, {
qs: {
populate: ['field'],
},
});
2019-08-09 10:44:24 +02:00
expect(getRes.statusCode).toBe(200);
expect(getRes.body).toMatchObject(expectedResult);
});
});
describe('DELETE entry', () => {
2019-10-22 18:01:03 +02:00
test('Returns entry with components', async () => {
2019-08-09 10:44:24 +02:00
const res = await rq.post('/', {
body: {
field: {
name: 'someString',
},
},
2021-07-08 21:53:30 +02:00
qs: {
populate: ['field'],
},
2019-08-09 10:44:24 +02:00
});
2021-07-19 21:17:34 +02:00
const deleteRes = await rq.delete(`/${res.body.id}`, {
qs: {
populate: ['field'],
},
});
2019-08-09 10:44:24 +02:00
expect(deleteRes.statusCode).toBe(200);
expect(deleteRes.body).toMatchObject(res.body);
2021-07-19 21:17:34 +02:00
const getRes = await rq.get(`/${res.body.id}`, {
qs: {
populate: ['field'],
},
});
2019-08-09 10:44:24 +02:00
expect(getRes.statusCode).toBe(404);
});
});
});