【问题标题】:How can I access inline style using React findDomNode function?如何使用 React findDomNode 函数访问内联样式?
【发布时间】:2015-11-18 20:46:49
【问题描述】:

我目前正在测试 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 里面的内容,我可以从中断言测试结果。

非常感谢!

【问题讨论】:

  • 你试过了吗?你的结果是什么?
  • 它现在可以工作了,只是有环境问题

标签: unit-testing reactjs jestjs reactjs-testutils


【解决方案1】:

确实有效。

用法:

firstChild.getDOMNode().style.backgroundColor

【讨论】:

  • 不要说谎
  • :“元素”类型上不存在属性“样式”。
猜你喜欢
  • 2017-06-15
  • 1970-01-01
  • 1970-01-01
  • 2016-05-12
  • 1970-01-01
  • 2021-05-18
  • 1970-01-01
相关资源
最近更新 更多