单元测试 – 在React 0.14中单元测试中如何检查DOM节点的道具?

升级React从0.13升级到v0.14.0-beta3后,我在单元测试中收到了很多警告:
Warning: ReactDOMComponent: Do not access .props of a DOM node; instead,recreate the props as `render` did originally or read the DOM properties/attributes directly from this node (e.g.,this.refs.Box.className). This DOM node was rendered by `Button`.

它们是由我的单元测试引起的,例如:

it('should render to a <a> when href is given',function () {
    var button = TestUtils.renderIntodocument(<Button className="amazon" href="#">Hello</Button>);
    expect(TestUtils.scryRenderedDOMComponentsWithTag(button,'button').length).toBe(0);
    expect(TestUtils.scryRenderedDOMComponentsWithTag(button,'a').length).toBe(1);
    expect(TestUtils.scryRenderedDOMComponentsWithTag(button,'a')[0].props.href).toBe('#');
    expect(TestUtils.scryRenderedDOMComponentsWithTag(button,'a')[0].props.className).toBe('amazon Button');
});

我该如何解决?有没有推荐的这样测试DOM元素的做法?

在调试器中,我发现这些元素(在我的例子中就是TestUtils.scryRenderedDOMComponentsWithTag(button,’a’)[0])实际上是添加了一些React属性的DOM元素(如道具和状态).

所以现在,有了这个知识,我可以根据DOM属性属性编写我的断言,如:

expect(b.getAttribute('href')).toEqual('#');

相关文章

一、前言 在组件方面react和Vue一样的,核心思想玩的就是组件...
前言: 前段时间学习完react后,刚好就接到公司一个react项目...
前言: 最近收到组长通知我们项目组后面新开的项目准备统一技...
react 中的高阶组件主要是对于 hooks 之前的类组件来说的,如...
我们上一节了解了组件的更新机制,但是只是停留在表层上,例...
我们上一节了解了 react 的虚拟 dom 的格式,如何把虚拟 dom...