2020-10-27 11:27:17 +01:00
|
|
|
'use strict';
|
|
|
|
|
2019-12-04 18:49:47 +01:00
|
|
|
const { registerAndLogin } = require('../../../../test/helpers/auth');
|
|
|
|
const createModelsUtils = require('../../../../test/helpers/models');
|
|
|
|
const { createAuthRequest } = require('../../../../test/helpers/request');
|
|
|
|
|
|
|
|
let modelsUtils;
|
|
|
|
let rq;
|
|
|
|
|
|
|
|
describe('Test type date', () => {
|
|
|
|
beforeAll(async () => {
|
|
|
|
const token = await registerAndLogin();
|
|
|
|
rq = createAuthRequest(token);
|
|
|
|
|
|
|
|
modelsUtils = createModelsUtils({ rq });
|
|
|
|
|
2019-12-12 10:15:25 +01:00
|
|
|
await modelsUtils.createContentTypeWithType('withdatetime', 'datetime');
|
2019-12-04 18:49:47 +01:00
|
|
|
}, 60000);
|
|
|
|
|
|
|
|
afterAll(async () => {
|
2019-12-12 10:15:25 +01:00
|
|
|
await modelsUtils.deleteContentType('withdatetime');
|
2019-12-04 18:49:47 +01:00
|
|
|
}, 60000);
|
|
|
|
|
|
|
|
test('Create entry with valid value JSON', async () => {
|
2020-11-02 12:40:35 +01:00
|
|
|
const res = await rq.post(
|
|
|
|
'/content-manager/collection-types/application::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 valid value FormData', async () => {
|
|
|
|
const now = new Date(2019, 0, 12);
|
|
|
|
|
2020-11-02 12:40:35 +01:00
|
|
|
const res = await rq.post(
|
|
|
|
'/content-manager/collection-types/application::withdatetime.withdatetime',
|
|
|
|
{
|
|
|
|
formData: {
|
|
|
|
data: JSON.stringify({ field: now }),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2019-12-04 18:49:47 +01:00
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body).toMatchObject({
|
|
|
|
field: now.toISOString(),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Create entry with timestamp value should be converted to ISO', async () => {
|
|
|
|
const now = new Date(2016, 4, 8);
|
|
|
|
|
2020-11-02 12:40:35 +01:00
|
|
|
const res = await rq.post(
|
|
|
|
'/content-manager/collection-types/application::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);
|
|
|
|
|
2020-11-02 12:40:35 +01:00
|
|
|
const res = await rq.post(
|
|
|
|
'/content-manager/collection-types/application::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 () => {
|
2020-11-02 12:40:35 +01:00
|
|
|
const res = await rq.post(
|
|
|
|
'/content-manager/collection-types/application::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 () => {
|
2020-11-02 12:40:35 +01:00
|
|
|
const res = await rq.get(
|
|
|
|
'/content-manager/collection-types/application::withdatetime.withdatetime'
|
|
|
|
);
|
2019-12-04 18:49:47 +01:00
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(Array.isArray(res.body)).toBe(true);
|
|
|
|
res.body.forEach(entry => {
|
|
|
|
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);
|
|
|
|
|
2020-11-02 12:40:35 +01:00
|
|
|
const res = await rq.post(
|
|
|
|
'/content-manager/collection-types/application::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(
|
2020-11-02 12:40:35 +01:00
|
|
|
`/content-manager/collection-types/application::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(),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|