UNPKG

oa-jira

Version:

Octet Agile's JIRA connectivity project.

82 lines (67 loc) 3.49 kB
const AxiosMock = require('../../../mocks/jira/axios.mock'); const Project = require('../../../../src/model/classes/project.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 [Projects] 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.projects.create()).rejects.toThrow(); }); it('should delegate to class static utility.', async () => { const spy = jest.spyOn(Project, 'create').mockResolvedValue('OK'); expect(model.projects.create({ name: 'type' })).resolves.toEqual('OK'); expect(spy).toHaveBeenCalledWith({ name: 'type' }); jest.spyOn(Project, 'create').mockRestore(); }); }); describe('Check [resolve] service.', () => { it('should delegate to class static utility.', async () => { const spy = jest.spyOn(Project, 'resolve').mockResolvedValue('OK'); expect(model.projects.resolve('type')).resolves.toEqual('OK'); expect(spy).toHaveBeenCalledWith('type'); jest.spyOn(Project, 'resolve').mockRestore(); }); }); describe('Check [get by id] service', () => { const mock = AxiosMock.init(); afterEach(() => mock.reset()); it('should reject when given nothing', async () => { expect(oaJira.projects.getById()).rejects.toThrow('A [connection] is mandatory and cannot be falsy.'); }); it('should resolve a project class from jira when no cache given', async () => { const project = await oaJira.projects.getById({ connection, id: '10000' }); expect(project).toBeInstanceOf(Project); expect(project.getId()).toEqual('10000'); expect(mock.getSpy()).toHaveBeenCalledTimes(1); }); it('should resolve a project class from jira when nominal', async () => { const project1 = await oaJira.projects.getById({ connection, id: '10000', cache }); expect(project1).toBeInstanceOf(Project); expect(project1.getId()).toEqual('10000'); expect(cache.projects.has('10000')).toBeTruthy(); expect(mock.getSpy()).toHaveBeenCalledTimes(1); const project2 = await oaJira.projects.getById({ connection, id: '10666', cache }); expect(project2).toBeInstanceOf(Project); expect(project2.getId()).toEqual('10666'); expect(cache.projects.has('10666')).toBeTruthy(); expect(mock.getSpy()).toHaveBeenCalledTimes(2); const project3 = await oaJira.projects.getById({ connection, id: '10000', cache }); expect(project3).toBeInstanceOf(Project); expect(project3.getId()).toEqual('10000'); expect(mock.getSpy()).toHaveBeenCalledTimes(2); }); it('should resolve null when not found', () => { expect(oaJira.projects.getById({ connection, id: '404', cache })).resolves.toBeNull(); }); it('should reject when fail to retrieve project from jira', () => { expect(oaJira.projects.getById({ connection, id: '500' })).rejects.toThrow( 'Fail to [Get [/rest/api/3/project/500] (Status 500)]' ); }); }); });