加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 20:42:28 所属栏目:百科 来源:网络整理
导读:升级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 t
升级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('#');

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读