oa-jira
Version:
Octet Agile's JIRA connectivity project.
87 lines (79 loc) • 3.47 kB
JavaScript
const commons = require('../../../../src/commons');
const InvalidClass = require('../../../../src/commons/errors/invalid.class.error');
const Missing = require('../../../../src/commons/errors/missing.error');
describe('Check [date] utilities', () => {
describe('Check [check] utility', () => {
const now = new Date();
it.each([
[now, now, undefined],
[now, now, { required: true }],
[null, undefined, { required: false }]
])('should resolve [%s] when value is [%p] and options are [%p].', (expected, given, options) => {
expect(commons.date.check(given, options)).toEqual(expected);
});
it.each([
[new Missing('date'), undefined, undefined],
[new Missing('d'), undefined, { name: 'd' }],
[new Missing('date'), undefined, { required: true }],
[new InvalidClass('date', 'Date'), 'a', undefined],
[new InvalidClass('d', 'Date'), 'a', { name: 'd' }]
])('should reject [%p] when value is [%p] and options are [%p].', (error, given, options) => {
expect(() => commons.date.check(given, options)).toThrow(error);
});
});
describe('Check [resolve] utility', () => {
it.each([
[{}, undefined],
[{ required: true }, { required: true }]
])('should encapsulate in a promise the check function when given options is [%p].', (options, given) => {
const spyAsync = jest.spyOn(commons.promise, 'async').mockResolvedValue('called !');
expect(commons.date.resolve('value', given)).resolves.toEqual('called !');
expect(spyAsync).toHaveBeenCalledWith(commons.date.check, 'value', options);
jest.spyOn(commons.promise, 'async').mockRestore();
});
});
describe('Check [compare] utility', () => {
const first = new Date('2022-05-14T06:36:27.216Z');
const second = new Date('2022-05-19T22:00:00.000Z');
it.each([
[0, undefined, undefined],
[1, first, undefined],
[-1, undefined, first],
[-1, first, second],
[0, first, first],
[1, second, first]
])('should return [%p] when first date is [%p] and second date is [%p].', (expected, date1, date2) => {
expect(commons.date.compare(date1, date2)).toEqual(expected);
});
});
describe('Check [min] utility', () => {
const first = new Date('2022-05-14T06:36:27.216Z');
const second = new Date('2022-05-19T22:00:00.000Z');
it.each([
[undefined, undefined, undefined],
[first, first, undefined],
[first, undefined, first],
[first, first, second],
[first, first, first],
[first, second, first],
[first, first, second]
])('should return [%p] when first date is [%p] and second date is [%p].', (expected, date1, date2) => {
expect(commons.date.min(date1, date2)).toEqual(expected);
});
});
describe('Check [max] utility', () => {
const first = new Date('2022-05-14T06:36:27.216Z');
const second = new Date('2022-05-19T22:00:00.000Z');
it.each([
[undefined, undefined, undefined],
[first, first, undefined],
[first, undefined, first],
[second, first, second],
[first, first, first],
[second, second, first],
[second, first, second]
])('should return [%p] when first date is [%p] and second date is [%p].', (expected, date1, date2) => {
expect(commons.date.max(date1, date2)).toEqual(expected);
});
});
});