From 063d0f93f08c3c4a23ebbcbc8542ce99eb7de05b Mon Sep 17 00:00:00 2001 From: Maxim Sukharev Date: Tue, 6 Mar 2018 12:28:30 +0100 Subject: [PATCH] Add tests for error path and fix bug Signed-off-by: Maxim Sukharev --- src/state/features.test.js | 26 ++++++++++++++++++++++++++ src/state/filePairs.js | 19 ++++++++++++------- src/state/filePairs.test.js | 22 ++++++++++++++++++++++ 3 files changed, 60 insertions(+), 7 deletions(-) diff --git a/src/state/features.test.js b/src/state/features.test.js index aac86ef..5a48e85 100644 --- a/src/state/features.test.js +++ b/src/state/features.test.js @@ -8,6 +8,7 @@ import reducer, { mostSimilar, leastSimilar, } from './features'; +import { ADD as ERROR_ADD } from './errors'; const mockStore = configureMockStore([thunk]); @@ -71,6 +72,31 @@ describe('features/actions', () => { ]); }); }); + + it('error', () => { + const blobIdA = 1; + const blobIdB = 2; + const store = mockStore({ + features: { + ...initialState, + }, + }); + + const errText = 'some error'; + fetch.mockReject(errText); + + return store.dispatch(load(blobIdA, blobIdB)).then(() => { + expect(store.getActions()).toEqual([ + { + type: LOAD, + }, + { + type: ERROR_ADD, + error: errText, + }, + ]); + }); + }); }); }); diff --git a/src/state/filePairs.js b/src/state/filePairs.js index 1b8c990..886c85c 100644 --- a/src/state/filePairs.js +++ b/src/state/filePairs.js @@ -120,12 +120,17 @@ export const selectPair = (expId, id) => dispatch => { export const loadAnnotations = (expId, id) => dispatch => { dispatch({ type: LOAD_ANNOTATIONS }); - return api.getFilePairAnnotations(expId, id).then(res => - dispatch({ - type: SET_ANNOTATIONS, - data: res, - }) - ); + return api + .getFilePairAnnotations(expId, id) + .then(res => + dispatch({ + type: SET_ANNOTATIONS, + data: res, + }) + ) + .catch(e => { + dispatch(addErrors(e)); + }); }; /* Selectors */ @@ -180,7 +185,7 @@ export const middleware = store => next => action => { .then(() => next(loadAnnotations(expIdParam, +payload.params.pair))) .then(() => { const pair = getCurrentFilePair(store.getState()); - return next(featuresLoad(pair.leftBlobId, pair.rightBlobId)); + return pair && next(featuresLoad(pair.leftBlobId, pair.rightBlobId)); }); default: return result; diff --git a/src/state/filePairs.test.js b/src/state/filePairs.test.js index a97932c..fe1d75e 100644 --- a/src/state/filePairs.test.js +++ b/src/state/filePairs.test.js @@ -162,6 +162,28 @@ describe('filePairs/actions', () => { ]); }); }); + + it('error', () => { + const store = mockStore({ + filePairs: { + ...initialState, + pairs: { 1: 'pair' }, + }, + }); + + const errText = 'some error'; + fetch.mockReject(errText); + + return store.dispatch(loadAnnotations(1, 1)).then(() => { + expect(store.getActions()).toEqual([ + { type: LOAD_ANNOTATIONS }, + { + type: ERROR_ADD, + error: errText, + }, + ]); + }); + }); }); });