Skip to content

Commit

Permalink
refactor: change email learnerns logic and new test case for BuilkEma…
Browse files Browse the repository at this point in the history
…ilForm
  • Loading branch information
johnvente committed Oct 10, 2023
1 parent 3b14c1f commit 0f667ad
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/components/bulk-email-tool/bulk-email-form/BulkEmailForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,16 @@ function BulkEmailForm(props) {
if not, we will delete the individual-learners from emailRecipients because of we won't use the emails
*/
useEffect(() => {
if (learnersEmailList.length && !editor.emailRecipients.includes('individual-learners')) {
const hasLearners = learnersEmailList.length > 0;
const hasIndividualLearners = editor.emailRecipients.includes('individual-learners');
const hasLearnersGroup = editor.emailRecipients.includes('learners');

if (hasLearners && !hasIndividualLearners) {
dispatch(addRecipient('individual-learners'));
if (editor.emailRecipients.includes('learners')) {
if (hasLearnersGroup) {
dispatch(removeRecipient('learners'));
}
} else if (!learnersEmailList.length && editor.emailRecipients.includes('individual-learners')) {
} else if (!hasLearners && hasIndividualLearners) {
dispatch(removeRecipient('individual-learners'));
}
}, [dispatch, editor.emailRecipients, learnersEmailList]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,31 @@ describe('bulk-email-form', () => {
fireEvent.click(continueButton);
expect(dispatchMock).toHaveBeenCalled();
});
test('learnersEmailList state is updated correctly', () => {
const { getByTestId } = render(
renderBulkEmailFormContext({
editor: {
editMode: true,
emailBody: 'test',
emailSubject: 'test',
emailRecipients: ['test'],
scheduleDate: formatDate(tomorrow),
scheduleTime: '10:00',
schedulingId: 1,
emailId: 1,
isLoading: false,
errorRetrievingData: false,
},
}),
);

const learnersEmailAddButton = getByTestId('learners-email-add-button');
const emailLearnerInput = getByTestId('learners-email-input');
fireEvent.change(emailLearnerInput, { target: { value: '[email protected]' } });
expect(emailLearnerInput.value).toBe('[email protected]');
fireEvent.click(learnersEmailAddButton);
// clean the input after adding an email learner
expect(emailLearnerInput.value).toBe('');
expect(dispatchMock).toHaveBeenCalled();
});
});

0 comments on commit 0f667ad

Please sign in to comment.