Skip to content

Commit

Permalink
refactor: Update error message while adding library team member [FC-0…
Browse files Browse the repository at this point in the history
…062] (#1572)

Update the error message while adding a library team member to show the error message generated by the server
  • Loading branch information
ChrisChV authored Dec 18, 2024
1 parent b110b6b commit 64906a1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 8 deletions.
31 changes: 28 additions & 3 deletions src/library-authoring/library-team/LibraryTeam.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,10 @@ describe('<LibraryTeam />', () => {
expect(await screen.findByText('Team Member added')).toBeInTheDocument();
});

it('shows error when user do not exist', async () => {
it('shows error when specific error (string)', async () => {
const url = getLibraryTeamApiUrl(libraryId);
const axiosMock = new MockAdapter(getAuthenticatedHttpClient());
axiosMock.onPost(url).reply(400, { email: 'Error' });
axiosMock.onPost(url).reply(400, { email: 'This is a specific error.' });

await renderLibraryTeam();

Expand All @@ -204,7 +204,32 @@ describe('<LibraryTeam />', () => {
});

expect(await screen.findByText(
'Error adding Team Member. Please verify that the email is correct and belongs to a registered user.',
'Error adding Team Member. This is a specific error.',
)).toBeInTheDocument();
});

it('shows error when specific error (Array)', async () => {
const url = getLibraryTeamApiUrl(libraryId);
const axiosMock = new MockAdapter(getAuthenticatedHttpClient());
axiosMock.onPost(url).reply(400, { email: ['This is a specific error.'] });

await renderLibraryTeam();

const addButton = screen.getByRole('button', { name: 'New team member' });
userEvent.click(addButton);
const emailInput = screen.getByRole('textbox', { name: 'User\'s email address' });
userEvent.click(emailInput);
userEvent.type(emailInput, '[email protected]');

const saveButton = screen.getByRole('button', { name: /add member/i });
userEvent.click(saveButton);

await waitFor(() => {
expect(axiosMock.history.post.length).toEqual(1);
});

expect(await screen.findByText(
'Error adding Team Member. This is a specific error.',
)).toBeInTheDocument();
});

Expand Down
11 changes: 10 additions & 1 deletion src/library-authoring/library-team/LibraryTeam.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,16 @@ const LibraryTeam: React.FC<Record<never, never>> = () => {
}).catch((addMemberError) => {
const errorData = typeof addMemberError === 'object' ? addMemberError.response?.data : undefined;
if (errorData && 'email' in errorData) {
showToast(intl.formatMessage(messages.addMemberEmailError));
const errorEmail = errorData.email;
if (typeof errorEmail === 'string') {
showToast(intl.formatMessage(messages.addMemberSpecificError, {
message: errorEmail,
}));
} else {
showToast(intl.formatMessage(messages.addMemberSpecificError, {
message: errorEmail[0],
}));
}
} else {
showToast(intl.formatMessage(messages.addMemberError));
}
Expand Down
8 changes: 4 additions & 4 deletions src/library-authoring/library-team/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,10 @@ const messages = defineMessages({
defaultMessage: 'Error adding Team Member',
description: 'Message shown when an error occurs while adding a Library Team member',
},
addMemberEmailError: {
id: 'course-authoring.library-authoring.library-team.add-member-email-error',
defaultMessage: 'Error adding Team Member. Please verify that the email is correct and belongs to a registered user.',
description: 'Message shown when an error occurs with email while adding a Library Team member.',
addMemberSpecificError: {
id: 'course-authoring.library-authoring.library-team.add-member-specific-error',
defaultMessage: 'Error adding Team Member. {message}',
description: 'Message shown when an error occurs while adding a Library Team member, including a specific error message.',
},
deleteMemberSuccess: {
id: 'course-authoring.library-authoring.library-team.delete-member-success',
Expand Down

0 comments on commit 64906a1

Please sign in to comment.