升級作出反應,從0.13
到v0.14.0-beta3
後,我得到了很多類似的警告在我的單元測試:如何在React 0.14的單元測試中檢查DOM節點的道具?
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元素的做法?
你不應該需要測試這樣的道具價值,因爲你知道你到底是什麼通過。組件本身不能改變道具的價值,所以道具永遠是你給它的東西。 – Tom
我正在檢查一個子節點的道具(所以不是我剛剛通過的),所以這確實有道理。 – mik01aj