从此文档:https://reactjs.org/docs/hooks-faq.html#how-to-test-components-that-use-hooks
文档提供了一种设置和拆卸测试的方法,如下所示:
let container;
beforeEach(() => {
container = document.createElement('div');
document.body.appendChild(container);
});
afterEach(() => {
document.body.removeChild(container);
container = null;
});
从此文档:https://reactjs.org/docs/testing-recipes.html#setup--teardown
安装和拆卸方式如下:
import { unmountComponentAtNode } from "react-dom";
let container = null;
beforeEach(() => {
// setup a DOM element as a render target
container = document.createElement("div");
document.body.appendChild(container);
});
afterEach(() => {
// cleanup on exiting
unmountComponentAtNode(container);
container.remove();
container = null;
});
我有点困惑哪一种是拆解测试的最佳方法?
unmountComponentAtNode
+ dom.remove()
还是 document.body.removeChild
?
更新
这两个官方文档在teardown测试的时候给出了这两种方式,是不是说明这两种方式都可以呢?它们相等吗?或者,什么?
最佳答案
unmountComponentAtNode
将调用 componentWillUnmount
生命周期方法,但 document.body.removeChild
不会。
关于javascript - 拆卸测试时使用 unmountComponentAtNode 或 document.body.removeChild ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58637602/