2019-08-08 10:41:05 +02:00
|
|
|
const { registerAndLogin } = require('../../../../test/helpers/auth');
|
|
|
|
const createModelsUtils = require('../../../../test/helpers/models');
|
|
|
|
const { createAuthRequest } = require('../../../../test/helpers/request');
|
|
|
|
|
|
|
|
let modelsUtils;
|
|
|
|
let rq;
|
|
|
|
|
2019-12-05 15:26:01 +01:00
|
|
|
describe('Test type date', () => {
|
2019-08-08 10:41:05 +02:00
|
|
|
beforeAll(async () => {
|
|
|
|
const token = await registerAndLogin();
|
|
|
|
rq = createAuthRequest(token);
|
|
|
|
|
|
|
|
modelsUtils = createModelsUtils({ rq });
|
|
|
|
|
2019-12-12 10:15:25 +01:00
|
|
|
await modelsUtils.createContentTypeWithType('withdate', 'date');
|
2019-08-08 10:41:05 +02:00
|
|
|
}, 60000);
|
|
|
|
|
|
|
|
afterAll(async () => {
|
2019-12-12 10:15:25 +01:00
|
|
|
await modelsUtils.deleteContentType('withdate');
|
2019-08-08 10:41:05 +02:00
|
|
|
}, 60000);
|
|
|
|
|
|
|
|
test('Create entry with valid value JSON', async () => {
|
2020-03-02 15:18:08 +01:00
|
|
|
const res = await rq.post('/content-manager/explorer/application::withdate.withdate', {
|
|
|
|
body: {
|
|
|
|
field: '2019-08-08',
|
|
|
|
},
|
|
|
|
});
|
2019-08-08 10:41:05 +02:00
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body).toMatchObject({
|
2019-12-05 15:26:01 +01:00
|
|
|
field: '2019-08-08',
|
2019-08-08 10:41:05 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Create entry with valid value FormData', async () => {
|
2019-08-08 12:34:40 +02:00
|
|
|
const now = new Date(2019, 0, 12);
|
2019-08-08 10:41:05 +02:00
|
|
|
|
2020-03-02 15:18:08 +01:00
|
|
|
const res = await rq.post('/content-manager/explorer/application::withdate.withdate', {
|
|
|
|
formData: {
|
|
|
|
data: JSON.stringify({ field: now }),
|
|
|
|
},
|
|
|
|
});
|
2019-08-08 10:41:05 +02:00
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body).toMatchObject({
|
2019-12-05 15:26:01 +01:00
|
|
|
field: '2019-01-12',
|
2019-08-08 10:41:05 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-12-05 15:26:01 +01:00
|
|
|
test.each([
|
|
|
|
'2019-08-08',
|
|
|
|
'2019-08-08 12:11:12',
|
|
|
|
'2019-08-08T00:00:00',
|
|
|
|
'2019-08-08T00:00:00Z',
|
|
|
|
'2019-08-08 00:00:00.123',
|
|
|
|
'2019-08-08 00:00:00.123Z',
|
|
|
|
'2019-08-08T00:00:00.123',
|
|
|
|
'2019-08-08T00:00:00.123Z',
|
2020-03-02 15:18:08 +01:00
|
|
|
])('Date can be sent in any iso format and the date part will be kept, (%s)', async input => {
|
|
|
|
const res = await rq.post('/content-manager/explorer/application::withdate.withdate', {
|
|
|
|
body: {
|
|
|
|
field: input,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body).toMatchObject({
|
|
|
|
field: '2019-08-08',
|
|
|
|
});
|
|
|
|
});
|
2019-12-05 15:26:01 +01:00
|
|
|
|
|
|
|
test.each([1234567891012, '1234567891012', '2019/12/11', '12:11:11'])(
|
|
|
|
'Throws on invalid date (%s)',
|
|
|
|
async value => {
|
2020-03-02 15:18:08 +01:00
|
|
|
const res = await rq.post('/content-manager/explorer/application::withdate.withdate', {
|
|
|
|
body: {
|
|
|
|
field: value,
|
|
|
|
},
|
|
|
|
});
|
2019-12-05 15:26:01 +01:00
|
|
|
|
|
|
|
expect(res.statusCode).toBe(400);
|
|
|
|
}
|
|
|
|
);
|
2019-08-08 10:41:05 +02:00
|
|
|
|
|
|
|
test('Reading entry, returns correct value', async () => {
|
2020-03-02 15:18:08 +01:00
|
|
|
const res = await rq.get('/content-manager/explorer/application::withdate.withdate');
|
2019-08-08 10:41:05 +02:00
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(Array.isArray(res.body)).toBe(true);
|
|
|
|
res.body.forEach(entry => {
|
2020-03-02 15:18:08 +01:00
|
|
|
expect(entry.field).toMatch(/^([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))$/);
|
2019-08-08 10:41:05 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Updating entry sets the right value and format JSON', async () => {
|
2019-08-08 12:34:40 +02:00
|
|
|
const now = new Date(2018, 7, 5);
|
2019-08-08 10:41:05 +02:00
|
|
|
|
2020-03-02 15:18:08 +01:00
|
|
|
const res = await rq.post('/content-manager/explorer/application::withdate.withdate', {
|
|
|
|
body: {
|
|
|
|
field: now,
|
|
|
|
},
|
|
|
|
});
|
2019-08-08 10:41:05 +02:00
|
|
|
|
2019-08-08 12:34:40 +02:00
|
|
|
const newDate = new Date(2017, 10, 23);
|
2019-08-08 10:41:05 +02:00
|
|
|
const updateRes = await rq.put(
|
2019-11-15 11:49:32 +01:00
|
|
|
`/content-manager/explorer/application::withdate.withdate/${res.body.id}`,
|
2019-08-08 10:41:05 +02:00
|
|
|
{
|
|
|
|
body: {
|
|
|
|
field: newDate,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(updateRes.statusCode).toBe(200);
|
|
|
|
expect(updateRes.body).toMatchObject({
|
|
|
|
id: res.body.id,
|
2019-12-05 15:26:01 +01:00
|
|
|
field: '2017-11-23',
|
2019-08-08 10:41:05 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|