UNPKG

oa-jira

Version:

Octet Agile's JIRA connectivity project.

202 lines (167 loc) 8.35 kB
const Version = require('../../../../src/model/classes/version.class'); describe('Check [Version] class.', () => { const minimal = { id: '10004', name: 'v1' }; const maximal = { id: ' 10003 ', name: ' v0.1.0 ', archived: true, released: true, releaseDate: new Date('2022-07-13'), startDate: new Date('2022-04-04'), projectId: 10000 }; const minimalToJSON = { id: '10004', name: 'v1', archived: false, released: false }; const maximalToJSON = { id: '10003', name: 'v0.1.0', archived: true, released: true, releaseDate: new Date('2022-07-13').toISOString(), startDate: new Date('2022-04-04').toISOString(), projectId: 10000 }; const cases = { nominal: [[minimal], [maximal]], 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 [start date] MUST implements [Date].', { id: '10000', name: 'a-name', startDate: 42 }], ['A [release date] MUST implements [Date].', { id: '10000', name: 'a-name', releaseDate: 'date' }], ['A [archived] MUST be a [boolean].', { id: '10000', name: 'a-name', archived: 'true' }], ['A [released] MUST be a [boolean].', { id: '10000', name: 'a-name', released: 'kaboom!' }], ['A [project id] MUST be a [integer].', { id: '10000', name: 'a-name', projectId: 'kaboom!' }] ] }; describe('Check constructor.', () => { it('should throw error when given nothing.', () => { expect(() => new Version()).toThrow(); }); it.each(cases.nominal)('should resolve a new instance when init with [%p]', given => { expect(new Version(given)).toBeInstanceOf(Version); }); it.each(cases.invalid)('should reject [%p] when create from [%p]', (error, given) => { expect(() => new Version(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 Version(given).getId()).toBe(given.id.trim()); }); }); describe('Check [name] getter.', () => { it.each(cases.nominal)('should return the given trimmed name when init with [%p]', given => { expect(new Version(given).getName()).toBe(given.name.trim()); }); }); describe('Check [start date] getter.', () => { it.each(cases.nominal)('should return the given start date when init with [%p]', given => { expect(new Version(given).getStartDate()).toBe(given.startDate ? given.startDate : null); }); }); describe('Check [release date] getter.', () => { it.each(cases.nominal)('should return the given release date when init with [%p]', given => { expect(new Version(given).getReleaseDate()).toBe(given.releaseDate ? given.releaseDate : null); }); }); describe('Check [is archived] getter.', () => { it.each(cases.nominal)('should return the given archived flag when init with [%p]', given => { expect(new Version(given).getArchived()).toEqual(Object.hasOwn(given, 'archived') ? !!given.archived : null); }); }); describe('Check [is released] getter.', () => { it.each(cases.nominal)('should return the given released flag when init with [%p]', given => { expect(new Version(given).getReleased()).toEqual(Object.hasOwn(given, 'released') ? !!given.released : null); }); }); describe('Check [project id] getter.', () => { it.each(cases.nominal)('should return the given board id when init with [%p]', given => { expect(new Version(given).getProjectId()).toBe(given.projectId ? given.projectId : null); }); }); }); describe('Check [to string] utility.', () => { it.each(cases.nominal)('should return expected string representation when init with [%p]', given => { expect(`${new Version(given)}`).toEqual(`Version[${given.name.trim()} #${given.id.trim()}]`); }); }); describe('Check [to JSON] utility.', () => { it('should return expected JSON representation when minimal.', () => { expect(new Version(minimal).toJSON()).toEqual(minimalToJSON); }); it('should return expected JSON representation when maximal.', () => { expect(new Version(maximal).toJSON()).toEqual(maximalToJSON); }); }); describe('Check static utilities.', () => { describe('Check [create] static utility.', () => { it('should reject error when given nothing.', () => { expect(Version.create()).rejects.toThrow(); }); it.each(cases.invalid)('should reject [%p] when create from [%p]', (expected, given) => { expect(Version.create(given)).rejects.toThrow(expected); }); it.each(cases.nominal)('should resolve new instance when given is [%p].', async given => { expect((await Version.create(given)).toJSON()).toEqual(new Version(given).toJSON()); }); }); describe('Check [check] static utility.', () => { it('should throw when given is falsy.', () => { expect(() => Version.check()).toThrow('A [version] is mandatory and cannot be falsy.'); }); it('should throw when given does not implements expected class.', () => { expect(() => Version.check(2)).toThrow('A [version] MUST implements [Version].'); }); it.each(cases.nominal)('should return given when given is initialised with [%p].', given => { const instance = new Version(given); expect(Version.check(instance)).toBe(instance); }); }); describe('Check [resolve] static utility.', () => { it('should reject when given is falsy.', () => { expect(Version.resolve()).rejects.toThrow('A [version] is mandatory and cannot be falsy.'); }); it('should reject when given does not implements expected class.', () => { expect(Version.resolve(2)).rejects.toThrow('A [version] MUST implements [Version].'); }); it.each(cases.nominal)('should resolve given when given is initialised with [%p].', given => { const instance = new Version(given); expect(Version.resolve(instance)).resolves.toBe(instance); }); }); describe('Check [resolve bulk] static utility.', () => { it('should reject when given is missing.', () => { expect(Version.resolveBulk()).rejects.toThrow('A [bulk of versions] MUST be a [array].'); }); it('should reject when given is not an array.', () => { expect(Version.resolveBulk('Kaboom !')).rejects.toThrow('A [bulk of versions] MUST be a [array].'); }); it('should reject when one version is invalid.', () => { expect(Version.resolveBulk([2])).rejects.toThrow('A [version] MUST implements [Version].'); }); it('should resolve given when given is valid.', () => { const validBulk = [new Version(minimal), new Version(maximal)]; expect(Version.resolveBulk(validBulk)).resolves.toEqual(validBulk); }); }); describe('Check [check bulk] static utility.', () => { it('should throw when given is missing.', () => { expect(() => Version.checkBulk()).toThrow('A [bulk of versions] MUST be a [array].'); }); it('should throw when given is not an array.', () => { expect(() => Version.checkBulk('Kaboom !')).toThrow('A [bulk of versions] MUST be a [array].'); }); it('should throw when one version is invalid.', () => { expect(() => Version.checkBulk([2])).toThrow('A [version] MUST implements [Version].'); }); it('should resolve given when given is valid.', () => { const validBulk = [new Version(minimal), new Version(maximal)]; expect(Version.checkBulk(validBulk)).toBe(validBulk); }); }); }); });