From e72a28081bea6a0ffcd0531e9a39804edd826d26 Mon Sep 17 00:00:00 2001 From: siffogh Date: Tue, 20 Mar 2018 16:50:40 +0100 Subject: [PATCH] chore(maintain): improve test coverage --- __mocks__/got.js | 5 ++--- test/TaskService.test.js | 26 ++++++++++++++++++++++++++ test/__internal/EngineService.test.js | 22 +++++++++++++++------- 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/__mocks__/got.js b/__mocks__/got.js index 5eb1517..41a7f58 100644 --- a/__mocks__/got.js +++ b/__mocks__/got.js @@ -1,6 +1,5 @@ -const handleRequest = (url, { error }) => { - const path = url.split('/').reverse()[0]; - switch (path) { +const handleRequest = (url, { errorType, error }) => { + switch (errorType) { case 'FAIL_WITHOUT_RESPONSE': case 'FAIL_WITH_RESPONSE': return Promise.reject(error); diff --git a/test/TaskService.test.js b/test/TaskService.test.js index da444e5..bfa5695 100644 --- a/test/TaskService.test.js +++ b/test/TaskService.test.js @@ -14,6 +14,20 @@ describe('TaskService', () => { taskService = new TaskService(new events(), engineService); }); + it('error(event, data) should emit error event with data: ', () => { + // given + const emitSpy = jest.spyOn(taskService.events, 'emit'); + const event = 'some_event'; + const expectedEvent = `${event}:error`; + const expectedData = 'some data'; + + // when + taskService.error(event, expectedData); + + // then + expect(emitSpy).toBeCalledWith(expectedEvent, expectedData); + }); + describe('sanitizeTask', () => { test('should return task with id when task id is provided', () => { expect(taskService.sanitizeTask('2')).toEqual({ id: '2' }); @@ -41,6 +55,18 @@ describe('TaskService', () => { expect(e).toEqual(new Error(MISSING_TASK)); } }); + + test('should call api complete with provided task', () => { + //given + const completeSpy = jest.spyOn(engineService, 'complete'); + const expectedTaskId = 'foo'; + + //when + taskService.complete(expectedTaskId); + + //then + expect(completeSpy).toBeCalledWith({ id: expectedTaskId }); + }); }); describe('handleFailure', () => { diff --git a/test/__internal/EngineService.test.js b/test/__internal/EngineService.test.js index 5d7289a..9e51a22 100644 --- a/test/__internal/EngineService.test.js +++ b/test/__internal/EngineService.test.js @@ -179,32 +179,40 @@ describe('EngineService', () => { it('should throw error if request fails without response', async() => { // given - const url = '/FAIL_WITHOUT_RESPONSE'; + const errorType = 'FAIL_WITHOUT_RESPONSE'; const error = 'some error message'; // then + let thrownError; + try { - await engineService.request('GET', url, { error }); + await engineService.request('GET', '', { errorType, error }); } catch (e) { - expect(e).toEqual(error); + thrownError = e; } + + expect(thrownError).toBe(error); }); it('should throw error response if request fails with response', async() => { // given - const url = '/FAIL_WITHOUT_RESPONSE'; + const errorType = 'FAIL_WITHOUT_RESPONSE'; const error = { response: { - body: { messsage: 'Some error message' } + body: { message: 'Some error message' } } }; // then + let thrownError; + try { - await engineService.request('GET', url, { error }); + await engineService.request('GET', '', { errorType, error }); } catch (e) { - expect(e).toEqual(error.response.body.message); + thrownError = e; } + + expect(thrownError).toBe(error.response.body.message); }); }); });