【问题标题】:React Testing Library: testing props sent to mocked child componentReact 测试库:测试发送到模拟子组件的道具
【发布时间】:2020-07-24 17:21:06
【问题描述】:

使用 jest 和 react 测试库。

这似乎很重要,但我很想念它。你如何模拟一个子组件,但测试父组件是否使用正确的道具调用它?在这种情况下,我想测试 parent.js 并模拟出子组件。

父.js

import Child from './child'
const Parent = (props) => {
  const {firstname} = props;

  <Child name={firstname} />
}

test.js

import Parent from '../parent'
jest.mock('./child');

it('should render properly'), () => {
   render(<Parent firstname='bob'/>);
   expect('child').toBeCalledWith({ name: 'bob' });
}

【问题讨论】:

    标签: reactjs jestjs mocking react-testing-library


    【解决方案1】:

    您应该使用 jest.fn() 模拟 Child 模块并测试调用它的道具。

    这可能会有所帮助: https://dev.to/d_ir/verifying-children-passed-to-react-mock-components-2mf9

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-10
      • 2021-12-03
      • 2021-10-01
      • 1970-01-01
      • 2017-01-10
      • 1970-01-01
      • 2022-01-20
      • 2019-12-10
      相关资源
      最近更新 更多