oa-jira
Version:
Octet Agile's JIRA connectivity project.
120 lines (99 loc) • 4.99 kB
JavaScript
const Resolution = require('../../../../src/model/classes/resolution.class');
describe('Check [Resolution] class.', () => {
const maximal = { id: ' 1 ', name: ' n ', description: ' d ' };
const minimal = { id: '1', name: 'n' };
const maximalToJSON = { id: '1', name: 'n', description: 'd' };
const minimalToJSON = { id: '1', name: 'n' };
const cases = {
nominal: [[maximal], [minimal]],
invalid: [
['A [id] is mandatory and cannot be falsy.', { name: 'a-name' }],
['A [id] MUST be a [string].', { id: 1, name: 'a-name' }],
['The [id] CANNOT be smaller than 1.', { id: ' ', name: 'a-name' }],
['A [name] is mandatory and cannot be falsy.', { id: '10000' }],
['A [name] MUST be a [string].', { id: '10000', name: 2 }],
['The [name] CANNOT be smaller than 1.', { id: '10000', name: ' ' }],
['A [description] MUST be a [string].', { id: '10000', name: 'a-name', description: 42 }]
]
};
describe('Check constructor.', () => {
it('should reject error when given nothing.', () => {
expect(() => new Resolution()).toThrow();
});
it.each(cases.nominal)('should resolve a new instance when init with [%p]', given => {
expect(new Resolution(given)).toBeInstanceOf(Resolution);
});
it.each(cases.invalid)('should reject [%p] when create from [%p]', (error, given) => {
expect(() => new Resolution(given)).toThrow(error);
});
});
describe('Check getters.', () => {
describe('Check [id] getter.', () => {
it.each(cases.nominal)('should return the given id when init with [%p]', given => {
expect(new Resolution(given).getId()).toEqual(given.id.trim());
});
});
describe('Check [name] getter.', () => {
it.each(cases.nominal)('should return the given trimmed name when init with [%p]', given => {
expect(new Resolution(given).getName()).toEqual(given.name.trim());
});
});
describe('Check [description] getter.', () => {
it.each(cases.nominal)('should return the given trimmed description when init with [%p]', given => {
expect(new Resolution(given).getDescription()).toEqual(given.description ? given.description.trim() : null);
});
});
});
describe('Check utilities.', () => {
describe('Check [to string] utility.', () => {
it.each(cases.nominal)('should return expected string representation when init with [%p]', given => {
expect(`${new Resolution(given)}`).toEqual(`Resolution[${given.name.trim()} #${given.id.trim()}]`);
});
});
describe('Check [to JSON] utility.', () => {
it('should return expected JSON representation when minimal', () => {
expect(new Resolution(minimal).toJSON()).toEqual(minimalToJSON);
});
it('should return expected JSON representation when maximal', () => {
expect(new Resolution(maximal).toJSON()).toEqual(maximalToJSON);
});
});
});
describe('Check static utilities.', () => {
describe('Check [create] static utility.', () => {
it('should reject error when given nothing.', () => {
expect(Resolution.create()).rejects.toThrow();
});
it.each(cases.invalid)('should reject [%p] when create from [%p]', (expected, given) => {
expect(Resolution.create(given)).rejects.toThrow(expected);
});
it.each(cases.nominal)('should resolve new instance when given is [%p].', async given => {
expect((await Resolution.create(given)).toJSON()).toEqual(new Resolution(given).toJSON());
});
});
describe('Check [check] static utility.', () => {
it('should throw when given is falsy.', () => {
expect(() => Resolution.check()).toThrow('A [resolution] is mandatory and cannot be falsy.');
});
it('should throw when given does not implements expected class.', () => {
expect(() => Resolution.check(2)).toThrow('A [resolution] MUST implements [Resolution].');
});
it.each(cases.nominal)('should return given when given is initialised with [%p].', given => {
const instance = new Resolution(given);
expect(Resolution.check(instance)).toBe(instance);
});
});
describe('Check [resolve] static utility.', () => {
it('should reject when given is falsy.', () => {
expect(Resolution.resolve()).rejects.toThrow('A [resolution] is mandatory and cannot be falsy.');
});
it('should reject when given does not implements expected class.', () => {
expect(Resolution.resolve(2)).rejects.toThrow('A [resolution] MUST implements [Resolution].');
});
it.each(cases.nominal)('should resolve given when given is initialised with [%p].', given => {
const instance = new Resolution(given);
expect(Resolution.resolve(instance)).resolves.toBe(instance);
});
});
});
});