React.jsx:类型无效,需要一个字符串(对于内置组件)或一个类/函数,但得到:object |反应本机用户界面日期选择器

问题描述 投票:0回答:1

我有一个使用

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();
  });
});

如何解决这个错误?

typescript react-native jestjs datepicker
1个回答
0
投票

如下所述嘲笑

react-native-ui-datepicker
为我解决了这个问题。

jest.mock('react-native-ui-datepicker', () => {
  const Picker = () => <div />;
  return Picker;
});
© www.soinside.com 2019 - 2024. All rights reserved.