我有一个使用
DateTimePicker
库中的 react-native-ui-datepicker
的组件。在使用 jest
为其编写测试用例时,它显示以下错误
Warning: React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object.
Check the render method of `SingleDatePicker`.
at onChange (/Users/**/DateTimePicker/SingleDatePicker.tsx:9:3)
18 | testID="single-date-picker"
19 | >
> 20 | <DateTimePicker
| ^
21 | mode={'single'}
22 | date={date}
23 | onChange={onChange}
at printWarning (node_modules/react/cjs/react-jsx-runtime.development.js:87:30)
at error (node_modules/react/cjs/react-jsx-runtime.development.js:61:7)
我的测试用例是,
import React from 'react';
import dayjs from 'dayjs';
import { render } from '@testing-library/react-native';
import DatePicker from './DatePicker';
import { screen } from '@testing-library/react-native';
jest.mock('react-native-ui-datepicker', () => ({
default: jest.fn(),
}));
const mockOnChange = jest.fn();
describe('Testing DatePicker', () => {
const date = dayjs().format();
it('Testing DatePicker', () => {
render(
<DatePicker type="single" date={date} onChange={mockOnChange} />
);
expect(screen.queryByTestId('single-date-picker')).toBeTruthy();
});
});
如何解决这个错误?
如下所述嘲笑
react-native-ui-datepicker
为我解决了这个问题。
jest.mock('react-native-ui-datepicker', () => {
const Picker = () => <div />;
return Picker;
});