UNPKG

oa-jira

Version:

Octet Agile's JIRA connectivity project.

196 lines (164 loc) 8.42 kB
const AxiosMock = require('../../../mocks/jira/axios.mock'); const model = require('../../../../src/model'); const oaJira = require('../../../../'); const Issue = require('../../../../src/model/classes/issue.class'); const oaConstants = require('../../../mocks/jira/constants/oa.constants'); const Connection = require('../../../../src/connection/classes/connection.class'); const Cache = require('../../../../src/cache/classes/cache.class'); const IssueSnapshot = require('../../../../src/model/classes/issue.snapshot.class'); describe('Check [Issues] services', () => { const connection = new Connection({ mode: 'Bearer', baseUrl: 'localhost', token: 'TOKEN' }); const mock = AxiosMock.init(); const cache = new Cache(); beforeEach(() => { mock.reset(); cache.reset(); }); describe('Check [create] service.', () => { it('should reject error when given nothing.', () => { expect(model.issues.create()).rejects.toThrow(); }); it('should delegate to class static utility.', () => { const spy = jest.spyOn(Issue, 'create').mockResolvedValue('OK'); expect(model.issues.create({ key: 'issue' })).resolves.toEqual('OK'); expect(spy).toHaveBeenCalledWith({ key: 'issue' }); jest.spyOn(Issue, 'create').mockRestore(); }); }); describe('Check [resolve] service.', () => { it('should delegate to class static utility.', async () => { const spy = jest.spyOn(Issue, 'resolve').mockResolvedValue('OK'); expect(model.issues.resolve('issue')).resolves.toEqual('OK'); expect(spy).toHaveBeenCalledWith('issue'); await jest.spyOn(Issue, 'resolve').mockRestore(); }); }); describe('Check [get by key] service', () => { it('should reject when given nothing', async () => { expect(oaJira.issues.getByKey()).rejects.toThrow('A [connection] is mandatory and cannot be falsy.'); }); it('should resolve an issue from jira when minimal and cache given', async () => { const issue = await oaJira.issues.getByKey({ connection, key: 'KEY-000', cache }); expect(issue).toBeInstanceOf(Issue); expect(issue.toJSON()).toEqual(oaConstants.issues['KEY-000']); expect(mock.getSpy()).toHaveBeenCalledTimes(1); }); it('should resolve an issue from jira no cache is given', async () => { const issue = await oaJira.issues.getByKey({ connection, key: 'KEY-001' }); expect(issue).toBeInstanceOf(Issue); expect(issue.toJSON()).toEqual(oaConstants.issues['KEY-001']); expect(issue.getFirst()).toBeInstanceOf(IssueSnapshot); expect(issue.getFirst().toJSON()).toEqual( oaConstants.issues['KEY-001'].snapshots.snapshots['2022-06-01T10:01:01.001Z'] ); expect(issue.getLast().toJSON()).toEqual( oaConstants.issues['KEY-001'].snapshots.snapshots['2022-06-11T10:11:11.011Z'] ); expect(mock.getSpy()).toHaveBeenCalledTimes(7); }); it('should resolve an issue from jira when cache is given', async () => { const issue = await oaJira.issues.getByKey({ connection, key: 'KEY-001', cache }); expect(issue).toBeInstanceOf(Issue); expect(issue.getKey()).toEqual('KEY-001'); expect(mock.getSpy()).toHaveBeenCalledTimes(7); expect(issue.toJSON()).toEqual(oaConstants.issues['KEY-001']); const issue2 = await oaJira.issues.getByKey({ connection, key: 'KEY-001', cache }); expect(issue2).toBeInstanceOf(Issue); expect(issue2.toJSON()).toEqual(oaConstants.issues['KEY-001']); expect(issue2.getKey()).toEqual('KEY-001'); expect(mock.getSpy()).toHaveBeenCalledTimes(8); const issue3 = await oaJira.issues.getByKey({ connection, key: 'KEY-001', cache }); expect(issue3).toBeInstanceOf(Issue); expect(issue3.toJSON()).toEqual(oaConstants.issues['KEY-001']); expect(issue3.getKey()).toEqual('KEY-001'); expect(mock.getSpy()).toHaveBeenCalledTimes(9); }); it('should reject when fail to retrieve sprint', async () => { expect(oaJira.issues.getByKey({ connection, key: 'KEY-401' })).rejects.toThrow( 'Fail to [Get [/rest/agile/1.0/sprint/500] (Status 500)].' ); }); it('should reject when fail to retrieve status', async () => { expect(oaJira.issues.getByKey({ connection, key: 'KEY-402' })).rejects.toThrow( 'Fail to [Get [/rest/api/3/status/500] (Status 500)].' ); }); it('should reject when fail to retrieve status', async () => { expect(oaJira.issues.getByKey({ connection, key: 'KEY-403' })).rejects.toThrow( 'Fail to [Get [/rest/api/3/issuetype/500] (Status 500)].' ); }); it('should reject when fail to retrieve resolution', async () => { expect(oaJira.issues.getByKey({ connection, key: 'KEY-404' })).rejects.toThrow( 'Fail to [Get [/rest/api/3/resolution/500] (Status 500)].' ); }); it('should reject when fail to retrieve version', async () => { expect(oaJira.issues.getByKey({ connection, key: 'KEY-405' })).rejects.toThrow( 'Fail to [Get [/rest/api/3/version/500] (Status 500)].' ); }); it('should reject when summary field is invalid', async () => { expect(oaJira.issues.getByKey({ connection, key: 'KEY-406' })).rejects.toThrow('A [summary] MUST be a [string].'); }); it('should reject when sprint field is invalid', async () => { expect(oaJira.issues.getByKey({ connection, key: 'KEY-407' })).rejects.toThrow( 'A [id] is mandatory and cannot be falsy.' ); }); it('should reject when category of status field is invalid', async () => { expect(oaJira.issues.getByKey({ connection, key: 'KEY-408' })).rejects.toThrow('A [id] MUST be a [integer].'); }); it('should reject when status field is invalid', async () => { expect(oaJira.issues.getByKey({ connection, key: 'KEY-409' })).rejects.toThrow( 'A [id] is mandatory and cannot be falsy.' ); }); it('should reject when project field is invalid', async () => { expect(oaJira.issues.getByKey({ connection, key: 'KEY-410' })).rejects.toThrow( 'A [id] is mandatory and cannot be falsy.' ); }); it('should reject when resolution field is invalid', async () => { expect(oaJira.issues.getByKey({ connection, key: 'KEY-411' })).rejects.toThrow( 'A [id] is mandatory and cannot be falsy.' ); }); it('should reject when issue type field is invalid', async () => { expect(oaJira.issues.getByKey({ connection, key: 'KEY-412' })).rejects.toThrow( 'A [id] is mandatory and cannot be falsy.' ); }); it('should reject when version field is invalid', async () => { expect(oaJira.issues.getByKey({ connection, key: 'KEY-413' })).rejects.toThrow( 'A [id] is mandatory and cannot be falsy.' ); }); it('should resolve null when not found', async () => { expect(oaJira.issues.getByKey({ connection, key: '404', cache })).resolves.toBeNull(); }); it('should reject when fail to retrieve category from jira', () => { expect(oaJira.issues.getByKey({ connection, key: '500' })).rejects.toThrow( 'Fail to [Get [/rest/api/3/issue/500] (Status 500)]' ); }); }); describe('Check [get by board id] service', () => { it('should reject when given nothing', async () => { expect(oaJira.issues.getByBoardId()).rejects.toThrow('A [connection] is mandatory and cannot be falsy.'); }); it('should resolve empty array when no issues on board', async () => { const issues = await oaJira.issues.getByBoardId({ connection, boardId: 2, cache }); expect(issues).toHaveLength(0); expect(mock.getSpy()).toHaveBeenCalled(); }); it('should resolve an issue from jira when minimal and cache given', async () => { const issues = await oaJira.issues.getByBoardId({ connection, boardId: 1, cache, offset: 0, size: 2 }); expect(issues).toHaveLength(2); expect(mock.getSpy()).toHaveBeenCalled(); }); it('should reject when an issue is invalid', async () => { expect(oaJira.issues.getByBoardId({ connection, boardId: 3, cache })).rejects.toThrow(); }); }); });