4

我正在尝试测试一个呈现索环 Menu组件的组件。grommetMenu组件将一个绝对定位的菜单呈现到文档的顶层,作为子级插入到body. 因此,它呈现在包装器的范围之外。我可以通过document.body.innerHTML(参考 jsdom 文档)找到它,但我想使用酶与它进行交互。有什么建议吗?

我的测试:

const wrapper = mount(
    <MyComponent checkThis={checkThisSpy} />
);
wrapper.find('.spec-menu').simulate('click');
console.log(document.body.innerHTML); // Shows the absolute menu inserted into the body

执行此操作的索环中的线document.body.insertBefore(drop.container, document.body.firstChild);https://github.com/grommet/grommet/blob/master/src/js/utils/Drop.js#L197

只是在寻找有关处理此问题的最佳方法的一些指导。谢谢!

4

1 回答 1

3

最终,我们无法找到任何方法来测试使用酶本身的成分。因为我们使用 jsdom 来提供 dom,document所以是全局可用的。我们最终写了一些看起来像的期望

expect(document.getElementsByClassName('my-top-level')).to.have.length(1)

普通的 dom API 足以测试我们想要的一切,只是有点笨拙。

于 2017-06-14T19:01:52.303 回答