我有一个功能组件,我想用模拟功能测试它(简化演示)
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 函数?