如果我使用mocked store渲染redux组件,酶浅渲染会以一种意想不到的方式出现。
我有一个简单的测试,看起来像这样:
import React from 'react';
import { shallow } from 'enzyme';
import { createMockStore } from 'redux-test-utils';
import Test from './Test'
it('should render ', () => {
const testState = {
app: {
bar: ['a', 'b', 'c']
}
};
const store = createMockStore(testState)
const context = {
store,
};
const shallowComponent = shallow(<Test items={[]}/>, {context});
console.log(shallowComponent.debug());
}Test组件如下所示:
class Test extends React.Component {
constructor(props) {
super(props);
}
render() {
return(
<div className="here"/>
)
}
}
export default Test;不出所料,它会打印出以下内容:
<div className="here" />但是,如果我的组件是redux组件:
class Test extends React.Component {
constructor(props) {
super(props);
}
render() {
return(
<div className="here"/>
)
}
}
const mapStateToProps = state => {
return {
barData: state.app.bar
}
}
export default connect(
mapStateToProps
)(Test)然后我在控制台中得到的是:
<BarSeriesListTest items={{...}} barData={{...}} dispatch={[Function]} />为什么会有这样的差异呢?我如何在我的redux版本中测试我的组件是否嵌入了<div className="here"/>?
发布于 2017-10-31 21:43:30
默认情况下,将导出连接的构件。您可以做的是导入未连接到redux的组件。
import { Test } from './Test';那么你的测试应该可以工作了。
https://stackoverflow.com/questions/46875547
复制相似问题