From 66e78d92ce9e22f9eadab30ee1069a51e5e36e32 Mon Sep 17 00:00:00 2001 From: Segun Date: Sat, 23 Dec 2017 19:08:27 +0100 Subject: [PATCH] feature(read-book): prevent redirect on update profile failure --- client/__tests__/actions/userActions.spec.js | 2 +- .../__tests__/components/Dashboard/PdfViewer.spec.jsx | 10 ++++++++++ .../components/Dashboard/updateProfile.spec.jsx | 2 +- client/actions/updateProfile.js | 5 ++++- client/components/Dashboard/UpdateProfile.jsx | 6 ++++-- 5 files changed, 20 insertions(+), 5 deletions(-) diff --git a/client/__tests__/actions/userActions.spec.js b/client/__tests__/actions/userActions.spec.js index 1688be1..56b132b 100644 --- a/client/__tests__/actions/userActions.spec.js +++ b/client/__tests__/actions/userActions.spec.js @@ -41,7 +41,7 @@ describe('user Actions', () => { }); const expectedActions = []; const store = mockStore({}); - return store.dispatch(updateProfile({})).then(() => { + return store.dispatch(updateProfile({})).catch(() => { expect(store.getActions()).toEqual(expectedActions); expect(Notify.error).toHaveBeenCalled(); }); diff --git a/client/__tests__/components/Dashboard/PdfViewer.spec.jsx b/client/__tests__/components/Dashboard/PdfViewer.spec.jsx index 3840a17..749a3cc 100644 --- a/client/__tests__/components/Dashboard/PdfViewer.spec.jsx +++ b/client/__tests__/components/Dashboard/PdfViewer.spec.jsx @@ -54,6 +54,16 @@ describe('PdfViewer Component', () => { expect(wrapper.state().page).toBe(1); }); + it('should call onDocumentError if file load encounters an error', () => { + const wrapper = setUp(); + const onPageCompleteSpy = jest.spyOn( + wrapper.instance(), 'onDocumentError' + ); + wrapper.instance().onDocumentError(new Error('failed to load pdf')); + expect(onPageCompleteSpy).toHaveBeenCalledTimes(1); + expect(wrapper.state().fileError).toBe(true); + }); + it('should call setPage when page is manually inserted', () => { const wrapper = setUp(); const setPageSpy = jest.spyOn( diff --git a/client/__tests__/components/Dashboard/updateProfile.spec.jsx b/client/__tests__/components/Dashboard/updateProfile.spec.jsx index e860f45..f99bc1f 100644 --- a/client/__tests__/components/Dashboard/updateProfile.spec.jsx +++ b/client/__tests__/components/Dashboard/updateProfile.spec.jsx @@ -8,7 +8,7 @@ import { mockStoreData } from '../../__mocks__/mockData'; let props = { ...mockStoreData.authReducer, - updateProfile: jest.fn(), + updateProfile: jest.fn(() => Promise.resolve(1)), }; const middleware = [thunk]; diff --git a/client/actions/updateProfile.js b/client/actions/updateProfile.js index 168d97e..03e07bf 100644 --- a/client/actions/updateProfile.js +++ b/client/actions/updateProfile.js @@ -22,7 +22,10 @@ const updateProfile = profile => dispatch => ( Notify.success(response.data.message); return dispatch(loginUser(response.data)); }) - .catch(error => reportNetworkError(error)) + .catch((error) => { + reportNetworkError(error); + return Promise.reject(error); + }) ); export default updateProfile; diff --git a/client/components/Dashboard/UpdateProfile.jsx b/client/components/Dashboard/UpdateProfile.jsx index 682cd7b..ea74077 100644 --- a/client/components/Dashboard/UpdateProfile.jsx +++ b/client/components/Dashboard/UpdateProfile.jsx @@ -46,8 +46,10 @@ export class UpdateProfile extends Component { */ handleSubmit = (event) => { event.preventDefault(); - this.props.updateProfile(this.state); - this.setState(() => ({ redirect: true })); + this.props.updateProfile(this.state) + .then(() => this.setState(() => ({ redirect: true }))) + .catch(() => {}) + ; } /**