|
| 1 | +import configureStore from 'redux-mock-store'; |
| 2 | +import thunk from 'redux-thunk'; |
| 3 | + |
| 4 | +import * as ProjectActions from './project'; |
| 5 | +import * as ActionTypes from '../../../constants'; |
| 6 | +import { apiClient } from '../../../utils/apiClient'; |
| 7 | +import { initialTestState } from '../../../testData/testReduxStore'; |
| 8 | + |
| 9 | +const mockStore = configureStore([thunk]); |
| 10 | + |
| 11 | +const existingProjectState = { |
| 12 | + ...initialTestState, |
| 13 | + project: { |
| 14 | + ...initialTestState.project, |
| 15 | + id: 'project123', |
| 16 | + owner: { id: initialTestState.user.id } |
| 17 | + } |
| 18 | +}; |
| 19 | + |
| 20 | +const newProjectState = { |
| 21 | + ...initialTestState, |
| 22 | + project: { |
| 23 | + ...initialTestState.project, |
| 24 | + id: undefined, |
| 25 | + owner: undefined |
| 26 | + } |
| 27 | +}; |
| 28 | + |
| 29 | +const httpError = (status, data) => ({ |
| 30 | + response: { status, data } |
| 31 | +}); |
| 32 | + |
| 33 | +const expectSaveFailureToast = (actions) => { |
| 34 | + expect(actions).toContainEqual(ProjectActions.startSavingProject()); |
| 35 | + expect(actions).toContainEqual(ProjectActions.endSavingProject()); |
| 36 | + expect(actions).toContainEqual({ |
| 37 | + type: ActionTypes.SET_TOAST_TEXT, |
| 38 | + text: 'Toast.SketchFailedSave' |
| 39 | + }); |
| 40 | + expect(actions).toContainEqual({ type: ActionTypes.SHOW_TOAST }); |
| 41 | +}; |
| 42 | + |
| 43 | +describe('saveProject', () => { |
| 44 | + let store; |
| 45 | + |
| 46 | + afterEach(() => { |
| 47 | + jest.restoreAllMocks(); |
| 48 | + if (store) { |
| 49 | + store.clearActions(); |
| 50 | + } |
| 51 | + }); |
| 52 | + |
| 53 | + describe('updating an existing project (PUT)', () => { |
| 54 | + it('handles a network error without throwing when error.response is undefined', async () => { |
| 55 | + store = mockStore(existingProjectState); |
| 56 | + jest |
| 57 | + .spyOn(apiClient, 'put') |
| 58 | + .mockRejectedValueOnce(new Error('Network Error')); |
| 59 | + |
| 60 | + await expect( |
| 61 | + store.dispatch(ProjectActions.saveProject()) |
| 62 | + ).resolves.toBeUndefined(); |
| 63 | + |
| 64 | + const actions = store.getActions(); |
| 65 | + expectSaveFailureToast(actions); |
| 66 | + expect(actions).toContainEqual(ProjectActions.projectSaveFail(undefined)); |
| 67 | + expect(actions).not.toContainEqual( |
| 68 | + expect.objectContaining({ type: ActionTypes.SHOW_ERROR_MODAL }) |
| 69 | + ); |
| 70 | + }); |
| 71 | + |
| 72 | + it('shows the staleSession modal on 403', async () => { |
| 73 | + store = mockStore(existingProjectState); |
| 74 | + jest |
| 75 | + .spyOn(apiClient, 'put') |
| 76 | + .mockRejectedValueOnce(httpError(403, { error: 'Forbidden' })); |
| 77 | + |
| 78 | + await store.dispatch(ProjectActions.saveProject()); |
| 79 | + |
| 80 | + const actions = store.getActions(); |
| 81 | + expectSaveFailureToast(actions); |
| 82 | + expect(actions).toContainEqual({ |
| 83 | + type: ActionTypes.SHOW_ERROR_MODAL, |
| 84 | + modalType: 'staleSession' |
| 85 | + }); |
| 86 | + expect(actions).not.toContainEqual( |
| 87 | + expect.objectContaining({ type: ActionTypes.PROJECT_SAVE_FAIL }) |
| 88 | + ); |
| 89 | + }); |
| 90 | + |
| 91 | + it('shows the staleProject modal on 409', async () => { |
| 92 | + store = mockStore(existingProjectState); |
| 93 | + jest |
| 94 | + .spyOn(apiClient, 'put') |
| 95 | + .mockRejectedValueOnce(httpError(409, { error: 'Conflict' })); |
| 96 | + |
| 97 | + await store.dispatch(ProjectActions.saveProject()); |
| 98 | + |
| 99 | + const actions = store.getActions(); |
| 100 | + expectSaveFailureToast(actions); |
| 101 | + expect(actions).toContainEqual({ |
| 102 | + type: ActionTypes.SHOW_ERROR_MODAL, |
| 103 | + modalType: 'staleProject' |
| 104 | + }); |
| 105 | + expect(actions).not.toContainEqual( |
| 106 | + expect.objectContaining({ type: ActionTypes.PROJECT_SAVE_FAIL }) |
| 107 | + ); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('creating a new project (POST)', () => { |
| 112 | + it('handles a network error without throwing when error.response is undefined', async () => { |
| 113 | + store = mockStore(newProjectState); |
| 114 | + jest |
| 115 | + .spyOn(apiClient, 'post') |
| 116 | + .mockRejectedValueOnce(new Error('Network Error')); |
| 117 | + |
| 118 | + await expect( |
| 119 | + store.dispatch(ProjectActions.saveProject()) |
| 120 | + ).resolves.toBeUndefined(); |
| 121 | + |
| 122 | + const actions = store.getActions(); |
| 123 | + expectSaveFailureToast(actions); |
| 124 | + expect(actions).toContainEqual(ProjectActions.projectSaveFail(undefined)); |
| 125 | + expect(actions).not.toContainEqual( |
| 126 | + expect.objectContaining({ type: ActionTypes.SHOW_ERROR_MODAL }) |
| 127 | + ); |
| 128 | + }); |
| 129 | + |
| 130 | + it('shows the staleSession modal on 403', async () => { |
| 131 | + store = mockStore(newProjectState); |
| 132 | + jest |
| 133 | + .spyOn(apiClient, 'post') |
| 134 | + .mockRejectedValueOnce(httpError(403, { error: 'Forbidden' })); |
| 135 | + |
| 136 | + await store.dispatch(ProjectActions.saveProject()); |
| 137 | + |
| 138 | + const actions = store.getActions(); |
| 139 | + expectSaveFailureToast(actions); |
| 140 | + expect(actions).toContainEqual({ |
| 141 | + type: ActionTypes.SHOW_ERROR_MODAL, |
| 142 | + modalType: 'staleSession' |
| 143 | + }); |
| 144 | + expect(actions).not.toContainEqual( |
| 145 | + expect.objectContaining({ type: ActionTypes.PROJECT_SAVE_FAIL }) |
| 146 | + ); |
| 147 | + }); |
| 148 | + }); |
| 149 | +}); |
0 commit comments