相关疑难解决方法(0)

如何在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`.
Run Code Online (Sandbox Code Playgroud)

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

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');
});
Run Code Online (Sandbox Code Playgroud)

我该如何解决?是否有任何推荐的测试方法来测试这样的DOM元素?

unit-testing reactjs

10
推荐指数
1
解决办法
5143
查看次数

标签 统计

reactjs ×1

unit-testing ×1