Skip to content

Commit

Permalink
refactor: convert masquerade UI widgets to TypeScript
Browse files Browse the repository at this point in the history
  • Loading branch information
bradenmacdonald committed Nov 6, 2024
1 parent 6534347 commit 6454a22
Show file tree
Hide file tree
Showing 12 changed files with 319 additions and 357 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* eslint-disable import/prefer-default-export */
import React from 'react';
import { useIntl } from '@edx/frontend-platform/i18n';
import { Input } from '@openedx/paragon';

import { MasqueradeStatus, Payload } from './data/api';
import messages from './messages';

interface Props extends Omit<React.InputHTMLAttributes<HTMLInputElement>, 'onSubmit' | 'onError'> {
onError: (error: string) => void;
onSubmit: (payload: Payload) => Promise<MasqueradeStatus>;
}

export const MasqueradeUserNameInput: React.FC<Props> = ({ onSubmit, onError, ...otherProps }) => {
const intl = useIntl();

const handleSubmit = React.useCallback((userIdentifier: string) => {
const payload: Payload = {

Check warning on line 18 in src/instructor-toolbar/masquerade-widget/MasqueradeUserNameInput.tsx

View check run for this annotation

Codecov / codecov/patch

src/instructor-toolbar/masquerade-widget/MasqueradeUserNameInput.tsx#L18

Added line #L18 was not covered by tests
role: 'student',
user_name: userIdentifier, // user name or email
};
onSubmit(payload).then((data) => {

Check warning on line 22 in src/instructor-toolbar/masquerade-widget/MasqueradeUserNameInput.tsx

View check run for this annotation

Codecov / codecov/patch

src/instructor-toolbar/masquerade-widget/MasqueradeUserNameInput.tsx#L22

Added line #L22 was not covered by tests
if (data && data.success) {
global.location.reload();
} else {

Check warning on line 25 in src/instructor-toolbar/masquerade-widget/MasqueradeUserNameInput.tsx

View check run for this annotation

Codecov / codecov/patch

src/instructor-toolbar/masquerade-widget/MasqueradeUserNameInput.tsx#L24-L25

Added lines #L24 - L25 were not covered by tests
const error = (data && data.error) || '';
onError(error);

Check warning on line 27 in src/instructor-toolbar/masquerade-widget/MasqueradeUserNameInput.tsx

View check run for this annotation

Codecov / codecov/patch

src/instructor-toolbar/masquerade-widget/MasqueradeUserNameInput.tsx#L27

Added line #L27 was not covered by tests
}
}).catch(() => {
const message = intl.formatMessage(messages.genericError);
onError(message);

Check warning on line 31 in src/instructor-toolbar/masquerade-widget/MasqueradeUserNameInput.tsx

View check run for this annotation

Codecov / codecov/patch

src/instructor-toolbar/masquerade-widget/MasqueradeUserNameInput.tsx#L29-L31

Added lines #L29 - L31 were not covered by tests
});
return true;

Check warning on line 33 in src/instructor-toolbar/masquerade-widget/MasqueradeUserNameInput.tsx

View check run for this annotation

Codecov / codecov/patch

src/instructor-toolbar/masquerade-widget/MasqueradeUserNameInput.tsx#L33

Added line #L33 was not covered by tests
}, [onError]);

const handleKeyPress = React.useCallback((event: React.KeyboardEvent<HTMLInputElement>) => {
if (event.key === 'Enter') {
return handleSubmit(event.currentTarget.value);

Check warning on line 38 in src/instructor-toolbar/masquerade-widget/MasqueradeUserNameInput.tsx

View check run for this annotation

Codecov / codecov/patch

src/instructor-toolbar/masquerade-widget/MasqueradeUserNameInput.tsx#L38

Added line #L38 was not covered by tests
}
return true;

Check warning on line 40 in src/instructor-toolbar/masquerade-widget/MasqueradeUserNameInput.tsx

View check run for this annotation

Codecov / codecov/patch

src/instructor-toolbar/masquerade-widget/MasqueradeUserNameInput.tsx#L40

Added line #L40 was not covered by tests
}, [handleSubmit]);

return (
<Input
aria-labelledby="masquerade-search-label"
label={intl.formatMessage(messages.userNameLabel)}
onKeyPress={handleKeyPress}
type="text"
{...otherProps}
/>
);
};
163 changes: 0 additions & 163 deletions src/instructor-toolbar/masquerade-widget/MasqueradeWidget.jsx

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import React from 'react';
import { getAllByRole } from '@testing-library/dom';
import { act } from '@testing-library/react';
import { getConfig } from '@edx/frontend-platform';
import MockAdapter from 'axios-mock-adapter';
import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth';
import MasqueradeWidget from './MasqueradeWidget';
import { MasqueradeWidget } from './MasqueradeWidget';
import {
render, screen, fireEvent, initializeTestStore, waitFor, logUnhandledRequests,
} from '../../setupTest';
Expand Down Expand Up @@ -96,11 +95,11 @@ describe('Masquerade Widget Dropdown', () => {
axiosMock.onGet(masqueradeUrl).reply(200, mockResponse);

const { container } = render(<MasqueradeWidget {...mockData} />);
const dropdownToggle = container.querySelector('.dropdown-toggle');
const dropdownToggle = container.querySelector('.dropdown-toggle')!;
await act(async () => {
await fireEvent.click(dropdownToggle);
});
const dropdownMenu = container.querySelector('.dropdown-menu');
const dropdownMenu = container.querySelector('.dropdown-menu') as HTMLElement;
getAllByRole(dropdownMenu, 'button', { hidden: true }).forEach(button => {
if (button.textContent === option.name) {
expect(button).toHaveClass('active');
Expand All @@ -115,11 +114,11 @@ describe('Masquerade Widget Dropdown', () => {
const { container } = render(<MasqueradeWidget {...mockData} />);
await waitFor(() => expect(axiosMock.history.get).toHaveLength(1));

const dropdownToggle = container.querySelector('.dropdown-toggle');
const dropdownToggle = container.querySelector('.dropdown-toggle')!;
await act(async () => {
await fireEvent.click(dropdownToggle);
});
const dropdownMenu = container.querySelector('.dropdown-menu');
const dropdownMenu = container.querySelector('.dropdown-menu') as HTMLElement;
const studentOption = getAllByRole(dropdownMenu, 'button', { hidden: true }).filter(
button => (button.textContent === 'Specific Student...'),
)[0];
Expand Down
Loading

0 comments on commit 6454a22

Please sign in to comment.