小编use*_*079的帖子

功能组件中的笑话/酶模拟功能

我有一个功能组件,我想用模拟功能测试它(简化演示)

const remove = () => {
  ... do something
}

const removeButton = (props) => (
  <Button onClick={() => remove()}>
    Remove
  </Button>
);
Run Code Online (Sandbox Code Playgroud)

我试过这个测试用例

it('test remove button', () => {
  const test = shallow(<removeButton/>)
  const mockFunction = jest.fn()
  test.instance().remove = mockFunction
  test.find('Button').simulate('click')
  expect(mockFunction).toHaveBeenCalled()
})
Run Code Online (Sandbox Code Playgroud)

.instance().remove 无法模拟该函数,因为它超出了范围。我将如何模拟 remove 函数?

mocking reactjs jestjs enzyme

13
推荐指数
1
解决办法
2万
查看次数

标签 统计

enzyme ×1

jestjs ×1

mocking ×1

reactjs ×1