Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions src/actions/__tests__/weeklySummaryEmailBCCAction.js.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import axios from 'axios';
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';

import * as types from '../../constants/WeeklySummaryEmailBccConstants';
import { updateWeeklySummaryEmailAssignment } from '../weeklySummaryEmailBCCAction';

vi.mock('axios');

const mockStore = configureMockStore([thunk]);

describe('updateWeeklySummaryEmailAssignment action creator', () => {
it('dispatches updated assignment when API returns wrapped assignment payload', async () => {
const store = mockStore({});
const updatedAssignment = {
_id: 'assignment-id',
email: 'updated@example.com',
assignedTo: { _id: 'user-id', firstName: 'Updated', lastName: 'User' },
};

axios.put.mockResolvedValue({
status: 200,
data: { assignment: updatedAssignment },
});

await store.dispatch(updateWeeklySummaryEmailAssignment('assignment-id', 'updated@example.com'));

expect(store.getActions()).toContainEqual({
type: types.UPDATE_WEEKLY_SUMMARY_EMAIL_ASSIGNMENT,
payload: updatedAssignment,
});
});

it('dispatches updated assignment when API returns assignment directly', async () => {
const store = mockStore({});
const updatedAssignment = {
_id: 'assignment-id',
email: 'updated@example.com',
};

axios.put.mockResolvedValue({
status: 200,
data: updatedAssignment,
});

await store.dispatch(updateWeeklySummaryEmailAssignment('assignment-id', 'updated@example.com'));

expect(store.getActions()).toContainEqual({
type: types.UPDATE_WEEKLY_SUMMARY_EMAIL_ASSIGNMENT,
payload: updatedAssignment,
});
});

it('dispatches error action when update fails', async () => {
const store = mockStore({});
const error = new Error('Network Error');

axios.put.mockRejectedValue(error);

await store.dispatch(updateWeeklySummaryEmailAssignment('assignment-id', 'updated@example.com'));

expect(store.getActions()).toContainEqual({
type: types.WEEKLY_SUMMARY_EMAIL_ASSIGNMENT_ERROR,
payload: error,
});
});
});
13 changes: 12 additions & 1 deletion src/actions/weeklySummaryEmailBCCAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ const weeklySummaryEmailBccError = error => ({
payload: error,
});

const updateWeeklySummaryEmailBcc = assignment => ({
type: types.UPDATE_WEEKLY_SUMMARY_EMAIL_ASSIGNMENT,
payload: assignment,
});

// fetch all assignments
export const getAllWeeklySummaryEmailAssignments = () => {
const url = ENDPOINTS.WEEKLY_SUMMARY_EMAIL_BCC();
Expand Down Expand Up @@ -79,11 +84,17 @@ export const updateWeeklySummaryEmailAssignment = (id, email) => async dispatch
try {
const response = await axios.put(ENDPOINTS.UPDATE_WEEKLY_SUMMARY_EMAIL_BCC(id), { email });
if (response.status === 200) {
dispatch(getAllWeeklySummaryEmailAssignments());
const updated = response?.data?.assignment || response?.data || { _id: id, email };
if (updated && updated._id) {
dispatch(updateWeeklySummaryEmailBcc(updated));
} else {
dispatch(getAllWeeklySummaryEmailAssignments());
}
} else {
dispatch(weeklySummaryEmailBccError(response.data));
}
} catch (error) {
dispatch(weeklySummaryEmailBccError(error));
}
};

Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ const WeeklySummaryEmailAssignmentPopUp = React.memo(props => {
setEditingEmail(email);
};

const handleEditSave = id => {
const handleEditSave = async id => {
if (editingEmail && /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(editingEmail)) {
dispatch(updateWeeklySummaryEmailAssignment(id, editingEmail));
await dispatch(updateWeeklySummaryEmailAssignment(id, editingEmail));
setEditingId(null);
setEditingEmail('');
}
Expand Down
1 change: 1 addition & 0 deletions src/constants/WeeklySummaryEmailBccConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export const GET_WEEKLY_SUMMARY_EMAIL_ASSIGNMENTS = 'GET_WEEKLY_SUMMARY_EMAIL_AS
export const SET_WEEKLY_SUMMARY_EMAIL_ASSIGNMENT = 'SET_WEEKLY_SUMMARY_EMAIL_ASSIGNMENT';
export const DELETE_WEEKLY_SUMMARY_EMAIL_ASSIGNMENT = 'DELETE_WEEKLY_SUMMARY_EMAIL_ASSIGNMENT';
export const WEEKLY_SUMMARY_EMAIL_ASSIGNMENT_ERROR = 'WEEKLY_SUMMARY_EMAIL_ASSIGNMENT_ERROR';
export const UPDATE_WEEKLY_SUMMARY_EMAIL_ASSIGNMENT = 'UPDATE_WEEKLY_SUMMARY_EMAIL_ASSIGNMENT';
8 changes: 8 additions & 0 deletions src/reducers/WeeklySummaryEmailAssignment.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ const WeeklySummaryEmailAssignment = (state = initialState, action = {}) => {
emailAssignment: state.emailAssignment.filter(ele => ele._id !== action.payload),
error: null,
};
case types.UPDATE_WEEKLY_SUMMARY_EMAIL_ASSIGNMENT:
return {
...state,
emailAssignment: state.emailAssignment.map(item =>
item._id === action.payload._id ? { ...item, ...action.payload } : item,
),
error: null,
};
case types.WEEKLY_SUMMARY_EMAIL_ASSIGNMENT_ERROR:
return {
...state,
Expand Down
Loading