344 lines
8.1 KiB
JavaScript
Raw Normal View History

'use strict';
2019-08-09 10:44:24 +02:00
const { registerAndLogin } = require('../../../../test/helpers/auth');
const createModelsUtils = require('../../../../test/helpers/models');
const { createAuthRequest } = require('../../../../test/helpers/request');
let modelsUtils;
let rq;
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 hasPagination = path.includes('/content-manager');
2019-08-09 10:44:24 +02:00
beforeAll(async () => {
const token = await registerAndLogin();
const authRq = createAuthRequest(token);
modelsUtils = createModelsUtils({ rq: authRq });
2019-10-22 18:01:03 +02:00
await modelsUtils.createComponent({
name: 'somecomponent',
2019-08-09 10:44:24 +02:00
attributes: {
name: {
type: 'string',
},
},
});
2019-12-12 10:15:25 +01:00
await modelsUtils.createContentTypeWithType('withcomponent', 'component', {
component: 'default.somecomponent',
2019-08-09 10:44:24 +02:00
repeatable: false,
required: true,
});
rq = authRq.defaults({
baseUrl: `http://localhost:1337${path}`,
});
}, 60000);
afterAll(async () => {
2019-10-29 18:39:25 +01:00
await modelsUtils.deleteComponent('default.somecomponent');
2019-12-12 10:15:25 +01:00
await modelsUtils.deleteContentType('withcomponent');
2019-08-09 11:04:48 +02:00
}, 60000);
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',
},
},
});
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
},
});
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,
},
});
expect(res.statusCode).toBe(400);
}
);
test('Throws when sending a null value', async () => {
const res = await rq.post('/', {
body: {
field: null,
},
});
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: {},
});
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 () => {
2019-08-09 10:44:24 +02:00
const res = await rq.get('/');
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',
},
},
});
const updateRes = await rq.put(`/${res.body.id}`, {
body: {
field: value,
},
});
expect(updateRes.statusCode).toBe(400);
// shouldn't have been updated
const getRes = await rq.get(`/${res.body.id}`);
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',
},
},
});
const updateRes = await rq.put(`/${res.body.id}`, {
body: {},
});
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
const getRes = await rq.get(`/${res.body.id}`);
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',
},
},
});
const updateRes = await rq.put(`/${res.body.id}`, {
body: {
field: null,
},
});
expect(updateRes.statusCode).toBe(400);
const getRes = await rq.get(`/${res.body.id}`);
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',
},
},
});
const updateRes = await rq.put(`/${res.body.id}`, {
body: {
field: {
name: 'new String',
},
},
});
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',
},
});
const getRes = await rq.get(`/${res.body.id}`);
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',
},
},
});
const updateRes = await rq.put(`/${res.body.id}`, {
body: {
field: {
id: 'invalid_id',
name: 'new String',
},
},
});
expect(updateRes.statusCode).toBe(400);
});
2019-10-22 18:01:03 +02:00
test('Updates component if previsous component id is sent', async () => {
2019-08-09 10:44:24 +02:00
const res = await rq.post('/', {
body: {
field: {
name: 'someString',
},
},
});
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',
},
},
});
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);
const getRes = await rq.get(`/${res.body.id}`);
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',
},
},
});
const deleteRes = await rq.delete(`/${res.body.id}`);
expect(deleteRes.statusCode).toBe(200);
expect(deleteRes.body).toMatchObject(res.body);
const getRes = await rq.get(`/${res.body.id}`);
expect(getRes.statusCode).toBe(404);
});
});
});