strapi/api-tests/core/content-manager/components/single-required.test.api.js
Marc Roig ff8ed1fc36
feat: Draft & Publish V5 (#18941)
* feat: use document service in content manager
* feat: locale and status filtering
* feat: refactor single type controllers to use documents
* feat: get locale param from in cm endpoints
* feat: get locale param from cm endpoints
* feat: prevent empty string locale filtering
* fix(content-manager): access to non default locale documents
* chore(content-manager): revert route construction
* test(content-manager): counting number of draft relations for non default locales
* chore(content-manager): remove default locale from entity manager countDraftRelations


---------

Co-authored-by: Ben Irvin <ben@innerdvations.com>
Co-authored-by: Jamie Howard <48524071+jhoward1994@users.noreply.github.com>
Co-authored-by: Josh <37798644+joshuaellis@users.noreply.github.com>
2024-01-25 18:43:08 +01:00

423 lines
9.4 KiB
JavaScript

'use strict';
const { createTestBuilder } = require('api-tests/builder');
const { createStrapiInstance } = require('api-tests/strapi');
const { createAuthRequest } = require('api-tests/request');
let strapi;
let rq;
const component = {
displayName: 'somecomponent',
attributes: {
name: {
type: 'string',
},
},
};
const ct = {
displayName: 'withcomponent',
singularName: 'withcomponent',
pluralName: 'withcomponents',
attributes: {
field: {
type: 'component',
component: 'default.somecomponent',
repeatable: false,
required: true,
},
},
};
describe('Non repeatable and required component', () => {
const builder = createTestBuilder();
beforeAll(async () => {
await builder.addComponent(component).addContentType(ct).build();
strapi = await createStrapiInstance();
rq = await createAuthRequest({ strapi });
rq.setURLPrefix('/content-manager/collection-types/api::withcomponent.withcomponent');
});
afterAll(async () => {
await strapi.destroy();
await builder.cleanup();
});
describe('POST new entry', () => {
test('Creating entry with JSON works', async () => {
const res = await rq.post('/', {
body: {
field: {
name: 'someString',
},
},
qs: {
populate: ['field'],
},
});
expect(res.statusCode).toBe(200);
expect(res.body.field).toEqual(
expect.objectContaining({
id: expect.anything(),
name: 'someString',
})
);
});
test('Creating a second entry works', async () => {
const res = await rq.post('/', {
body: {
field: {
name: 'someValue',
},
},
qs: {
populate: ['field'],
},
});
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,
},
qs: {
populate: ['field'],
},
});
expect(res.statusCode).toBe(400);
}
);
// TODO: Fix publishing validation in document service
test.skip('Throws when publishing a null value', async () => {
const creationRes = await rq.post('/', {
body: {
field: null,
},
qs: {
populate: ['field'],
},
});
const res = await rq.post(`/${creationRes.body.id}/actions/publish`);
expect(res.statusCode).toBe(400);
});
test.skip('Throws when the component is not provided', async () => {
const creationRes = await rq.post('/', {
body: {},
qs: {
populate: ['field'],
},
});
const res = await rq.post(`/${creationRes.body.id}/actions/publish`);
expect(res.statusCode).toBe(400);
});
});
describe('GET entries', () => {
test('Should return entries with their nested components', async () => {
const res = await rq.get('/', {
qs: {
populate: ['field'],
},
});
expect(res.statusCode).toBe(200);
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),
});
});
});
});
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',
},
},
qs: {
populate: ['field'],
},
});
const updateRes = await rq.put(`/${res.body.id}`, {
body: {
field: value,
},
qs: {
populate: ['field'],
},
});
expect(updateRes.statusCode).toBe(400);
// shouldn't have been updated
const getRes = await rq.get(`/${res.body.id}`, {
qs: {
populate: ['field'],
},
});
expect(getRes.statusCode).toBe(200);
expect(getRes.body).toMatchObject({
id: res.body.id,
field: res.body.field,
});
}
);
test('Keeps the previous value if component not sent', async () => {
const res = await rq.post('/', {
body: {
field: {
name: 'someString',
},
},
qs: {
populate: ['field'],
},
});
const updateRes = await rq.put(`/${res.body.id}`, {
body: {},
qs: {
populate: ['field'],
},
});
expect(updateRes.statusCode).toBe(200);
expect(updateRes.body).toMatchObject({
id: res.body.id,
field: res.body.field,
});
const getRes = await rq.get(`/${res.body.id}`, {
qs: {
populate: ['field'],
},
});
expect(getRes.statusCode).toBe(200);
expect(getRes.body).toMatchObject({
id: res.body.id,
field: res.body.field,
});
});
// Fix publishing validation in document service
test.skip('Throws when publishing if component is null', async () => {
const creationRes = await rq.post('/', {
body: {
field: {
name: 'someString',
},
},
qs: {
populate: ['field'],
},
});
const updateRes = await rq.put(`/${creationRes.body.id}`, {
body: {
field: null,
},
qs: {
populate: ['field'],
},
});
const res = await rq.post(`/${updateRes.body.id}/actions/publish`);
expect(res.statusCode).toBe(400);
const getRes = await rq.get(`/${creationRes.body.id}`, {
qs: {
populate: ['field'],
},
});
expect(getRes.statusCode).toBe(200);
expect(getRes.body).toMatchObject(updateRes.body);
});
test('Replaces the previous component if sent without id', async () => {
const res = await rq.post('/', {
body: {
field: {
name: 'someString',
},
},
qs: {
populate: ['field'],
},
});
const updateRes = await rq.put(`/${res.body.id}`, {
body: {
field: {
name: 'new String',
},
},
qs: {
populate: ['field'],
},
});
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}`, {
qs: {
populate: ['field'],
},
});
expect(getRes.statusCode).toBe(200);
expect(getRes.body).toMatchObject({
id: res.body.id,
field: {
name: 'new String',
},
});
});
test('Throws on invalid id in component', async () => {
const res = await rq.post('/', {
body: {
field: {
name: 'someString',
},
},
qs: {
populate: ['field'],
},
});
const updateRes = await rq.put(`/${res.body.id}`, {
body: {
field: {
id: 'invalid_id',
name: 'new String',
},
},
});
expect(updateRes.statusCode).toBe(400);
});
test('Updates component if previous component id is sent', async () => {
const res = await rq.post('/', {
body: {
field: {
name: 'someString',
},
},
qs: {
populate: ['field'],
},
});
const updateRes = await rq.put(`/${res.body.id}`, {
body: {
field: {
id: res.body.field.id, // send old id to update the previous component
name: 'new String',
},
},
qs: {
populate: ['field'],
},
});
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}`, {
qs: {
populate: ['field'],
},
});
expect(getRes.statusCode).toBe(200);
expect(getRes.body).toMatchObject(expectedResult);
});
});
describe('DELETE entry', () => {
test('Returns entry with components', async () => {
const res = await rq.post('/', {
body: {
field: {
name: 'someString',
},
},
qs: {
populate: ['field'],
},
});
const deleteRes = await rq.delete(`/${res.body.id}`, {
qs: {
populate: ['field'],
},
});
expect(deleteRes.statusCode).toBe(200);
const getRes = await rq.get(`/${res.body.id}`, {
qs: {
populate: ['field'],
},
});
expect(getRes.statusCode).toBe(404);
});
});
});