学习玩笑和酶以测试反应应用程序,使用create-react-app启动。
在unmount
之后使用shallow
模拟添加/ removeEventListener崩溃,并在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();
});
在unmount
后使用mount
:
console.error node_modules/fbjs/lib/warning.js:33
Warning: Can only update a mounted or mounting component. This usually means you called setState, replaceState, or forceUpdate on an unmounted component. This is a no-op.
Please check the code for the App component.
在unmount
后使用shallow
:
/home/nik/projects/learn/jest/node_modules/react-scripts/scripts/test.js:20
throw err;
^
Invariant Violation: ReactShallowRenderer render(): Invalid component element.
at invariant (/home/nik/projects/learn/jest/node_modules/fbjs/lib/invariant.js:42:15)
at ReactShallowRenderer.render (/home/nik/projects/learn/jest/node_modules/enzyme-adapter-react-16/node_modules/react-test-renderer/cjs/react-test-renderer-shallow.development.js:104:38)
at Updater.enqueueSetState (/home/nik/projects/learn/jest/node_modules/enzyme-adapter-react-16/node_modules/react-test-renderer/cjs/react-test-renderer-shallow.development.js:329:20)
at App.Object.<anonymous>.Component.setState (/home/nik/projects/learn/jest/node_modules/react/cjs/react.development.js:237:16)
at loadData.then.results (/home/nik/projects/learn/jest/src/App.js:15:12)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
error An unexpected error occurred: "Command failed.
Exit code: 1
Command: sh
Arguments: -c react-scripts test --env=jsdom
Directory: /home/nik/projects/learn/jest
Output:
".
info If you think this is a bug, please open a bug report with the information provided in "/home/nik/projects/learn/jest/yarn-error.log".
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
答案 0 :(得分:1)
我把unmount挂在了诺言
return Promise.resolve().then(() => {
wrapper.unmount();
expect(remover).toHaveBeenCalled();
});
也错过了错误的线索,'loadData' - 没有嘲笑:
jest.spyOn(App.prototype, 'loadData')
.mockImplementation(() => {
return new Promise(resolve => resolve([topics, subTopics]))
});
答案 1 :(得分:0)
要解决第一个问题,您必须采取措施避免在卸载组件后调用_isMounted
。
更简单的方法是在调用setState之前保留componentDidMount() {
this._isMounted = true;
this.loadData().then(results => {
if (!this._isMounted) {
return;
}
const [topics, subTopics, ...rest] = results;
this.setState({ topics, subTopics });
});
window.addEventListener('resize', this.someHandler);
}
componentWillUnmount() {
this._isMounted = false;
window.removeEventListener('resize', this.someHandler);
}
标志,然后检查:
footer {
position: absolute;
left: 0;
bottom: 0;
width: 100%;
min-height: 50px;
line-height: 50px;
text-align: center;
margin-top: auto;
margin-bottom: auto;
color: white;
background-color: #232323;
}
footer a {
cursor: pointer;
}
footer a:hover {
text-decoration: underline;
}
footer a:not(:last-child)::after {
content: " | ";
cursor: default;
}
您可以阅读有关此问题的更多信息,并在React博客中找到另一种解决方案:https://reactjs.org/blog/2015/12/16/ismounted-antipattern.html