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