UNPKG

oa-jira

Version:

Octet Agile's JIRA connectivity project.

174 lines (141 loc) 7.38 kB
const AxiosMock = require('../../../mocks/jira/axios.mock'); const Sprint = require('../../../../src/model/classes/sprint.class'); const model = require('../../../../src/model'); const oaJira = require('../../../../'); const Connection = require('../../../../src/connection/classes/connection.class'); const Cache = require('../../../../src/cache/classes/cache.class'); describe('Check [Sprints] services', () => { const connection = new Connection({ mode: 'Bearer', baseUrl: 'localhost', token: 'TOKEN' }); const cache = new Cache(); beforeEach(() => cache.reset()); describe('Check [create] service.', () => { it('should reject error when given nothing.', () => { expect(model.sprints.create()).rejects.toThrow(); }); it('should delegate to class static utility.', async () => { const spy = jest.spyOn(Sprint, 'create').mockResolvedValue('OK'); expect(model.sprints.create({ name: 'category' })).resolves.toEqual('OK'); expect(spy).toHaveBeenCalledWith({ name: 'category' }); jest.spyOn(Sprint, 'create').mockRestore(); }); }); describe('Check [resolve] service.', () => { it('should delegate to class static utility.', async () => { const spy = jest.spyOn(Sprint, 'resolve').mockResolvedValue('OK'); expect(model.sprints.resolve('sprint')).resolves.toEqual('OK'); expect(spy).toHaveBeenCalledWith('sprint'); jest.spyOn(Sprint, 'resolve').mockRestore(); }); }); describe('Check [get by id] service', () => { const mock = AxiosMock.init(); afterEach(() => mock.reset()); it('should reject when given nothing', async () => { expect(oaJira.sprints.getById()).rejects.toThrow('A [connection] is mandatory and cannot be falsy.'); }); it('should resolve a sprint class from jira when no cache given', async () => { const sprint = await oaJira.sprints.getById({ connection, id: 14 }); expect(sprint).toBeInstanceOf(Sprint); expect(sprint.getId()).toEqual(14); expect(mock.getSpy()).toHaveBeenCalled(); }); it('should resolve a sprint class from jira when cache is given', async () => { const sprint1 = await model.sprints.getById({ connection, id: 16, cache }); expect(cache.sprints.has(16)).toBeTruthy(); expect(sprint1).toBeInstanceOf(Sprint); expect(sprint1.getId()).toEqual(16); expect(mock.getSpy()).toHaveBeenCalledTimes(1); const sprint2 = await oaJira.sprints.getById({ connection, id: 16, cache }); expect(sprint2).toBeInstanceOf(Sprint); expect(sprint2.getId()).toEqual(16); expect(mock.getSpy()).toHaveBeenCalledTimes(1); }); it('should resolve null when not found', () => { expect(oaJira.sprints.getById({ connection, id: 404, cache })).resolves.toBeNull(); }); it('should reject when fail to retrieve sprint from jira', async () => { await expect(oaJira.sprints.getById({ connection, id: 500 })).rejects.toThrow( 'Fail to [Get [/rest/agile/1.0/sprint/500] (Status 500)]' ); }); }); describe('Check [get by ids] service', () => { const mock = AxiosMock.init(); afterEach(() => mock.reset()); it('should reject when given nothing', async () => { expect(oaJira.sprints.getByIds()).rejects.toThrow('A [connection] is mandatory and cannot be falsy.'); }); it('should resolve a sprint class from jira when no cache given', async () => { const sprints = await oaJira.sprints.getByIds({ connection, ids: [14] }); expect(Array.isArray(sprints)).toBeTruthy(); expect(sprints[0]).toBeInstanceOf(Sprint); expect(sprints[0].getId()).toEqual(14); expect(mock.getSpy()).toHaveBeenCalled(); }); it('should resolve a sprint class from jira when cache is given', async () => { await oaJira.sprints.getById({ connection, id: 14, cache }); expect(mock.getSpy()).toHaveBeenCalledTimes(1); const sprints = await oaJira.sprints.getByIds({ connection, ids: [14], cache }); expect(Array.isArray(sprints)).toBeTruthy(); expect(sprints[0]).toBeInstanceOf(Sprint); expect(sprints[0].getId()).toEqual(14); expect(mock.getSpy()).toHaveBeenCalledTimes(1); }); it('should resolve a sprint class from jira when cache is given', async () => { await oaJira.sprints.getById({ connection, id: 14, cache }); expect(mock.getSpy()).toHaveBeenCalledTimes(1); const sprints = await oaJira.sprints.getByIds({ connection, ids: [14, 404, 15], cache }); expect(sprints).toHaveLength(2); expect(sprints[0].getId()).toEqual(14); expect(sprints[1].getId()).toEqual(15); expect(mock.getSpy()).toHaveBeenCalledTimes(3); }); it('should reject when fail to retrieve sprint from jira', () => { expect(oaJira.sprints.getByIds({ connection, ids: [14, 500, 15] })).rejects.toThrow( 'Fail to [Get [/rest/agile/1.0/sprint/500] (Status 500)]' ); }); }); describe('Check [get Last by ids] service', () => { const mock = AxiosMock.init(); afterEach(() => mock.reset()); it('should reject when given nothing', async () => { expect(oaJira.sprints.getLastByIds()).rejects.toThrow('A [connection] is mandatory and cannot be falsy.'); }); it('should resolve a null from jira when no sprint found', async () => { const sprint = await oaJira.sprints.getLastByIds({ connection, ids: [404] }); expect(sprint).toBeNull(); }); it('should resolve a sprint from jira when no cache given', async () => { const sprint = await oaJira.sprints.getLastByIds({ connection, ids: [14] }); expect(sprint).toBeInstanceOf(Sprint); expect(sprint.getId()).toEqual(14); expect(mock.getSpy()).toHaveBeenCalled(); }); it('should resolve a sprint from jira when cache is given', async () => { await oaJira.sprints.getById({ connection, id: 14, cache }); expect(mock.getSpy()).toHaveBeenCalledTimes(1); const sprint = await oaJira.sprints.getLastByIds({ connection, ids: [14], cache }); expect(sprint).toBeInstanceOf(Sprint); expect(sprint.getId()).toEqual(14); expect(mock.getSpy()).toHaveBeenCalledTimes(1); }); it('should resolve the last sprint from jira when cache is given', async () => { await oaJira.sprints.getById({ connection, id: 14, cache }); expect(mock.getSpy()).toHaveBeenCalledTimes(1); const sprint = await oaJira.sprints.getLastByIds({ connection, ids: [14, 404, 15], cache }); expect(sprint).toBeInstanceOf(Sprint); expect(sprint.getId()).toEqual(15); expect(mock.getSpy()).toHaveBeenCalledTimes(3); const sprint2 = await oaJira.sprints.getLastByIds({ connection, ids: [14, 2, 404, 15], cache }); expect(sprint2).toBeInstanceOf(Sprint); expect(sprint2.getId()).toEqual(2); expect(mock.getSpy()).toHaveBeenCalledTimes(4); }); it('should reject when fail to retrieve sprint from jira', async () => { await expect(oaJira.sprints.getLastByIds({ connection, ids: [14, 500, 15] })).rejects.toThrow( 'Fail to [Get [/rest/agile/1.0/sprint/500] (Status 500)]' ); }); }); });