【发布时间】:2015-11-21 16:11:37
【问题描述】:
在将 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 元素?
【问题讨论】:
-
您不需要像这样测试道具的值,因为您确切知道自己传递的是什么。组件本身不能改变 props 的值,所以 props 永远是你给它的。
-
我正在检查一个子节点的道具(所以不是我刚刚通过的那些),所以这确实有意义。
标签: unit-testing reactjs