test: add testRender()

This adds a testRender() helper function to simplify UI tests. Instead
of mocking the redux store, we use the real thing.
This commit is contained in:
David Lechner
2022-03-09 17:24:46 -06:00
parent d0c84cbb9f
commit 6afee4b6d6
5 changed files with 88 additions and 196 deletions
+10 -40
View File
@@ -1,59 +1,29 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2021 The Pybricks Authors
// Copyright (c) 2021-2022 The Pybricks Authors
import { I18nContext, I18nManager } from '@shopify/react-i18n';
import {
getByLabelText,
render,
screen,
waitForElementToBeRemoved,
} from '@testing-library/react';
import { getByLabelText, waitForElementToBeRemoved } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import React from 'react';
import { Provider } from 'react-redux';
import { Store } from 'redux';
import { testRender } from '../../test';
import AboutDialog from './AboutDialog';
it('should close when the button is clicked', () => {
const store = {
getState: jest.fn(() => ({ licenses: { list: null } })),
dispatch: jest.fn(),
subscribe: jest.fn(),
} as unknown as Store;
const i18n = new I18nManager({ locale: 'en' });
const close = jest.fn();
render(
<Provider store={store}>
<I18nContext.Provider value={i18n}>
<AboutDialog isOpen={true} onClose={() => close()} />
</I18nContext.Provider>
</Provider>,
);
userEvent.click(screen.getByLabelText('Close'));
const dialog = testRender(<AboutDialog isOpen={true} onClose={() => close()} />);
userEvent.click(dialog.getByLabelText('Close'));
expect(close).toHaveBeenCalled();
});
it('should manage license dialog open/close', async () => {
const store = {
getState: jest.fn(() => ({ licenses: { list: null } })),
dispatch: jest.fn(),
subscribe: jest.fn(),
} as unknown as Store;
const i18n = new I18nManager({ locale: 'en' });
render(
<Provider store={store}>
<I18nContext.Provider value={i18n}>
<AboutDialog isOpen={true} onClose={() => undefined} />
</I18nContext.Provider>
</Provider>,
);
const dialog = testRender(<AboutDialog isOpen={true} onClose={() => undefined} />);
userEvent.click(screen.getByText('Software Licenses'));
userEvent.click(dialog.getByText('Software Licenses'));
expect(
screen.getByText(
dialog.getByText(
`${process.env.REACT_APP_NAME} is built on open source software.`,
{
exact: false,
@@ -67,7 +37,7 @@ it('should manage license dialog open/close', async () => {
userEvent.click(getByLabelText(licenseDialog, 'Close'));
await waitForElementToBeRemoved(() =>
screen.queryByText(
dialog.queryByText(
`${process.env.REACT_APP_NAME} is built on open source software.`,
{
exact: false,