【发布时间】:2018-05-26 18:31:02
【问题描述】:
我正在尝试对组件进行简单的“浅层”测试:
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('') 它会通过。到目前为止,每一步都给我带来了问题,但在这里我找不到任何有效的解决方案。关于这个或更好的测试 React 组件的方法有什么建议吗?
【问题讨论】: