Anh*_*yen 10 testing reactjs enzyme
我有以下示例组件
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>
);
}
}
Run Code Online (Sandbox Code Playgroud)
使用酶和柴我想断言,为
this.props.active = true
Run Code Online (Sandbox Code Playgroud)
backgroundColor为绿色,并选中复选框.
这是我的测试用例
describe('<Header />', () => {
it('valid component', () => {
const wrapper = shallow(<ProfileHeader
active= {true}
/>);
????
});
Run Code Online (Sandbox Code Playgroud)
但我怎么能断言这两种情况呢?
ano*_*oop 14
检查下面的解决方案,了解可以使用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');
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11817 次 |
| 最近记录: |