oa-jira
Version:
Octet Agile's JIRA connectivity project.
80 lines (74 loc) • 4.17 kB
JavaScript
const commons = require('../../../../src/commons');
const InvalidType = require('../../../../src/commons/errors/invalid.type.error');
const Missing = require('../../../../src/commons/errors/missing.error');
const Prohibited = require('../../../../src/commons/errors/prohibited.error');
const TooBig = require('../../../../src/commons/errors/too.big.error');
const TooSmall = require('../../../../src/commons/errors/too.small.error');
describe('Check [string] utilities', () => {
describe('Check [check] utility', () => {
it.each([
['a', ' a ', undefined],
['a', ' a ', { required: true }],
[null, undefined, { required: false }],
['a', ' a ', { trim: true, accepted: [] }],
['a', ' a ', { trim: true, accepted: 2 }],
[' a ', ' a ', { trim: false, minLength: 3, maxLength: 3, accepted: [' a '] }]
])('should resolve [%s] when value is [%p] and options are [%p].', (expected, given, options) => {
expect(commons.string.check(given, options)).toEqual(expected);
});
it.each([
[new Missing('string'), undefined, undefined],
[new Missing('w'), undefined, { name: 'w' }],
[new Missing('string'), undefined, { required: true }],
[new InvalidType('string', 'string'), 2, undefined],
[new InvalidType('w', 'string'), 2, { name: 'w' }, 'w'],
[new TooSmall('string', 1), '', { minLength: 1 }],
[new TooSmall('w', 2), ' a ', { name: 'w', minLength: 2 }],
[new TooBig('string', 1), 'aa', { maxLength: 1 }],
[new TooBig('w', 2), ' aaa ', { name: 'w', maxLength: 2 }],
[new Prohibited('string', 'aa'), ' aa ', { trim: true, accepted: [' aa '] }],
[new Prohibited('w', 'aa'), ' aa ', { name: 'w', trim: true, accepted: [' aa '] }, 'w']
])('should reject [%p] when value is [%p] and options are [%p].', (error, given, options) => {
expect(() => commons.string.check(given, options)).toThrow(error);
});
});
describe('Check [check bulk] utility', () => {
it.each([
[['a'], [' a '], undefined],
[['a'], [' a '], { required: true }],
[[null], [undefined], { required: false }],
[['a'], [' a '], { trim: true, accepted: [] }],
[['a'], [' a '], { trim: true, accepted: 2 }],
[[' a '], [' a '], { trim: false, minLength: 3, maxLength: 3, accepted: [' a '] }]
])('should resolve [%s] when value is [%p] and options are [%p].', (expected, given, options) => {
expect(commons.string.checkBulk(given, options)).toEqual(expected);
});
it.each([
[new InvalidType('strings', 'array'), 2, undefined],
[new Missing('strings'), [undefined], undefined],
[new Missing('w'), [undefined], { name: 'w' }],
[new Missing('strings'), [undefined], { required: true }],
[new InvalidType('strings', 'string'), [2], undefined],
[new InvalidType('w', 'string'), [2], { name: 'w' }, 'w'],
[new TooSmall('strings', 1), [''], { minLength: 1 }],
[new TooSmall('w', 2), [' a '], { name: 'w', minLength: 2 }],
[new TooBig('strings', 1), ['aa'], { maxLength: 1 }],
[new TooBig('w', 2), [' aaa '], { name: 'w', maxLength: 2 }],
[new Prohibited('strings', 'aa'), [' aa '], { trim: true, accepted: [' aa '] }],
[new Prohibited('w', 'aa'), [' aa '], { name: 'w', trim: true, accepted: [' aa '] }, 'w']
])('should reject [%p] when value is [%p] and options are [%p].', (error, given, options) => {
expect(() => commons.string.checkBulk(given, options)).toThrow(error);
});
});
describe('Check [resolve] utility', () => {
it.each([
[{}, undefined],
[{ name: 'toto' }, { name: 'toto' }]
])('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.string.resolve('value', given)).resolves.toEqual('called !');
expect(spyAsync).toHaveBeenCalledWith(commons.string.check, 'value', options);
jest.spyOn(commons.promise, 'async').mockRestore();
});
});
});