Skip to content

Commit

Permalink
Improve test reliability in preparation of React 18. (#17990)
Browse files Browse the repository at this point in the history
* Improve test reliability in preparation of React 18.

* Improving more tests.

* Updating `react-select-event`.

* Improving more tests.

* Improving test.

* Using `act` when needed.

* Migrate `connect` test to testing-library.

* Make sure enzyme types are present.
  • Loading branch information
dennisoelkers authored Jan 19, 2024
1 parent 75ce9ac commit c8d84e8
Show file tree
Hide file tree
Showing 18 changed files with 191 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"jest-environment-jsdom": "29.6.2",
"jest-enzyme": "^7.1.2",
"jest-styled-components": "7.2.0",
"react-select-event": "^5.0.0",
"react-select-event": "5.5.1",
"resize-observer-polyfill": "^1.5.1"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { render, screen, act, fireEvent, waitFor } from 'wrappedTestingLibrary';

import { HTTPHeaderAuthConfigActions } from 'stores/authentication/HTTPHeaderAuthConfigStore';
import HTTPHeaderAuthConfig from 'logic/authentication/HTTPHeaderAuthConfig';
import asMock from 'helpers/mocking/AsMock';

import HTTPHeaderAuthConfigSection from './HTTPHeaderAuthConfigSection';

Expand All @@ -39,6 +40,7 @@ describe('<HTTPHeaderAuthConfigSection />', () => {

it('should display loading indicator while loading', async () => {
jest.useFakeTimers();
asMock(HTTPHeaderAuthConfigActions.load).mockImplementationOnce(() => new Promise(() => {}));

render(<HTTPHeaderAuthConfigSection />);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
import * as React from 'react';
import { render, screen, waitFor } from 'wrappedTestingLibrary';
import userEvent from '@testing-library/user-event';

import MenuItem from 'components/bootstrap/MenuItem';

Expand All @@ -30,7 +31,7 @@ describe('DropdownButton', () => {
));

const button = await screen.findByRole('button', { name: 'Click me!' });
button.click();
await userEvent.click(button);

await screen.findByRole('menuitem', { name: 'Hey there!' });
});
Expand All @@ -45,10 +46,10 @@ describe('DropdownButton', () => {
));

const button = await screen.findByRole('button', { name: 'Click me!' });
button.click();
await userEvent.click(button);

const menuitem = await screen.findByRole('menuitem', { name: 'Hey there!' });
menuitem.click();
await userEvent.click(menuitem);

await waitFor(() => {
expect(onClick).toHaveBeenCalled();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import React from 'react';
import { screen, render } from 'wrappedTestingLibrary';
import Immutable from 'immutable';
import { defaultUser } from 'defaultMockValues';
import userEvent from '@testing-library/user-event';

import { adminUser } from 'fixtures/users';
import MockAction from 'helpers/mocking/MockAction';
Expand Down Expand Up @@ -61,7 +62,7 @@ describe('<URLWhiteListFormModal>', () => {

expect(addButton).toBeInTheDocument();

addButton.click();
await userEvent.click(addButton);

expect(await screen.findByText('Whitelist URLs')).toBeInTheDocument();
expect(screen.getByDisplayValue('http://graylog.com')).toBeInTheDocument();
Expand Down Expand Up @@ -110,7 +111,7 @@ describe('<URLWhiteListFormModal>', () => {

expect(addButton).toBeInTheDocument();

addButton.click();
await userEvent.click(addButton);

expect(await screen.findByText('Whitelist URLs')).toBeInTheDocument();
expect(screen.getByDisplayValue('http://localhost(:\\d+)?')).toBeInTheDocument();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ describe('UrlWhitelistForm', () => {
jest.useRealTimers();
});

it('should show allow list toggle and url table', () => {
it('should show allow list toggle and url table', async () => {
const onUpdate = jest.fn();

render(<UrlWhiteListForm urls={config.entries}
disabled={config.disabled}
onUpdate={onUpdate} />);

expect(screen.getByRole('checkbox', { name: /disable whitelist/i })).toBeInTheDocument();
await screen.findByRole('checkbox', { name: /disable whitelist/i });

config.entries.forEach(({ title }) => {
expect(screen.getByDisplayValue(title)).toBeInTheDocument();
Expand Down Expand Up @@ -117,8 +117,8 @@ describe('UrlWhitelistForm', () => {
disabled={config.disabled}
onUpdate={onUpdate} />);

const row = screen.getByRole('row', { name: /3/i, exact: true });
const select = within(row).getByText(/exact match/i);
const row = await screen.findByRole('row', { name: /3/i });
const select = await within(row).findByText(/exact match/i);

await selectEvent.openMenu(select);
await selectEvent.select(select, 'Regex');
Expand All @@ -138,24 +138,26 @@ describe('UrlWhitelistForm', () => {
);
});

it('should add a new row to the form', () => {
it('should add a new row to the form', async () => {
const onUpdate = jest.fn();

render(<UrlWhiteListForm urls={config.entries}
disabled={config.disabled}
onUpdate={onUpdate} />);

expect(screen.queryByRole('cell', { name: String(config.entries.length + 1) })).not.toBeInTheDocument();
const button = (await screen.findAllByRole('button', { name: /add url/i }))[0];

const button = screen.getAllByRole('button', { name: /add url/i })[0];
expect(screen.queryByRole('cell', { name: String(config.entries.length + 1) })).not.toBeInTheDocument();

expect(button).toBeInTheDocument();

userEvent.click(button);

expect(screen.getByRole('cell', { name: String(config.entries.length + 1) })).toBeInTheDocument();

expect(onUpdate).toHaveBeenCalledTimes(1);
await waitFor(() => {
expect(onUpdate).toHaveBeenCalled();
});
});

it('should delete a row', async () => {
Expand All @@ -167,7 +169,7 @@ describe('UrlWhitelistForm', () => {

expect(screen.getByDisplayValue(config.entries[0].title)).toBeInTheDocument();

const row = screen.getByRole('row', { name: /1/i, exact: true });
const row = screen.getByRole('row', { name: /1/i });
const deleteButton = within(row).getByRole('button', { name: /delete entry/i });

expect(deleteButton).toBeInTheDocument();
Expand Down Expand Up @@ -244,10 +246,11 @@ describe('UrlWhitelistForm', () => {
disabled={config.disabled}
onUpdate={onUpdate} />);

const row = screen.getByRole('row', { name: /3/i, exact: true });
const row = screen.getByRole('row', { name: /3/i });
const select = within(row).getByText(/exact match/i);

await selectEvent.openMenu(select);

await selectEvent.select(select, 'Regex');

await screen.findByText(/not a valid java regular expression/i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
import * as React from 'react';
import { render, fireEvent, waitFor, screen } from 'wrappedTestingLibrary';
import { act, render, fireEvent, waitFor, screen } from 'wrappedTestingLibrary';
import selectEvent from 'react-select-event';
import { List } from 'immutable';

Expand Down Expand Up @@ -79,7 +79,11 @@ describe('<SettingsSection />', () => {
await screen.findByText('Hours');

fireEvent.change(timeoutAmountInput, { target: { value: '40' } });
await selectEvent.select(timeoutUnitSelect, 'Days');

await act(async () => {
await selectEvent.select(timeoutUnitSelect, 'Days');
});

await selectEvent.select(timezoneSelect, 'Vancouver');
fireEvent.click(submitButton);

Expand Down
11 changes: 8 additions & 3 deletions graylog2-web-interface/src/hooks/useLogout.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ import * as React from 'react';
import { render, screen } from 'wrappedTestingLibrary';
import { createMemoryRouter, RouterProvider } from 'react-router-dom';
import DefaultProviders from 'DefaultProviders';
import userEvent from '@testing-library/user-event';

import Routes from 'routing/Routes';
import { usePluginExports } from 'views/test/testPlugins';
import suppressConsole from 'helpers/suppressConsole';

import useLogout from './useLogout';

Expand All @@ -48,7 +50,7 @@ describe('useLogout', () => {
await screen.findByText('Logged in');

const logoutButton = await screen.findByRole('button', { name: 'logout' });
logoutButton.click();
userEvent.click(logoutButton);

await screen.findByText('Logged out');
});
Expand All @@ -63,7 +65,7 @@ describe('useLogout', () => {
await screen.findByText('Logged in');

const logoutButton = await screen.findByRole('button', { name: 'logout' });
logoutButton.click();
userEvent.click(logoutButton);

await screen.findByText('Logged out');

Expand All @@ -81,7 +83,10 @@ describe('useLogout', () => {
await screen.findByText('Logged in');

const logoutButton = await screen.findByRole('button', { name: 'logout' });
logoutButton.click();

suppressConsole(() => {
userEvent.click(logoutButton);
});

await screen.findByText('Logged out');

Expand Down
Loading

0 comments on commit c8d84e8

Please sign in to comment.