Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Don't render until AFTER all are fetched #311

Merged
merged 3 commits into from Dec 10, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 14 additions & 11 deletions src/api/FileVersionAPI.js
Expand Up @@ -46,7 +46,7 @@ class FileVersionAPI extends API {
*/
fetchVersionAnnotations(version: string): Promise<AnnotationMap> {
this.fileVersionId = version;

// $FlowFixMe
return this.fetchFromMarker({ version, fields: FIELDS }).then(this.createAnnotationMap);
}
Expand Down Expand Up @@ -77,11 +77,20 @@ class FileVersionAPI extends API {
* @param {Params} queryParams - Key-value map of querystring params
* @return {void}
*/
successHandler = (data: Data, queryParams: Params): void => {
successHandler = (data: Data, queryParams: Params): Promise<any> => {
if (data.type === 'error' || !Array.isArray(data.entries)) {
pramodsum marked this conversation as resolved.
Show resolved Hide resolved
const error = new Error(`Could not read annotations from file version with ID ${this.fileVersionId}`);
this.emit(ANNOTATOR_EVENT.error, {
pramodsum marked this conversation as resolved.
Show resolved Hide resolved
reason: ERROR_TYPE.read,
error: error.toString()
});
return Promise.reject(error);
}

const entries = this.data ? this.data.entries : [];
this.data = {
...data,
entries: entries.concat(data.entries)
entries: [...entries, ...data.entries]
};

const { next_marker } = data;
Expand All @@ -91,16 +100,10 @@ class FileVersionAPI extends API {
marker: next_marker
};

this.fetchFromMarker(params);
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(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