【发布时间】:2018-04-03 04:28:18
【问题描述】:
如果我使用模拟存储渲染 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"/>?
【问题讨论】:
-
这可能会对您有所帮助 github.com/reactjs/redux/issues/1534 。向下滚动,您会看到一个带有很多 + 的评论
标签: reactjs react-redux enzyme