【问题标题】:Enzyme check checkbox status酶检查复选框状态
【发布时间】:2017-03-01 07:01:03
【问题描述】:

我有以下示例组件

export default class Header extends Component{

render(){
  let activeStyle   = {"backgroundColor": "green"};
  let inActiveStyle = {"backgroundColor": "red"};
  return(
    <div className="profile-header" style={(this.props.active)?
      activeStyle:inActiveStyle}>
      <input type="checkbox" checked={this.props.active} readOnly/>
  </div>
 );
}
}

我想使用 Enzyme 和 chai 断言,对于

this.props.active = true 

背景颜色为绿色,复选框被选中。

这是我的测试用例

describe('<Header />', () => {
  it('valid component', () => {
    const wrapper = shallow(<ProfileHeader
    active= {true}
    />);
   ????
});

但是我怎样才能断言这两种情况呢?

【问题讨论】:

    标签: testing reactjs enzyme


    【解决方案1】:

    检查以下解决方案,对于背景颜色,您可以使用 css 字符串选择器检查(可能需要在选择器中进行一些更改)。 以下所有断言都经过测试并且可以正常工作。

    describe('<Header />', () => {
      it('valid component', () => {
        const wrapper = shallow(<ProfileHeader />);
        wrapper.setProps({ active: true });
        let checkbox = wrapper.find({ type: 'checkbox' });
        expect(checkbox.props().checked).to.equal(true);
        expect(wrapper.find('.backgroundColor')).to.equal('green');
        wrapper.setProps({ active: false });
        checkbox = wrapper.find({ type: 'checkbox' });
        expect(checkbox.props().checked).to.equal(false);
        expect(wrapper.find('.backgroundColor')).to.equal('red');
      });
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-18
      相关资源
      最近更新 更多