UNPKG

oa-jira

Version:

Octet Agile's JIRA connectivity project.

126 lines (106 loc) 5.23 kB
const Category = require('../../../../src/model/classes/category.class'); describe('Check [Category] class.', () => { const cases = { nominal: [ [{ id: 1, key: 'min-key', name: 'min-name' }], [{ id: 2, key: 'max-key', name: 'max-name', colorName: 'max-color-name' }], [{ id: 3, key: ' untrimmed-key ', name: ' untrimmed-name ', colorName: ' untrimmed-color-name ' }] ], invalid: [ ['A [id] is mandatory and cannot be falsy.', {}], ['A [id] MUST be a [integer].', { id: '1' }], ['The [id] CANNOT be smaller than 0.', { id: -1 }], ['A [key] is mandatory and cannot be falsy.', { id: 1 }], ['A [key] MUST be a [string].', { id: 1, key: 2 }], ['A [name] is mandatory and cannot be falsy.', { id: 1, key: 'key' }], ['A [name] MUST be a [string].', { id: 1, key: 'key', name: 2 }], ['A [color name] MUST be a [string].', { id: 1, key: 'key', name: 'name', colorName: 2 }] ] }; describe('Check constructor.', () => { it('should reject error when given nothing.', () => { expect(() => new Category()).toThrow(); }); it.each(cases.nominal)('should resolve a new instance when init with [%p]', given => { expect(new Category(given)).toBeInstanceOf(Category); }); it.each(cases.invalid)('should reject [%p] when create from [%p]', (error, given) => { expect(() => new Category(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 Category(given).getId()).toBe(given.id); }); }); describe('Check [key] getter.', () => { it.each(cases.nominal)('should return the given trimmed key when init with [%p]', given => { expect(new Category(given).getKey()).toBe(given.key.trim()); }); }); describe('Check [name] getter.', () => { it.each(cases.nominal)('should return the given trimmed name when init with [%p]', given => { expect(new Category(given).getName()).toBe(given.name.trim()); }); }); describe('Check [color name] getter.', () => { it.each(cases.nominal)('should return the given trimmed color name when init with [%p]', given => { expect(new Category(given).getColorName()).toBe(given.colorName ? given.colorName.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 Category(given)}`).toEqual(`Category[${given.name.trim()} #${given.id}]`); }); }); describe('Check [to JSON] utility.', () => { it.each(cases.nominal)('should return expected JSON representation when init with [%p]', given => { expect(new Category(given).toJSON()).toEqual( given.colorName ? { id: given.id, name: given.name.trim(), key: given.key.trim(), colorName: given.colorName.trim() } : { id: given.id, name: given.name.trim(), key: given.key.trim() } ); }); }); }); describe('Check static utilities.', () => { describe('Check [create] static utility.', () => { it('should reject error when given nothing.', () => { expect(Category.create()).rejects.toThrow(); }); it.each(cases.invalid)('should reject [%p] when create from [%p]', (expected, given) => { expect(Category.create(given)).rejects.toThrow(expected); }); it.each(cases.nominal)('should resolve new instance when given is [%p].', async given => { expect((await Category.create(given)).toJSON()).toEqual(new Category(given).toJSON()); }); }); describe('Check [check] static utility.', () => { it('should throw when given is falsy.', () => { expect(() => Category.check()).toThrow('A [category] is mandatory and cannot be falsy.'); }); it('should reject when given does not implements expected class.', () => { expect(() => Category.check(2)).toThrow('A [category] MUST implements [Category].'); }); it.each(cases.nominal)('should resolve given when given is initialised with [%p].', given => { const instance = new Category(given); expect(Category.check(instance)).toBe(instance); }); }); describe('Check [resolve] static utility.', () => { it('should reject when given is falsy.', () => { expect(Category.resolve()).rejects.toThrow('A [category] is mandatory and cannot be falsy.'); }); it('should reject when given does not implements expected class.', () => { expect(Category.resolve(2)).rejects.toThrow('A [category] MUST implements [Category].'); }); it.each(cases.nominal)('should resolve given when given is initialised with [%p].', given => { const instance = new Category(given); expect(Category.resolve(instance)).resolves.toBe(instance); }); }); }); });