Skip to content

Commit

Permalink
fix: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ArturGaspar committed Jul 29, 2024
1 parent 72a4ba3 commit c2c470c
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 50 deletions.
23 changes: 10 additions & 13 deletions src/course-rerun/CourseRerun.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ jest.mock('react-redux', () => ({
useSelector: jest.fn(),
}));

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useLocation: () => ({
pathname: mockPathname,
}),
}));

const RootWrapper = () => (
<MemoryRouter>
<AppProvider store={store} wrapWithRouter={false}>
Expand Down Expand Up @@ -77,17 +70,21 @@ describe('<CourseRerun />', () => {

it('shows the spinner before the query is complete', async () => {
useSelector.mockReturnValue({ organizationLoadingStatus: RequestStatus.IN_PROGRESS });
const { getByRole } = render(<RootWrapper />);

await act(async () => {
const { getByRole } = render(<RootWrapper />);
const spinner = getByRole('status');
expect(spinner.textContent).toEqual('Loading...');
await waitFor(() => {
const spinner = getByRole('status');
expect(spinner.textContent).toEqual('Loading...');
});
});
});

it('should show footer', () => {
it('should show footer', async () => {
const { getByText } = render(<RootWrapper />);
expect(getByText('Looking for help with Studio?')).toBeInTheDocument();
expect(getByText('LMS')).toHaveAttribute('href', process.env.LMS_BASE_URL);
await waitFor(() => {
expect(getByText('Looking for help with Studio?')).toBeInTheDocument();
expect(getByText('LMS')).toHaveAttribute('href', process.env.LMS_BASE_URL);
});
});
});
2 changes: 1 addition & 1 deletion src/files-and-videos/videos-page/VideosPage.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ describe('Videos page', () => {
uploadSpy.mockResolvedValue(new Promise(() => {}));

const addFilesButton = screen.getAllByLabelText('file-input')[3];
act(async () => {
await act(async () => {
userEvent.upload(addFilesButton, file);
});
await waitFor(() => {
Expand Down
44 changes: 26 additions & 18 deletions src/pages-and-resources/discussions/DiscussionsSettings.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ describe('DiscussionsSettings', () => {

expect(queryByTestId(container, 'appConfigForm')).toBeInTheDocument();

await act(() => userEvent.click(queryByText(container, appMessages.backButton.defaultMessage)));
act(() => userEvent.click(queryByText(container, appMessages.backButton.defaultMessage)));

await waitFor(() => {
expect(queryByTestId(container, 'appList')).toBeInTheDocument();
Expand Down Expand Up @@ -185,7 +185,7 @@ describe('DiscussionsSettings', () => {
// content has been loaded - prior to proceeding with our expectations.
await waitForElementToBeRemoved(screen.getByRole('status'));

act(async () => {
await act(async () => {
userEvent.click(queryByLabelText(container, 'Select Piazza'));

userEvent.click(getByRole(container, 'button', { name: 'Next' }));
Expand All @@ -210,11 +210,17 @@ describe('DiscussionsSettings', () => {
// content has been loaded - prior to proceeding with our expectations.
await waitForElementToBeRemoved(screen.getByRole('status'));

act(async () => {
userEvent.click(getByRole(container, 'checkbox', { name: 'Select Discourse' }));
userEvent.click(getByRole(container, 'button', { name: 'Next' }));
const discourseBox = getByRole(container, 'checkbox', { name: 'Select Discourse' });
expect(discourseBox).not.toBeDisabled();
userEvent.click(discourseBox);

userEvent.click(getByRole(container, 'button', { name: 'Next' }));

await waitFor(() => expect(screen.queryByRole('status')).toBeNull());

await act(async () => {
expect(await findByRole(container, 'heading', { name: 'Discourse' })).toBeInTheDocument();

await findByRole(container, 'button', { name: 'Save' });
userEvent.type(getByRole(container, 'textbox', { name: 'Consumer Key' }), 'key');
userEvent.type(getByRole(container, 'textbox', { name: 'Consumer Secret' }), 'secret');
userEvent.type(getByRole(container, 'textbox', { name: 'Launch URL' }), 'http://example.test');
Expand All @@ -241,15 +247,15 @@ describe('DiscussionsSettings', () => {

await waitFor(() => expect(screen.queryByRole('status')).toBeNull());

act(async () => {
await act(async () => {
expect(await findByRole(container, 'heading', { name: 'Discourse' })).toBeInTheDocument();

userEvent.type(getByRole(container, 'textbox', { name: 'Consumer Key' }), 'a');
userEvent.type(getByRole(container, 'textbox', { name: 'Consumer Secret' }), 'secret');
userEvent.type(getByRole(container, 'textbox', { name: 'Launch URL' }), 'http://example.test');
userEvent.click(getByRole(container, 'button', { name: 'Save' }));

waitFor(() => expect(getByRole(container, 'dialog', { name: 'OK' })).toBeInTheDocument());
await waitFor(() => expect(getByRole(container, 'dialog', { name: 'OK' })).toBeInTheDocument());
userEvent.click(getByRole(container, 'button', { name: 'Cancel' }));

expect(queryByRole(container, 'dialog', { name: 'Confirm' })).not.toBeInTheDocument();
Expand Down Expand Up @@ -305,7 +311,7 @@ describe('DiscussionsSettings', () => {
await waitForElementToBeRemoved(screen.getByRole('status'));

// Apply causes an async action to take place
act(async () => {
await act(async () => {
userEvent.click(queryByText(container, appMessages.saveButton.defaultMessage));
await waitFor(() => expect(axiosMock.history.post.length).toBe(1));

Expand Down Expand Up @@ -350,13 +356,15 @@ describe('DiscussionsSettings', () => {
// content has been loaded - prior to proceeding with our expectations.
await waitForElementToBeRemoved(screen.getByRole('status'));

act(async () => {
await act(async () => {
userEvent.click(getByRole(container, 'button', { name: 'Save' }));

await waitFor(() => expect(axiosMock.history.post.length).toBe(1));

expect(queryByTestId(container, 'appList')).not.toBeInTheDocument();
expect(queryByTestId(container, 'appConfigForm')).not.toBeInTheDocument();
await waitFor(() => {
expect(queryByTestId(container, 'appList')).not.toBeInTheDocument();
expect(queryByTestId(container, 'appConfigForm')).not.toBeInTheDocument();
});

// We don't technically leave the route in this case, though the modal is hidden.
expect(window.location.pathname).toEqual(`/course/${courseId}/pages-and-resources/discussion/configure/piazza`);
Expand Down Expand Up @@ -406,12 +414,12 @@ describe.each([
renderComponent(`/course/${courseId}/pages-and-resources/discussion`);
// This is an important line that ensures the spinner has been removed - and thus our main
// content has been loaded - prior to proceeding with our expectations.
waitForElementToBeRemoved(screen.getByRole('status'));
await waitForElementToBeRemoved(screen.getByRole('status'));

act(async () => {
await act(async () => {
userEvent.click(await screen.findByLabelText('Select Piazza'));
userEvent.click(queryByText(container, messages.nextButton.defaultMessage));
waitForElementToBeRemoved(screen.getByRole('status'));
await waitFor(() => waitForElementToBeRemoved(screen.getByRole('status')));

if (showLTIConfig) {
expect(queryByText(container, ltiMessages.formInstructions.defaultMessage)).toBeInTheDocument();
Expand Down Expand Up @@ -460,13 +468,13 @@ describe.each([
renderComponent(`/course/${courseId}/pages-and-resources/discussion`);
// This is an important line that ensures the spinner has been removed - and thus our main
// content has been loaded - prior to proceeding with our expectations.
waitForElementToBeRemoved(screen.getByRole('status'));
await waitForElementToBeRemoved(screen.getByRole('status'));

act(async () => {
await act(async () => {
userEvent.click(await screen.findByLabelText('Select Piazza'));
userEvent.click(await screen.findByText(messages.nextButton.defaultMessage));

waitForElementToBeRemoved(screen.getByRole('status'));
await waitFor(() => waitForElementToBeRemoved(screen.getByRole('status')));
if (enablePIISharing) {
expect(queryByTestId(container, 'piiSharingFields')).toBeInTheDocument();
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/studio-home/StudioHome.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ describe('<StudioHome />', async () => {
userIsActive: true,
});

await act(async () => {
const { getByRole } = render(<RootWrapper />);
const { getByRole } = render(<RootWrapper />);
await waitFor(() => {
const spinner = getByRole('status');
expect(spinner.textContent).toEqual('Loading...');
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,19 @@ exports[`CoursesFilters snapshot 1`] = `
search
</span>
</label>
<input
class="form-control"
id="pgn-searchfield-input-1"
name="searchfield-input"
placeholder="Search"
role="searchbox"
type="text"
value=""
/>
<div
class="pgn__form-control-decorator-group"
>
<input
class="form-control"
id="pgn-searchfield-input-1"
name="searchfield-input"
placeholder="Search"
role="searchbox"
type="text"
value=""
/>
</div>
<button
aria-label="submit search"
class="btn-icon btn-icon-primary btn-icon-sm pgn__searchfield__iconbutton-submit"
Expand Down
12 changes: 5 additions & 7 deletions src/taxonomy/TaxonomyListPage.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,18 @@ describe('<TaxonomyListPage />', () => {
it('shows the spinner before the query is complete', async () => {
// Simulate an API request that times out:
axiosMock.onGet(listTaxonomiesUrl).reply(new Promise(() => {}));
await act(async () => {
const { getByRole } = render(<RootWrapper />);
const { getByRole } = render(<RootWrapper />);
await waitFor(() => {
const spinner = getByRole('status');
expect(spinner.textContent).toEqual('Loading');
});
});

it('shows the data table after the query is complete', async () => {
axiosMock.onGet(listTaxonomiesUrl).reply(200, { results: taxonomies, canAddTaxonomy: false });
await act(async () => {
const { getByTestId, queryByText } = render(<RootWrapper />);
await waitFor(() => { expect(queryByText('Loading')).toEqual(null); });
expect(getByTestId('taxonomy-card-1')).toBeInTheDocument();
});
const { getByTestId, queryByText } = render(<RootWrapper />);
await waitFor(() => { expect(queryByText('Loading')).toEqual(null); });
expect(getByTestId('taxonomy-card-1')).toBeInTheDocument();
});

it.each(['CSV', 'JSON'])('downloads the taxonomy template %s', async (fileFormat) => {
Expand Down

0 comments on commit c2c470c

Please sign in to comment.