我在测试时收到有关行为的错误消息。
警告:测试中对EmployeesDashboard 的更新未包含在act(...) 中。
When testing, code that causes React state updates should be wrapped into act(...): act(() => { /* fire events that update state */ }); /* assert on the output */ in EmployeesDashboard (at EmployeesDashboard.test.tsx:69)"
我不知道为什么,因为似乎我已经将所有内容都包装在了行动中……我还应该在这里包装什么?任何帮助将不胜感激,因为我对测试还很陌生。
import React from 'react';
import { mocked } from 'ts-jest/utils';
import { act } from 'react-dom/test-utils';
import { EmployeesDashboard } from '../EmployeesDashboard';
import {
render,
wait,
waitForElementToBeRemoved,
} from '@testing-library/react';
import { Employee } from '@neurocann-ui/types';
import { getAll } from '@neurocann-ui/api';
jest.mock('@neurocann-ui/api');
const fakeEmployees: Employee[] = [
{
email: 'joni@gmail.com',
phone: '720-555-555',
mailingAddress: {
type: 'home',
streetOne: '390 Blake St',
streetTwo: 'Apt 11',
city: 'Denver',
state: 'CO',
zip: '90203',
},
name: 'Joni Baez',
permissions: ['Employee'],
hireDate: new Date('2020-09-19T05:13:12.923Z'),
},
{
email: 'joni22@gmail.com',
phone: '720-555-555',
mailingAddress: {
type: 'home',
streetOne: '390 Blake St',
streetTwo: 'Apt 11',
city: 'Denver',
state: 'CO',
zip: '90203',
},
name: 'Joni Baez',
permissions: ['Employee'],
hireDate: new Date('2020-09-19T05:13:12.923Z'),
},
];
mocked(getAll).mockImplementation(
(): Promise<any> => {
return Promise.resolve({ data: fakeEmployees });
},
);
describe('EmployeesDashboard', () => {
it('renders progress loader', () => {
act(() => {
render(<EmployeesDashboard />)
});
const employeesDashboard = document.getElementById(
'simple-circular-progress',
);
expect(employeesDashboard).toBeInTheDocument();
expect(employeesDashboard).toMatchSnapshot();
});
it('renders a table', async () => {
await act(() => { render(<EmployeesDashboard />) });
const table = document.getElementById('employees-table');
expect(table).toBeInTheDocument();
});
});