2

我目前正在开玩笑测试 React 组件,组件的内联样式会根据不同的 props 值进行更改。

这是我想做的一个例子:

let firstChild = TestUtils.findRenderedDOMComponentWithTag(renderedComponent, 'div');
    expect(firstChild.getDOMNode().style).toEqual({
      fontSize: '20px'
    });

这是组件道具:

let renderedComponent = TestUtils.renderIntoDocument(
      <CircleIcon
        size="small" />

这是要测试的组件 dom:

return (
      <div className="circle-icon" style={boxStyle}>
        <span className={this.props.icon}></span>
      </div>
    );

如果我能得到 boxStyle 里面的内容,我可以从中断言测试结果。

非常感谢!

4

1 回答 1

5

实际上它有效。

用法:

firstChild.getDOMNode().style.backgroundColor
于 2015-08-25T09:31:47.860 回答