在jsdom测试中卸载/销毁组件 [英] Unmount / destroy Component in jsdom test
本文介绍了在jsdom测试中卸载/销毁组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法在jsdom测试中卸载和垃圾收集使用 TestUtils.renderIntoDocument
挂载的React组件?
Is there a way to unmount and garbage collect a React Component that was mounted using TestUtils.renderIntoDocument
inside a jsdom test?
我正在尝试测试 componentWillUnmount
和 TestUtils.renderIntoDocument
上发生的事情要调用的任何DOM节点 React。 unmountComponentAtNode
on。
I'm trying to test something that happens on componentWillUnmount
and TestUtils.renderIntoDocument
doesn't return any DOM node to call React. unmountComponentAtNode
on.
推荐答案
不,但您只需使用 ReactDOM即可。手动渲染
:
var container = document.createElement('div');
ReactDOM.render(<Component />, container);
// ...
ReactDOM.unmountComponentAtNode(container);
这正是 ReactTestUtils无论如何都做了什么,所以如果你需要对容器的引用,没有理由不这样做。
This is exactly what ReactTestUtils does anyway, so there's no reason not to do it this way if you need a reference to the container.
这篇关于在jsdom测试中卸载/销毁组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文