Skip to content

Commit

Permalink
Adding test for errors during single or bulk mutations
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-bizz committed Jun 28, 2024
1 parent 02e4111 commit f4ed663
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 31 deletions.
1 change: 0 additions & 1 deletion src/components/Tool/FixMailingAddresses/Contact.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ interface Props {
addresses,
id,
name,
onlyErrorOnce,
}: HandleSingleConfirmProps) => void;
}

Expand Down
128 changes: 117 additions & 11 deletions src/components/Tool/FixMailingAddresses/FixMailingAddresses.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(
<Components
mocks={{
InvalidAddresses: {
...mockInvalidAddressesResponse.InvalidAddresses,
},
UpdateContactAddress: () => {
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(
<Components
mocks={{
Expand All @@ -444,7 +477,6 @@ describe('FixSendNewsletter', () => {
);
userEvent.click(getAllByRole('button', { name: 'Confirm' })[0]);

const name = 'Baggins, Frodo';
await waitFor(() => expect(getByText(name)).toBeInTheDocument());

await waitFor(() => {
Expand All @@ -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(
<Components
mocks={{
InvalidAddresses: {
contacts: {
nodes: [
{
id: 'contactId',
name: name1,
status: null,
addresses: {
nodes: [mpdxSourcedAddress, tntSourcedAddress],
},
},
{
id: 'contactId2',
name: name2,
status: null,
addresses: {
nodes: [mpdxSourcedAddress, tntSourcedAddress],
},
},
],
},
},
UpdateContactAddress: () => {
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(
<Components
mocks={{
Expand Down Expand Up @@ -514,7 +622,7 @@ describe('FixSendNewsletter', () => {
});
});

it('should fire handleSingleConfirm', async () => {
it('should not fire handleSingleConfirm', async () => {
process.env.APP_NAME = 'MPDX';
const { getByRole, queryByTestId, queryByText } = render(
<Components
Expand All @@ -538,13 +646,11 @@ describe('FixSendNewsletter', () => {
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();
});
});
});
28 changes: 9 additions & 19 deletions src/components/Tool/FixMailingAddresses/FixMailingAddresses.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,8 @@ const FixSendNewsletter: React.FC<Props> = ({
addresses,
id,
name,
onlyErrorOnce = false,
}: HandleSingleConfirmProps) => {
const errors: string[] = [];
let errorOccurred = false;

for (let idx = 0; idx < addresses.length; idx++) {
const address = addresses[idx];
Expand All @@ -170,29 +169,21 @@ const FixSendNewsletter: React.FC<Props> = ({
},
},
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' });
Expand Down Expand Up @@ -222,7 +213,6 @@ const FixSendNewsletter: React.FC<Props> = ({
addresses,
id: contact.id,
name: contact.name,
onlyErrorOnce: true,
});
callsByContact.push(callContactMutation);
}
Expand Down

0 comments on commit f4ed663

Please sign in to comment.