UNPKG

oa-jira

Version:

Octet Agile's JIRA connectivity project.

98 lines (89 loc) 4.49 kB
const commons = require('../../../../src/commons'); const InvalidType = require('../../../../src/commons/errors/invalid.type.error'); const Missing = require('../../../../src/commons/errors/missing.error'); const TooBig = require('../../../../src/commons/errors/too.big.error'); const TooSmall = require('../../../../src/commons/errors/too.small.error'); describe('Check [int] utilities', () => { describe('Check [check] utility', () => { it.each([ [1, 1, undefined], [2, 2, { required: true }], [null, undefined, { required: false }], [2, 2, { min: 2, max: 2 }] ])('should resolve [%s] when value is [%p] and options are [%p].', (expected, given, options) => { expect(commons.int.check(given, options)).toEqual(expected); }); it.each([ [new Missing('integer'), undefined, undefined], [new Missing('w'), undefined, { name: 'w' }], [new Missing('integer'), undefined, { required: true }], [new InvalidType('integer', 'integer'), '2', undefined], [new InvalidType('i', 'integer'), '2', { name: 'i' }], [new TooSmall('integer', -1), -2, { min: -1 }], [new TooSmall('integer', 1), 0, { min: 1 }], [new TooSmall('i', 2), 1, { name: 'i', min: 2 }], [new TooBig('integer', 1), 2, { max: 1 }], [new TooBig('integer', -2), -1, { max: -2 }] ])('should reject [%p] when value is [%p] and options are [%p].', (error, given, options) => { expect(() => commons.int.check(given, options)).toThrow(error); }); }); describe('Check [check bulk] utility', () => { it.each([ [[1], [1], undefined], [[2], [2], { required: true }], [[], [], { required: false }], [[2], [2], { min: 2, max: 2 }] ])('should resolve [%s] when value is [%p] and options are [%p].', (expected, given, options) => { expect(commons.int.checkBulk(given, options)).toEqual(expected); }); it.each([ [new InvalidType('integers', 'array'), 2, undefined], [new Missing('integers'), [undefined], undefined], [new Missing('w'), [undefined], { name: 'w' }], [new Missing('integers'), [undefined], { required: true }], [new InvalidType('integers', 'integer'), ['2'], undefined], [new InvalidType('i', 'integer'), ['2'], { name: 'i' }], [new TooSmall('integers', -1), [-2], { min: -1 }], [new TooSmall('integers', 1), [0], { min: 1 }], [new TooSmall('i', 2), [1], { name: 'i', min: 2 }], [new TooBig('integers', 1), [2], { max: 1 }], [new TooBig('integers', -2), [-1], { max: -2 }] ])('should reject [%p] when value is [%p] and options are [%p].', (error, given, options) => { expect(() => commons.int.checkBulk(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.int.resolve('value', given)).resolves.toEqual('called !'); expect(spyAsync).toHaveBeenCalledWith(commons.int.check, 'value', options); jest.spyOn(commons.promise, 'async').mockRestore(); }); }); describe('Check [resolve bulk] utility', () => { it('should reject when given nothing.', () => { expect(commons.int.resolveBulk()).rejects.toThrow(new InvalidType('integers', 'array')); }); it.each([ [[1], [1], undefined], [[1, 2], [1, 2], { required: true }], [[null], [undefined], { required: false }], [[2], [2], { min: 2, max: 2 }] ])('should resolve [%s] when value is [%p] and options are [%p].', (expected, given, options) => { expect(commons.int.resolveBulk(given, options)).resolves.toEqual(expected); }); it.each([ [new Missing('integers'), [undefined], undefined], [new InvalidType('i', 'integer'), ['2'], { name: 'i' }], [new TooSmall('integers', 1), [0], { min: 1 }], [new TooSmall('i', 2), [1], { name: 'i', min: 2 }], [new TooBig('integers', -2), [-1], { max: -2 }] ])('should reject [%p] when value is [%p] and options are [%p].', (error, given, options) => { expect(commons.int.resolveBulk(given, options)).rejects.toThrow(error); }); }); });