From 0f667ad990896b11bcfb17b4b229939aada7e59a Mon Sep 17 00:00:00 2001 From: Jhon Vente Date: Tue, 10 Oct 2023 09:40:39 -0500 Subject: [PATCH] refactor: change email learnerns logic and new test case for BuilkEmailForm --- .../bulk-email-form/BulkEmailForm.jsx | 10 ++++--- .../test/BulkEmailForm.test.jsx | 27 +++++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/components/bulk-email-tool/bulk-email-form/BulkEmailForm.jsx b/src/components/bulk-email-tool/bulk-email-form/BulkEmailForm.jsx index a6288ac2..747bcb23 100644 --- a/src/components/bulk-email-tool/bulk-email-form/BulkEmailForm.jsx +++ b/src/components/bulk-email-tool/bulk-email-form/BulkEmailForm.jsx @@ -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]); diff --git a/src/components/bulk-email-tool/bulk-email-form/test/BulkEmailForm.test.jsx b/src/components/bulk-email-tool/bulk-email-form/test/BulkEmailForm.test.jsx index b7a29039..245860f2 100644 --- a/src/components/bulk-email-tool/bulk-email-form/test/BulkEmailForm.test.jsx +++ b/src/components/bulk-email-tool/bulk-email-form/test/BulkEmailForm.test.jsx @@ -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: 'test@email.com' } }); + expect(emailLearnerInput.value).toBe('test@email.com'); + fireEvent.click(learnersEmailAddButton); + // clean the input after adding an email learner + expect(emailLearnerInput.value).toBe(''); + expect(dispatchMock).toHaveBeenCalled(); + }); });