学习玩笑和酶来测试 React 应用程序,使用 create-react-app 引导。
惩戒添加/ removeEventListener崩溃使用unmount后shallow,用在警告unmount后mount,见下文。有谁知道我做错了什么?
我的测试:
it('should add and remove resize event handler', () => {
const adder = jest
.spyOn(global, 'addEventListener')
.mockImplementation(() => {});
const remover = jest
.spyOn(global, 'removeEventListener')
.mockImplementation(() => {});
const wrapper = shallow(<App />);
// this seems to work
expect(adder).toHaveBeenCalled();
// causing issues
wrapper.unmount();
expect(remover).toHaveBeenCalled();
});
Run Code Online (Sandbox Code Playgroud)
使用unmount后mount:
console.error node_modules/fbjs/lib/warning.js:33
Warning: Can only update a mounted or mounting component. This usually means …Run Code Online (Sandbox Code Playgroud)