diff --git a/src/components/Tool/FixMailingAddresses/Contact.tsx b/src/components/Tool/FixMailingAddresses/Contact.tsx index 1341fb177..990146302 100644 --- a/src/components/Tool/FixMailingAddresses/Contact.tsx +++ b/src/components/Tool/FixMailingAddresses/Contact.tsx @@ -120,7 +120,6 @@ interface Props { addresses, id, name, - onlyErrorOnce, }: HandleSingleConfirmProps) => void; } diff --git a/src/components/Tool/FixMailingAddresses/FixMailingAddresses.test.tsx b/src/components/Tool/FixMailingAddresses/FixMailingAddresses.test.tsx index c434631d5..be34724c6 100644 --- a/src/components/Tool/FixMailingAddresses/FixMailingAddresses.test.tsx +++ b/src/components/Tool/FixMailingAddresses/FixMailingAddresses.test.tsx @@ -429,7 +429,40 @@ describe('FixSendNewsletter', () => { }); describe('handleSingleConfirm()', () => { - it('should fire handleSingleConfirm', async () => { + const name = 'Baggins, Frodo'; + it('should handle error', async () => { + const { getAllByRole, getByText, queryByTestId } = render( + { + throw new Error('Server Error'); + }, + }} + />, + ); + await waitFor(() => + expect(queryByTestId('loading')).not.toBeInTheDocument(), + ); + userEvent.click(getAllByRole('button', { name: 'Confirm' })[0]); + + await waitFor(() => expect(getByText(name)).toBeInTheDocument()); + + await waitFor(() => { + expect(mockEnqueue).toHaveBeenCalledWith( + `Error updating contact ${name}`, + { + variant: 'error', + autoHideDuration: 7000, + }, + ); + }); + expect(getByText(name)).toBeInTheDocument(); + }); + + it('should handle success and remove contact', async () => { const { getAllByRole, getByText, queryByTestId, queryByText } = render( { ); userEvent.click(getAllByRole('button', { name: 'Confirm' })[0]); - const name = 'Baggins, Frodo'; await waitFor(() => expect(getByText(name)).toBeInTheDocument()); await waitFor(() => { @@ -457,10 +489,86 @@ describe('FixSendNewsletter', () => { }); describe('handleBulkConfirm()', () => { - it('should fire handleSingleConfirm', async () => { + const name1 = 'Baggins, Frodo'; + const name2 = 'Gamgee, Samwise'; + + it('should handle Error', async () => { + process.env.APP_NAME = 'MPDX'; + const { getByRole, getByText, queryByTestId } = render( + { + throw new Error('Server Error'); + }, + }} + />, + ); + await waitFor(() => + expect(queryByTestId('loading')).not.toBeInTheDocument(), + ); + + expect(getByText(name1)).toBeInTheDocument(); + expect(getByText(name2)).toBeInTheDocument(); + + userEvent.click(getByRole('button', { name: 'Confirm 2 as MPDX' })); + + await waitFor(() => + expect(getByRole('heading', { name: 'Confirm' })).toBeInTheDocument(), + ); + + userEvent.click(getByRole('button', { name: 'Yes' })); + + await waitFor(() => { + expect(mockEnqueue).toHaveBeenCalledWith( + `Error updating contact ${name1}`, + { + variant: 'error', + autoHideDuration: 7000, + }, + ); + expect(mockEnqueue).toHaveBeenCalledWith( + `Error updating contact ${name2}`, + { + variant: 'error', + autoHideDuration: 7000, + }, + ); + expect(mockEnqueue).toHaveBeenCalledWith( + `Error when updating 2 contact(s)`, + { + variant: 'error', + }, + ); + + expect(getByText(name1)).toBeInTheDocument(); + expect(getByText(name2)).toBeInTheDocument(); + }); + }); + + it('should handle success and remove contacts', async () => { process.env.APP_NAME = 'MPDX'; - const name1 = 'Baggins, Frodo'; - const name2 = 'Gamgee, Samwise'; const { getByRole, queryByTestId, queryByText } = render( { }); }); - it('should fire handleSingleConfirm', async () => { + it('should not fire handleSingleConfirm', async () => { process.env.APP_NAME = 'MPDX'; const { getByRole, queryByTestId, queryByText } = render( { expect(getByRole('heading', { name: 'Confirm' })).toBeInTheDocument(), ); - userEvent.click(getByRole('button', { name: 'Yes' })); + userEvent.click(getByRole('button', { name: 'No' })); await waitFor(() => { - expect(mockEnqueue).toHaveBeenCalledWith(`Updated contact ${name}`, { - variant: 'success', - }); - expect(queryByText(name)).not.toBeInTheDocument(); + expect(mockEnqueue).not.toHaveBeenCalled(); + expect(queryByText(name)).toBeInTheDocument(); }); }); }); diff --git a/src/components/Tool/FixMailingAddresses/FixMailingAddresses.tsx b/src/components/Tool/FixMailingAddresses/FixMailingAddresses.tsx index e738f529d..5fc6a7cfd 100644 --- a/src/components/Tool/FixMailingAddresses/FixMailingAddresses.tsx +++ b/src/components/Tool/FixMailingAddresses/FixMailingAddresses.tsx @@ -153,9 +153,8 @@ const FixSendNewsletter: React.FC = ({ addresses, id, name, - onlyErrorOnce = false, }: HandleSingleConfirmProps) => { - const errors: string[] = []; + let errorOccurred = false; for (let idx = 0; idx < addresses.length; idx++) { const address = addresses[idx]; @@ -170,29 +169,21 @@ const FixSendNewsletter: React.FC = ({ }, }, update(cache) { - if (idx === addresses.length - 1 && !errors.length) { + if (idx === addresses.length - 1 && !errorOccurred) { cache.evict({ id: `Contact:${id}` }); } }, - onError(error) { - errors.push( - `${name} - ${t('Error while saving addresses.')} ${error.cause}`, - ); + onError() { + errorOccurred = true; }, }); } - if (errors.length) { - if (onlyErrorOnce) { - enqueueSnackbar(t(`Error updating contact ${name}`), { - variant: 'error', - autoHideDuration: 7000, - }); - } else { - errors.forEach((error) => { - enqueueSnackbar(error, { variant: 'error' }); - }); - } + if (errorOccurred) { + enqueueSnackbar(t(`Error updating contact ${name}`), { + variant: 'error', + autoHideDuration: 7000, + }); return { success: false }; } else { enqueueSnackbar(t(`Updated contact ${name}`), { variant: 'success' }); @@ -222,7 +213,6 @@ const FixSendNewsletter: React.FC = ({ addresses, id: contact.id, name: contact.name, - onlyErrorOnce: true, }); callsByContact.push(callContactMutation); }