最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to mock the elements of react-hook-form when testing with react-testing-library? - Stack Overflow

programmeradmin3浏览0评论

Having a basic ponent which uses react-hook-form:

const { handleSubmit, reset, control } = useForm({ resolver: yupResolver(schema) });

...

  <MyComponent
    title='title'
    open={isOpened}
    control={control}
  />

This ponent has 3 props, title - a string, open - a function, control - no idea what is it, all of them mandatory.

So, when writing a test for it I got stuck here:

import { render } from '@testing-library/react';

import '@testing-library/jest-dom';
import MyComponent from './my-ponent';

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

How can control be mocked for this test?

Having a basic ponent which uses react-hook-form:

const { handleSubmit, reset, control } = useForm({ resolver: yupResolver(schema) });

...

  <MyComponent
    title='title'
    open={isOpened}
    control={control}
  />

This ponent has 3 props, title - a string, open - a function, control - no idea what is it, all of them mandatory.

So, when writing a test for it I got stuck here:

import { render } from '@testing-library/react';

import '@testing-library/jest-dom';
import MyComponent from './my-ponent';

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

How can control be mocked for this test?

Share Improve this question asked Dec 21, 2021 at 22:53 Leo MessiLeo Messi 6,18622 gold badges78 silver badges153 bronze badges 1
  • Where does control e from? You should at least provide where the control variable is defined or imported from what module – Lin Du Commented Dec 22, 2021 at 2:00
Add a ment  | 

3 Answers 3

Reset to default 8

If anyone is getting any error while using hooks inside tests, try renderHook from testing-library/react:

import { render, renderHook } from '@testing-library/react'        

const { result } = renderHook(() => useForm())

render(
  <TextField control={result.current.control} />
)

Maybe just passing in the real control from useForm, like they are doing in react-hook-form's testing. https://github./react-hook-form/react-hook-form/blob/master/src/__tests__/useController.test.tsx

control came from useForm:

const { control } = useForm();

Control is necessary when you use Controller or useController The doc: https://react-hook-form./api/usecontroller

I suppose the TaskModal ponent is in a form. I remend to put the form inside the modal and it would be easier to test otherwise you can wrap your ponent (TaskModal) with a form for your test.

发布评论

评论列表(0)

  1. 暂无评论