【发布时间】:2022-01-23 06:52:40
【问题描述】:
拥有一个使用 react-hook-form 的基本组件:
const { handleSubmit, 重置, 控制 } = useForm({ 解析器:yupResolver(schema) });
...
<MyComponent
title='title'
open={isOpened}
control={control}
/>
这个组件有 3 个 props,title - 一个字符串,open - 一个函数,control - 不知道是什么,都是必填项。
因此,在为它编写测试时,我陷入了困境:
import { render } from '@testing-library/react';
import '@testing-library/jest-dom';
import MyComponent from './my-component';
describe('MyComponent', () => {
const title = 'title';
it('should render successfully', () => {
const { baseElement, getByText } = render(
<TaskModal
title={title}
open={jest.fn()}
control={} // what should be written here?
/>
);
expect(baseElement).toBeTruthy();
expect(getByText(title)).toBeTruthy();
});
control 怎么能被这个测试模拟?
【问题讨论】:
-
control来自哪里?您至少应该提供控制变量在何处定义或从哪个模块导入
标签: javascript reactjs typescript react-testing-library react-hook-form