我试图做一个简单的浅层'测试组件:
class Background extends React.Component{
selectColor = (e) => {
let bgColor = window.getComputedStyle(e.target)
.getPropertyValue("background-color");
this.props.dispatch(actions.set_bg_color(bgColor));
this.next();
}
next = () => {
this.props.history.push('/editor');
}
render(){
return(
<section className="select-background">
<Menu />
<p className="p-select-background">
Select Background
<span className="p-background-message">
(appears if image is too small to fit screen)
</span>
</p>
<div className="bg-color-palette-container">
<div className="bg-color-palette-wrapper">
<div className="bg-color" onClick={this.selectColor}></div>
<div className="bg-color" onClick={this.selectColor}></div>
<div className="bg-color" onClick={this.selectColor}></div>
<div className="bg-color" onClick={this.selectColor}></div>
<div className="bg-color" onClick={this.selectColor}></div>
<div className="bg-color" onClick={this.selectColor}></div>
<div className="bg-color" onClick={this.selectColor}></div>
<div className="bg-color" onClick={this.selectColor}></div>
</div>
</div>
</section>
)
}
}
const mapToState = (state, props) => ({
bgColor: state.bgColor
})
export default connect(mapToState)(Background);
以下是测试:
import React from 'react';
import {shallow} from 'enzyme';
import store from '../store.js';
import Background from './Background';
describe('<Background />', () => {
it('Renders without crashing', () => {
shallow(<Background store={store}/>);
expect(wrapper.hasClass('select-background')).toEqual(true);
});
});
这会失败,但如果我改为.hasClass(&#39;&#39;)则会通过。到目前为止,每一步都给我带来了问题,但在这里我找不到任何有效的解决方案。有关这方面的建议或更好的方法来测试React组件吗?
答案 0 :(得分:2)
解决。不得不在浅包装上使用.dive()。