UNPKG

oa-jira

Version:

Octet Agile's JIRA connectivity project.

133 lines (112 loc) 5.65 kB
const Type = require('../../../../src/model/classes/type.class'); describe('Check [Type] class.', () => { const maximal = { id: ' 1 ', name: ' n ', description: ' d ', iconUrl: ' i ', subtask: false, hierarchyLevel: 0 }; const minimal = { id: '1', name: 'n' }; const maximalToJSON = { id: '1', name: 'n', description: 'd', iconUrl: 'i', subtask: false, hierarchyLevel: 0 }; const minimalToJSON = { id: '1', name: 'n', subtask: false, hierarchyLevel: null }; const cases = { nominal: [ [maximal], [{ id: '1', name: 'n', description: 'd', iconUrl: 'i', subtask: true, hierarchyLevel: 2 }], [{ id: ' 1 ', name: ' n ' }], [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 }], ['A [icon Url] MUST be a [string].', { id: '10000', name: 'a-name', iconUrl: 2 }], ['A [subtask] MUST be a [boolean].', { id: '10000', name: 'a-name', subtask: 'true' }], ['A [hierarchyLevel] MUST be a [integer].', { id: '10000', name: 'a-name', hierarchyLevel: '2' }] ] }; describe('Check constructor.', () => { it('should reject error when given nothing.', () => { expect(() => new Type()).toThrow(); }); it.each(cases.nominal)('should resolve a new instance when init with [%p]', given => { expect(new Type(given)).toBeInstanceOf(Type); }); it.each(cases.invalid)('should reject [%p] when create from [%p]', (error, given) => { expect(() => new Type(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 Type(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 Type(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 Type(given).getDescription()).toEqual(given.description ? given.description.trim() : null); }); }); describe('Check [icon Url] getter.', () => { it.each(cases.nominal)('should return the given trimmed icon url when init with [%p]', given => { expect(new Type(given).getIconUrl()).toEqual(given.iconUrl ? given.iconUrl.trim() : null); }); }); describe('Check [is subtask] getter.', () => { it.each(cases.nominal)('should return the given subtask flag when init with [%p]', given => { expect(new Type(given).isSubTask()).toEqual(Object.hasOwn(given, 'subtask') ? !!given.subtask : null); }); }); describe('Check [hierarchy level] getter.', () => { it.each(cases.nominal)('should return the given hierarchy level when init with [%p]', given => { expect(new Type(given).getHierarchyLevel()).toEqual( given.hierarchyLevel === undefined ? null : given.hierarchyLevel ); }); }); }); describe('Check utilities.', () => { describe('Check [to string] utility.', () => { it.each(cases.nominal)('should return expected string representation when init with [%p]', given => { expect(`${new Type(given)}`).toEqual(`Type[${given.name.trim()} #${given.id.trim()}]`); }); }); describe('Check [to JSON] utility.', () => { it('should return expected JSON representation when minimal', () => { expect(new Type(minimal).toJSON()).toEqual(minimalToJSON); }); it('should return expected JSON representation when maximal', () => { expect(new Type(maximal).toJSON()).toEqual(maximalToJSON); }); }); }); describe('Check static utilities.', () => { describe('Check [create] static utility.', () => { it('should reject error when given nothing.', () => { expect(Type.create()).rejects.toThrow(); }); it.each(cases.invalid)('should reject [%p] when create from [%p]', (expected, given) => { expect(Type.create(given)).rejects.toThrow(expected); }); it.each(cases.nominal)('should resolve new instance when given is [%p].', async given => { expect((await Type.create(given)).toJSON()).toEqual(new Type(given).toJSON()); }); }); describe('Check [check] static utility.', () => { it('should throw when given is falsy.', () => { expect(() => Type.check()).toThrow('A [type] is mandatory and cannot be falsy.'); }); it('should throw when given does not implements expected class.', () => { expect(() => Type.check(2)).toThrow('A [type] MUST implements [Type].'); }); it.each(cases.nominal)('should return given when given is initialised with [%p].', given => { const instance = new Type(given); expect(Type.check(instance)).toBe(instance); }); }); }); });