strapi/tests/api/core/content-manager/components/repeatable-required.test.api.js

606 lines
13 KiB
JavaScript
Raw Normal View History

'use strict';
const { createTestBuilder } = require('api-tests/builder');
const { createStrapiInstance } = require('api-tests/strapi');
const { createAuthRequest } = require('api-tests/request');
2019-08-09 10:44:24 +02:00
let strapi;
2019-08-09 10:44:24 +02:00
let rq;
const component = {
2021-11-02 18:27:49 +01:00
displayName: 'somecomponent',
attributes: {
name: {
type: 'string',
},
},
};
const ct = {
2021-09-13 16:57:04 +02:00
displayName: 'withcomponent',
singularName: 'withcomponent',
pluralName: 'withcomponents',
attributes: {
field: {
type: 'component',
component: 'default.somecomponent',
repeatable: true,
required: true,
},
},
};
describe('Non repeatable and Not required component', () => {
const builder = createTestBuilder();
2019-08-09 10:44:24 +02:00
beforeAll(async () => {
2022-08-08 23:33:39 +02:00
await builder.addComponent(component).addContentType(ct).build();
2019-08-09 10:44:24 +02:00
strapi = await createStrapiInstance();
rq = await createAuthRequest({ strapi });
rq.setURLPrefix('/content-manager/collection-types/api::withcomponent.withcomponent');
});
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-19 21:17:34 +02:00
qs: {
populate: ['field'],
},
2019-08-09 10:44:24 +02:00
});
expect(res.statusCode).toBe(200);
expect(Array.isArray(res.body.field)).toBe(true);
expect(res.body.field).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: expect.anything(),
name: 'someString',
}),
])
);
});
test('Creating second entry', 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-19 21:17:34 +02:00
qs: {
populate: ['field'],
},
2019-08-09 10:44:24 +02:00
});
expect(res.statusCode).toBe(200);
expect(Array.isArray(res.body.field)).toBe(true);
expect(res.body.field).toEqual(
expect.arrayContaining([
expect.objectContaining({
id: expect.anything(),
name: 'someValue',
}),
])
);
});
test.each(['someString', 128219, false, {}, null])(
'Throws if the field is not an object %p',
2022-08-08 23:33:39 +02:00
async (value) => {
2019-08-09 10:44:24 +02:00
const res = await rq.post('/', {
body: {
field: value,
},
});
expect(res.statusCode).toBe(400);
}
);
test('Can send an empty array', async () => {
const res = await rq.post('/', {
body: {
field: [],
},
2021-07-19 21:17:34 +02:00
qs: {
populate: ['field'],
},
2019-08-09 10:44:24 +02:00
});
expect(res.statusCode).toBe(200);
expect(res.body.field).toEqual([]);
});
2019-10-22 18:01:03 +02:00
test('Throws when component is not provided', async () => {
2019-08-09 10:44:24 +02:00
const res = await rq.post('/', {
body: {},
2021-07-19 21:17:34 +02:00
qs: {
populate: ['field'],
},
2019-08-09 10:44:24 +02:00
});
expect(res.statusCode).toBe(400);
});
});
describe('GET entries', () => {
test('Data is orderd in the order sent', async () => {
const res = await rq.post('/', {
body: {
field: [
{
name: 'firstString',
},
{
name: 'someString',
},
],
},
2021-07-19 21:17:34 +02:00
qs: {
populate: ['field'],
},
});
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(Array.isArray(getRes.body.field)).toBe(true);
expect(getRes.body.field[0]).toMatchObject({
name: 'firstString',
});
expect(getRes.body.field[1]).toMatchObject({
name: 'someString',
});
});
2019-10-22 18:01:03 +02:00
test('Should return entries with their nested components', async () => {
2021-07-19 21:17:34 +02:00
const res = await rq.get('/', {
qs: {
populate: ['field'],
},
});
2019-08-09 10:44:24 +02:00
expect(res.statusCode).toBe(200);
expect(res.body.pagination).toBeDefined();
expect(Array.isArray(res.body.results)).toBe(true);
2022-08-08 23:33:39 +02:00
res.body.results.forEach((entry) => {
2019-08-09 10:44:24 +02:00
expect(Array.isArray(entry.field)).toBe(true);
if (entry.field.length === 0) return;
expect(entry.field).toEqual(
expect.arrayContaining([
expect.objectContaining({
name: expect.any(String),
}),
])
);
});
});
});
describe('PUT entry', () => {
test.each(['someString', 128219, false, {}, null])(
'Throws when sending invalid updated field %p',
2022-08-08 23:33:39 +02:00
async (value) => {
2019-08-09 10:44:24 +02:00
const res = await rq.post('/', {
body: {
field: [
{
name: 'someString',
},
],
},
2021-07-19 21:17:34 +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-19 21:17:34 +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-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);
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
}
);
test('Updates order at each request', async () => {
const res = await rq.post('/', {
body: {
field: [
{
name: 'someString',
},
{
name: 'otherString',
},
],
},
2021-07-19 21:17:34 +02:00
qs: {
populate: ['field'],
},
2019-08-09 10:44:24 +02:00
});
expect(res.body.field[0]).toMatchObject({
name: 'someString',
});
expect(res.body.field[1]).toMatchObject({
name: 'otherString',
});
const updateRes = await rq.put(`/${res.body.id}`, {
body: {
field: [
{
name: 'otherString',
},
{
name: 'someString',
},
],
},
2021-07-19 21:17:34 +02:00
qs: {
populate: ['field'],
},
2019-08-09 10:44:24 +02:00
});
expect(updateRes.statusCode).toBe(200);
expect(Array.isArray(updateRes.body.field)).toBe(true);
expect(updateRes.body.field[0]).toMatchObject({
name: 'otherString',
});
expect(updateRes.body.field[1]).toMatchObject({
name: 'someString',
});
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(Array.isArray(getRes.body.field)).toBe(true);
expect(getRes.body.field[0]).toMatchObject({
name: 'otherString',
});
expect(getRes.body.field[1]).toMatchObject({
name: 'someString',
});
});
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',
},
{
name: 'otherString',
},
],
},
2021-07-19 21:17:34 +02:00
qs: {
populate: ['field'],
},
2019-08-09 10:44:24 +02:00
});
const updateRes = await rq.put(`/${res.body.id}`, {
body: {},
2021-07-19 21:17:34 +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-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);
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('Removes previous components if empty array sent', async () => {
2019-08-09 10:44:24 +02:00
const res = await rq.post('/', {
body: {
field: [
{
name: 'someString',
},
],
},
2021-07-19 21:17:34 +02:00
qs: {
populate: ['field'],
},
2019-08-09 10:44:24 +02:00
});
const updateRes = await rq.put(`/${res.body.id}`, {
body: {
field: [],
},
2021-07-19 21:17:34 +02:00
qs: {
populate: ['field'],
},
2019-08-09 10:44:24 +02:00
});
const expectResult = {
id: res.body.id,
field: [],
};
expect(updateRes.statusCode).toBe(200);
expect(updateRes.body).toMatchObject(expectResult);
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(expectResult);
});
2019-10-22 18:01:03 +02:00
test('Replaces the previous components if sent without id', async () => {
2019-08-09 10:44:24 +02:00
const res = await rq.post('/', {
body: {
field: [
{
name: 'someString',
},
],
},
2021-07-19 21:17:34 +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-19 21:17:34 +02:00
qs: {
populate: ['field'],
},
2019-08-09 10:44:24 +02:00
});
expect(updateRes.statusCode).toBe(200);
2022-08-08 23:33:39 +02:00
const oldIds = res.body.field.map((val) => val.id);
updateRes.body.field.forEach((val) => {
2019-08-09 10:44:24 +02:00
expect(oldIds.includes(val.id)).toBe(false);
});
expect(updateRes.body).toMatchObject({
id: res.body.id,
field: [
{
name: 'new String',
},
],
});
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({
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-19 21:17:34 +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',
},
],
},
2021-07-19 21:17:34 +02:00
qs: {
populate: ['field'],
},
2019-08-09 10:44:24 +02:00
});
expect(updateRes.statusCode).toBe(400);
});
2019-10-22 18:01:03 +02:00
test('Updates component with ids, create new ones and removes old ones', async () => {
2019-08-09 10:44:24 +02:00
const res = await rq.post('/', {
body: {
field: [
{
name: 'one',
},
{
name: 'two',
},
{
name: 'three',
},
],
},
2021-07-19 21:17:34 +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[0].id, // send old id to update the previous component
2019-08-09 10:44:24 +02:00
name: 'newOne',
},
{
name: 'newTwo',
},
{
id: res.body.field[2].id,
name: 'three',
},
{
name: 'four',
},
],
},
2021-07-19 21:17:34 +02:00
qs: {
populate: ['field'],
},
2019-08-09 10:44:24 +02:00
});
const expectedResult = {
id: res.body.id,
field: [
{
id: res.body.field[0].id,
name: 'newOne',
},
{
name: 'newTwo',
},
{
id: res.body.field[2].id,
name: 'three',
},
{
name: 'four',
},
],
};
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',
},
{
name: 'someOtherString',
},
{
name: 'otherSomeString',
},
],
},
2021-07-19 21:17:34 +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);
});
});
});