我的react应用程序中有这个组件:
export const DEMO = {
test: {
hi: (txt) => console.log(txt)
}
}
export default function App() {
const getAlert = (c) => {
DEMO.test.hi('hello');
}
return (
<div className="App">
<button onClick={getAlert}>open alert</button>
</div>
);
}
it('should trigger calls', () => {
const spyOnFun = jest.spyOn(DEMO.test, 'hi');
const wrapper = shallow(
<App/>,
);
wrapper.find('button').simulate('click');
expect(spyOnFun).toHaveBeenCalledTimes(1); //expect 1 but get 0
});
您如何看到我监视该方法,jest.spyOn(DEMO.test, 'hi');
并且我希望在单击后获得 1 个调用,但是我得到了 0 并且我的测试没有通过。
有什么问题可以解决?