Oli*_*ins 5 reactjs enzyme react-redux
如果我使用模拟商店渲染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());
}
Run Code Online (Sandbox Code Playgroud)
Test组件如下所示:
class Test extends React.Component {
constructor(props) {
super(props);
}
render() {
return(
<div className="here"/>
)
}
}
export default Test;
Run Code Online (Sandbox Code Playgroud)
正如预期的那样打印出来:
<div className="here" />
Run Code Online (Sandbox Code Playgroud)
但是,如果我的组件是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)
Run Code Online (Sandbox Code Playgroud)
然后我在控制台得到的是:
<BarSeriesListTest items={{...}} barData={{...}} dispatch={[Function]} />
Run Code Online (Sandbox Code Playgroud)
为什么会出现这种差异?如何<div className="here"/>在我的redux版本的组件中测试我的组件是否已嵌入其中?
| 归档时间: |
|
| 查看次数: |
269 次 |
| 最近记录: |