Skip to content

Commit

Permalink
test: improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
bradenmacdonald committed Dec 2, 2024
1 parent 9132f8c commit dd16567
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions src/instructor-toolbar/masquerade-widget/MasqueradeWidget.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
fireEvent,
getAllByRole,
initializeTestStore,
logUnhandledRequests,
render,
screen,
waitFor,
Expand All @@ -18,8 +17,8 @@ describe('Masquerade Widget Dropdown', () => {
let mockData;
let courseware;
let mockResponse;
let axiosMock;
let masqueradeUrl;
let axiosMock: MockAdapter;
let masqueradeUrl: string;
const masqueradeOptions = [
{
name: 'Staff',
Expand All @@ -45,7 +44,7 @@ describe('Masquerade Widget Dropdown', () => {
masqueradeUrl = `${getConfig().LMS_BASE_URL}/courses/${courseware.courseId}/masquerade`;
mockData = {
courseId: courseware.courseId,
onError: () => {},
onError: jest.fn(),
};
});

Expand All @@ -64,7 +63,6 @@ describe('Masquerade Widget Dropdown', () => {
};
axiosMock.reset();
axiosMock.onGet(masqueradeUrl).reply(200, mockResponse);
logUnhandledRequests(axiosMock);
});

it('renders masquerade name correctly', async () => {
Expand Down Expand Up @@ -153,4 +151,35 @@ describe('Masquerade Widget Dropdown', () => {
await user.keyboard('{Enter}');
await waitFor(() => expect(axiosMock.history.post).toHaveLength(1));
});

it('can display an error when failing to masquerade as a specific user', async () => {
const user = userEvent.setup();
// Configure our mock:
axiosMock.onPost(masqueradeUrl).reply(200, { // Note: The API endpoint returns a 200 response on error!
success: false,
error: 'That user does not exist',
});
// Render the masquerade controls:
const { container } = render(<MasqueradeWidget {...mockData} />);
await waitFor(() => expect(axiosMock.history.get).toHaveLength(1));

// Select "specific student..."
const dropdownToggle = container.querySelector('.dropdown-toggle')!;
await user.click(dropdownToggle);
const dropdownMenu = container.querySelector('.dropdown-menu') as HTMLElement;
const studentOption = getAllByRole(dropdownMenu, 'button', { hidden: true }).filter(
button => (button.textContent === 'Specific Student...'),
)[0];
await user.click(studentOption);

// Enter a username, POST the request to the server
const usernameInput = await screen.findByLabelText(/Username or email/);
await user.type(usernameInput, 'testuser');
expect(axiosMock.history.post).toHaveLength(0);
await user.keyboard('{Enter}');
await waitFor(() => expect(axiosMock.history.post).toHaveLength(1));
await waitFor(() => {
expect(mockData.onError).toHaveBeenLastCalledWith('That user does not exist');
});
});
});

0 comments on commit dd16567

Please sign in to comment.