fix some test console errors

The react testing library prints errors to the console about not using
act(). This adds fixes/workarounds for most cases.
This commit is contained in:
David Lechner
2023-05-17 16:33:47 -05:00
committed by David Lechner
parent bf4670b25d
commit c733881815
6 changed files with 51 additions and 11 deletions
+13 -1
View File
@@ -1,11 +1,18 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2021-2022 The Pybricks Authors
// Copyright (c) 2021-2023 The Pybricks Authors
import { cleanup } from '@testing-library/react';
import React from 'react';
import { testRender } from '../../test';
import App from './App';
jest.mock('react', () => {
const React = jest.requireActual('react');
// don't lazy-load, just use fallback for React.Suspense
React.Suspense = ({ fallback }: Record<string, unknown>) => fallback;
return React;
});
beforeAll(() => {
// this lets us use jest.spyOn with window.innerWidth
const defaultInnerWidth = window.innerWidth;
@@ -14,6 +21,11 @@ beforeAll(() => {
});
});
beforeEach(() => {
// prevent tour popup
localStorage.setItem('tour.showOnStartup', 'false');
});
afterEach(() => {
cleanup();
jest.resetAllMocks();
+9 -9
View File
@@ -68,7 +68,7 @@ describe('Toolbar', () => {
const { button1, button2, button3 } = getButtons(toolbar);
button1.focus();
act(() => button1.focus());
await act(() => user.keyboard('{ArrowRight}'));
expect(button2).toHaveFocus();
@@ -82,7 +82,7 @@ describe('Toolbar', () => {
const { button1, button2, button3 } = getButtons(toolbar);
button3.focus();
act(() => button3.focus());
await act(() => user.keyboard('{ArrowLeft}'));
expect(button2).toHaveFocus();
@@ -96,7 +96,7 @@ describe('Toolbar', () => {
const { button1, button2, button3 } = getButtons(toolbar);
button3.focus();
act(() => button3.focus());
await act(() => user.keyboard('{ArrowRight}'));
expect(button1).toHaveFocus();
@@ -110,7 +110,7 @@ describe('Toolbar', () => {
const { button1, button2, button3 } = getButtons(toolbar);
button1.focus();
act(() => button1.focus());
await act(() => user.keyboard('{ArrowLeft}'));
expect(button3).toHaveFocus();
@@ -124,7 +124,7 @@ describe('Toolbar', () => {
const { button1, button2, button3 } = getButtons(toolbar);
button3.focus();
act(() => button3.focus());
await act(() => user.keyboard('{Home}'));
expect(button1).toHaveFocus();
@@ -138,7 +138,7 @@ describe('Toolbar', () => {
const { button1, button2, button3 } = getButtons(toolbar);
button1.focus();
act(() => button1.focus());
await act(() => user.keyboard('{End}'));
expect(button3).toHaveFocus();
@@ -152,7 +152,7 @@ describe('Toolbar', () => {
const { button1, button2, button3 } = getButtons(toolbar);
button2.focus();
act(() => button2.focus());
await act(() => user.keyboard('{ArrowUp}'));
expect(button2).toHaveFocus();
@@ -166,7 +166,7 @@ describe('Toolbar', () => {
const { button1, button2, button3 } = getButtons(toolbar);
button2.focus();
act(() => button2.focus());
await act(() => user.keyboard('{ArrowDown}'));
expect(button2).toHaveFocus();
@@ -180,7 +180,7 @@ describe('Toolbar', () => {
const { button1, button2, button3 } = getButtons(toolbar);
button2.focus();
act(() => button2.focus());
await act(() => user.tab());
expect(document.body).toHaveFocus();
+13
View File
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2022-2023 The Pybricks Authors
import { AbstractPureComponent2 } from '@blueprintjs/core';
import { act, cleanup, getByLabelText, waitFor } from '@testing-library/react';
import React from 'react';
import { testRender } from '../../test';
@@ -8,6 +9,18 @@ import { firmwareInstallPybricks } from '../firmware/actions';
import { firmwareRestoreOfficialDialogShow } from '../firmware/restoreOfficialDialog/actions';
import Settings from './Settings';
beforeEach(() => {
// this avoids react testing lib errors about not using act() by running
// callbacks immediately instead of deferring
jest.spyOn(
AbstractPureComponent2.prototype,
'requestAnimationFrame',
).mockImplementation((callback) => {
callback();
return () => undefined;
});
});
afterEach(() => {
cleanup();
jest.resetAllMocks();
+5 -1
View File
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2020-2022 The Pybricks Authors
// Copyright (c) 2020-2023 The Pybricks Authors
// jest-dom adds custom jest matchers for asserting on DOM nodes.
// allows you to do things like:
@@ -15,6 +15,10 @@ import {
} from '@blueprintjs/core/lib/cjs/components/hotkeys/hotkeyParser';
// @ts-expect-error no typings
import matchMediaPolyfill from 'mq-polyfill';
import { config } from 'react-transition-group';
// avoid react testing library errors about not using act()
config.disabled = true;
jest.mock('./fileStorage/hooks');