Skip to content

Commit

Permalink
Fix: Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pramodsum committed Dec 7, 2018
1 parent 639fcef commit c1eb361
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
19 changes: 10 additions & 9 deletions src/api/FileVersionAPI.js
Expand Up @@ -78,6 +78,15 @@ class FileVersionAPI extends API {
* @return {void}
*/
successHandler = (data: Data, queryParams: Params): Promise<any> => {
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,
Expand All @@ -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);
};

/**
Expand Down
31 changes: 17 additions & 14 deletions src/api/__tests__/FileVersionAPI-test.js
Expand Up @@ -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()
});
});
});
});
Expand Down

0 comments on commit c1eb361

Please sign in to comment.