|
| 1 | +import axios from 'axios'; |
| 2 | +import configureMockStore from 'redux-mock-store'; |
| 3 | +import thunk from 'redux-thunk'; |
| 4 | + |
| 5 | +import * as types from '../../constants/WeeklySummaryEmailBccConstants'; |
| 6 | +import { updateWeeklySummaryEmailAssignment } from '../weeklySummaryEmailBCCAction'; |
| 7 | + |
| 8 | +vi.mock('axios'); |
| 9 | + |
| 10 | +const mockStore = configureMockStore([thunk]); |
| 11 | + |
| 12 | +describe('updateWeeklySummaryEmailAssignment action creator', () => { |
| 13 | + it('dispatches updated assignment when API returns wrapped assignment payload', async () => { |
| 14 | + const store = mockStore({}); |
| 15 | + const updatedAssignment = { |
| 16 | + _id: 'assignment-id', |
| 17 | + email: 'updated@example.com', |
| 18 | + assignedTo: { _id: 'user-id', firstName: 'Updated', lastName: 'User' }, |
| 19 | + }; |
| 20 | + |
| 21 | + axios.put.mockResolvedValue({ |
| 22 | + status: 200, |
| 23 | + data: { assignment: updatedAssignment }, |
| 24 | + }); |
| 25 | + |
| 26 | + await store.dispatch(updateWeeklySummaryEmailAssignment('assignment-id', 'updated@example.com')); |
| 27 | + |
| 28 | + expect(store.getActions()).toContainEqual({ |
| 29 | + type: types.UPDATE_WEEKLY_SUMMARY_EMAIL_ASSIGNMENT, |
| 30 | + payload: updatedAssignment, |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + it('dispatches updated assignment when API returns assignment directly', async () => { |
| 35 | + const store = mockStore({}); |
| 36 | + const updatedAssignment = { |
| 37 | + _id: 'assignment-id', |
| 38 | + email: 'updated@example.com', |
| 39 | + }; |
| 40 | + |
| 41 | + axios.put.mockResolvedValue({ |
| 42 | + status: 200, |
| 43 | + data: updatedAssignment, |
| 44 | + }); |
| 45 | + |
| 46 | + await store.dispatch(updateWeeklySummaryEmailAssignment('assignment-id', 'updated@example.com')); |
| 47 | + |
| 48 | + expect(store.getActions()).toContainEqual({ |
| 49 | + type: types.UPDATE_WEEKLY_SUMMARY_EMAIL_ASSIGNMENT, |
| 50 | + payload: updatedAssignment, |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + it('dispatches error action when update fails', async () => { |
| 55 | + const store = mockStore({}); |
| 56 | + const error = new Error('Network Error'); |
| 57 | + |
| 58 | + axios.put.mockRejectedValue(error); |
| 59 | + |
| 60 | + await store.dispatch(updateWeeklySummaryEmailAssignment('assignment-id', 'updated@example.com')); |
| 61 | + |
| 62 | + expect(store.getActions()).toContainEqual({ |
| 63 | + type: types.WEEKLY_SUMMARY_EMAIL_ASSIGNMENT_ERROR, |
| 64 | + payload: error, |
| 65 | + }); |
| 66 | + }); |
| 67 | +}); |
0 commit comments