strapi/api-tests/core/content-manager/fields/datetime.test.api.js

126 lines
3.2 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-12-04 18:49:47 +01:00
const builder = createTestBuilder();
let strapi;
2019-12-04 18:49:47 +01:00
let rq;
const ct = {
2021-09-13 16:57:04 +02:00
displayName: 'withdatetime',
singularName: 'withdatetime',
pluralName: 'withdatetimes',
attributes: {
field: {
type: 'datetime',
},
},
};
2019-12-04 18:49:47 +01:00
// TODO: Fix document service validations
describe.skip('Test type datetime', () => {
beforeAll(async () => {
await builder.addContentType(ct).build();
2019-12-04 18:49:47 +01:00
strapi = await createStrapiInstance();
rq = await createAuthRequest({ strapi });
});
2019-12-04 18:49:47 +01:00
afterAll(async () => {
await strapi.destroy();
await builder.cleanup();
});
2019-12-04 18:49:47 +01:00
test('Create entry with valid value JSON', async () => {
2021-08-06 18:09:49 +02:00
const res = await rq.post('/content-manager/collection-types/api::withdatetime.withdatetime', {
body: {
field: '2019-08-08T10:10:57.000Z',
},
});
2019-12-04 18:49:47 +01:00
expect(res.statusCode).toBe(200);
expect(res.body).toMatchObject({
field: '2019-08-08T10:10:57.000Z',
});
});
test('Create entry with timestamp value should be converted to ISO', async () => {
const now = new Date(2016, 4, 8);
2021-08-06 18:09:49 +02:00
const res = await rq.post('/content-manager/collection-types/api::withdatetime.withdatetime', {
body: {
field: now.getTime(),
},
});
2019-12-04 18:49:47 +01:00
expect(res.statusCode).toBe(200);
expect(res.body).toMatchObject({
field: now.toISOString(),
});
});
test('Accepts string timestamp', async () => {
const now = new Date(2000, 0, 1);
2021-08-06 18:09:49 +02:00
const res = await rq.post('/content-manager/collection-types/api::withdatetime.withdatetime', {
body: {
field: `${now.getTime()}`,
},
});
2019-12-04 18:49:47 +01:00
expect(res.statusCode).toBe(200);
expect(res.body).toMatchObject({
field: now.toISOString(),
});
});
test('Throws on invalid date format', async () => {
2021-08-06 18:09:49 +02:00
const res = await rq.post('/content-manager/collection-types/api::withdatetime.withdatetime', {
body: {
field: 'azdazindoaizdnoainzd',
},
});
2019-12-04 18:49:47 +01:00
expect(res.statusCode).toBe(400);
});
test('Reading entry, returns correct value', async () => {
2021-08-06 18:09:49 +02:00
const res = await rq.get('/content-manager/collection-types/api::withdatetime.withdatetime');
2019-12-04 18:49:47 +01: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-12-04 18:49:47 +01:00
expect(new Date(entry.field).toISOString()).toBe(entry.field);
});
});
test('Updating entry sets the right value and format JSON', async () => {
const now = new Date(2018, 7, 5);
2021-08-06 18:09:49 +02:00
const res = await rq.post('/content-manager/collection-types/api::withdatetime.withdatetime', {
body: {
field: now.getTime(),
},
});
2019-12-04 18:49:47 +01:00
const newDate = new Date(2017, 10, 23);
const updateRes = await rq.put(
2021-08-06 18:09:49 +02:00
`/content-manager/collection-types/api::withdatetime.withdatetime/${res.body.id}`,
2019-12-04 18:49:47 +01:00
{
body: {
field: newDate,
},
}
);
expect(updateRes.statusCode).toBe(200);
expect(updateRes.body).toMatchObject({
id: res.body.id,
field: newDate.toISOString(),
});
});
});