From c1eb3611b003898c57fb83a9af70709410af3c0b Mon Sep 17 00:00:00 2001 From: Sumedha Pramod Date: Fri, 7 Dec 2018 13:58:17 -0800 Subject: [PATCH] Fix: Tests --- src/api/FileVersionAPI.js | 19 ++++++++------- src/api/__tests__/FileVersionAPI-test.js | 31 +++++++++++++----------- 2 files changed, 27 insertions(+), 23 deletions(-) diff --git a/src/api/FileVersionAPI.js b/src/api/FileVersionAPI.js index adfcda9ab..0b06ab8d4 100644 --- a/src/api/FileVersionAPI.js +++ b/src/api/FileVersionAPI.js @@ -78,6 +78,15 @@ class FileVersionAPI extends API { * @return {void} */ successHandler = (data: Data, queryParams: Params): Promise => { + if (data.type === 'error' || !Array.isArray(data.entries)) { + const error = new Error(`Could not read annotations from file version with ID ${this.fileVersionId}`); + this.emit(ANNOTATOR_EVENT.error, { + reason: ERROR_TYPE.read, + error: error.toString() + }); + return Promise.reject(error); + } + const entries = this.data ? this.data.entries : []; this.data = { ...data, @@ -94,15 +103,7 @@ class FileVersionAPI extends API { return this.fetchFromMarker(params); } - if (data.type === 'error' || !Array.isArray(data.entries)) { - const error = new Error(`Could not read annotations from file version with ID ${this.fileVersionId}`); - this.emit(ANNOTATOR_EVENT.error, { - reason: ERROR_TYPE.read, - error: error.toString() - }); - } - - return Promise.resolve(); + return Promise.resolve(this.data); }; /** diff --git a/src/api/__tests__/FileVersionAPI-test.js b/src/api/__tests__/FileVersionAPI-test.js index 2100d82d6..931c05685 100644 --- a/src/api/__tests__/FileVersionAPI-test.js +++ b/src/api/__tests__/FileVersionAPI-test.js @@ -55,34 +55,37 @@ describe('api/FileVersionAPI', () => { describe('successHandler()', () => { beforeEach(() => { - api.fetchFromMarker = jest.fn(); + api.fetchFromMarker = jest.fn().mockReturnValue(Promise.resolve()); api.emit = jest.fn(); }); it('should call fetch the remaining annotations if the version has more to fetch', () => { api.data = { entries: [{}] }; - api.successHandler({ entries: [{}, {}], next_marker: 'marker', limit: 1 }); - expect(api.data.entries.length).toEqual(3); - expect(api.fetchFromMarker).toBeCalled(); + api.successHandler({ entries: [{}, {}], next_marker: 'marker', limit: 1 }).then(() => { + expect(api.data.entries.length).toEqual(3); + expect(api.fetchFromMarker).toBeCalled(); + }); }); it('should not call fetch if no more annotations need to be fetched', () => { api.data = { entries: [{}] }; - api.successHandler({ entries: [{}, {}], limit: 1 }); - expect(api.data.entries.length).toEqual(3); - expect(api.fetchFromMarker).not.toBeCalled(); + api.successHandler({ entries: [{}, {}], limit: 1 }).then(() => { + expect(api.data.entries.length).toEqual(3); + expect(api.fetchFromMarker).not.toBeCalled(); + }); }); it('should emit an error if the success response is of type error', () => { api.fileVersionId = 123; - api.successHandler({ type: 'error' }); - const error = new Error(`Could not read annotations from file version with ID ${api.fileVersionId}`); + api.successHandler({ type: 'error' }).catch(() => { + const error = new Error(`Could not read annotations from file version with ID ${api.fileVersionId}`); - expect(api.data.entries.length).toEqual(1); - expect(api.fetchFromMarker).not.toBeCalled(); - expect(api.emit).toBeCalledWith(ANNOTATOR_EVENT.error, { - reason: ERROR_TYPE.read, - error: error.toString() + expect(api.data.entries.length).toEqual(1); + expect(api.fetchFromMarker).not.toBeCalled(); + expect(api.emit).toBeCalledWith(ANNOTATOR_EVENT.error, { + reason: ERROR_TYPE.read, + error: error.toString() + }); }); }); });