strapi/packages/core/content-manager/tests/fields/enumeration.test.e2e.js

118 lines
2.8 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');
const builder = createTestBuilder();
let strapi;
let rq;
const ct = {
name: 'withenumeration',
attributes: {
field: {
type: 'enumeration',
enum: ['one', 'two'],
},
},
};
describe('Test type enumeration', () => {
beforeAll(async () => {
await builder.addContentType(ct).build();
strapi = await createStrapiInstance();
rq = await createAuthRequest({ strapi });
});
afterAll(async () => {
await strapi.destroy();
await builder.cleanup();
});
test('Create entry value enumeration input JSON', async () => {
2019-11-15 11:49:32 +01:00
const res = await rq.post(
'/content-manager/collection-types/application::withenumeration.withenumeration',
2019-11-15 11:49:32 +01:00
{
body: {
field: 'one',
},
}
);
expect(res.statusCode).toBe(200); // should return 201
expect(res.body).toMatchObject({
field: 'one',
});
});
test('Reading entry, returns correct value', async () => {
2019-11-15 11:49:32 +01:00
const res = await rq.get(
'/content-manager/collection-types/application::withenumeration.withenumeration'
2019-11-15 11:49:32 +01:00
);
expect(res.statusCode).toBe(200);
expect(res.body.pagination).toBeDefined();
expect(Array.isArray(res.body.results)).toBe(true);
res.body.results.forEach(entry => {
expect(['one', 'two'].includes(entry.field)).toBe(true);
});
});
test('Updating entry sets the right value and format', async () => {
2019-11-15 11:49:32 +01:00
const res = await rq.post(
'/content-manager/collection-types/application::withenumeration.withenumeration',
2019-11-15 11:49:32 +01:00
{
body: {
field: 'two',
},
}
);
const updateRes = await rq.put(
`/content-manager/collection-types/application::withenumeration.withenumeration/${res.body.id}`,
{
body: {
field: 'one',
},
}
);
expect(updateRes.statusCode).toBe(200);
expect(updateRes.body).toMatchObject({
id: res.body.id,
field: 'one',
});
});
test('Allows null value', async () => {
const res = await rq.post(
'/content-manager/collection-types/application::withenumeration.withenumeration',
{
body: {
field: null,
},
}
);
expect(res.statusCode).toBe(200); // should return 201
expect(res.body).toMatchObject({
field: null,
});
});
test('Throws an error when the enumeration value is not in the options', async () => {
const res = await rq.post(
'/content-manager/collection-types/application::withenumeration.withenumeration',
{
body: {
field: 'invalid-value',
},
}
);
expect(res.statusCode).toBe(400);
});
});