111 lines
2.8 KiB
JavaScript
Raw Normal View History

'use strict';
const { createTestBuilder } = require('../../../../test/helpers/builder');
const { createStrapiInstance } = require('../../../../test/helpers/strapi');
2019-12-04 18:49:47 +01:00
const { createAuthRequest } = require('../../../../test/helpers/request');
const builder = createTestBuilder();
let strapi;
2019-12-04 18:49:47 +01:00
let rq;
const ct = {
name: 'withtime',
attributes: {
field: {
type: 'time',
},
},
};
2019-12-05 15:26:01 +01:00
describe('Test type time', () => {
2019-12-04 18:49:47 +01:00
beforeAll(async () => {
await builder.addContentType(ct).build();
2019-12-04 18:49:47 +01:00
strapi = await createStrapiInstance({ ensureSuperAdmin: true });
rq = await createAuthRequest({ strapi });
2019-12-04 18:49:47 +01:00
}, 60000);
afterAll(async () => {
await strapi.destroy();
await builder.cleanup();
2019-12-04 18:49:47 +01:00
}, 60000);
test('Create entry with valid value JSON', async () => {
const res = await rq.post('/content-manager/collection-types/application::withtime.withtime', {
body: {
field: '10:10:57.123',
},
});
2019-12-04 18:49:47 +01:00
expect(res.statusCode).toBe(200);
expect(res.body).toMatchObject({
2019-12-05 15:26:01 +01:00
field: '10:10:57.123',
2019-12-04 18:49:47 +01:00
});
});
2019-12-05 15:26:01 +01:00
test.each(['00:00:00', '01:03:11.2', '01:03:11.93', '01:03:11.123'])(
'Accepts multiple time formats %s',
async input => {
const res = await rq.post(
'/content-manager/collection-types/application::withtime.withtime',
{
body: {
field: input,
},
}
);
2019-12-05 15:26:01 +01:00
expect(res.statusCode).toBe(200);
}
);
test.each(['24:11:23', '23:72:11', '12:45:83', 1234, {}, 'test', new Date()])(
'Throws on invalid time (%s)',
async input => {
const res = await rq.post(
'/content-manager/collection-types/application::withtime.withtime',
{
body: {
field: input,
},
}
);
2019-12-05 15:26:01 +01:00
expect(res.statusCode).toBe(400);
}
);
2019-12-04 18:49:47 +01:00
test('Reading entry, returns correct value', async () => {
const res = await rq.get('/content-manager/collection-types/application::withtime.withtime');
2019-12-04 18:49:47 +01:00
expect(res.statusCode).toBe(200);
expect(Array.isArray(res.body.results)).toBe(true);
res.body.results.forEach(entry => {
expect(entry.field).toMatch(/^2[0-3]|[01][0-9]:[0-5][0-9]:[0-5][0-9](.[0-9]{1,3})?$/);
2019-12-04 18:49:47 +01:00
});
});
test('Updating entry sets the right value and format JSON', async () => {
const res = await rq.post('/content-manager/collection-types/application::withtime.withtime', {
body: {
field: '12:11:04',
},
});
2019-12-04 18:49:47 +01:00
2019-12-05 15:26:01 +01:00
const uptimeRes = await rq.put(
`/content-manager/collection-types/application::withtime.withtime/${res.body.id}`,
2019-12-04 18:49:47 +01:00
{
body: {
2019-12-05 15:26:01 +01:00
field: '13:45:19.123',
2019-12-04 18:49:47 +01:00
},
}
);
2019-12-05 15:26:01 +01:00
expect(uptimeRes.statusCode).toBe(200);
expect(uptimeRes.body).toMatchObject({
2019-12-04 18:49:47 +01:00
id: res.body.id,
2019-12-05 15:26:01 +01:00
field: '13:45:19.123',
2019-12-04 18:49:47 +01:00
});
});
});