我正在尝试测试我的简单组件(先前的切换,今天在material-ui库中名为Switch)。
我将其包裹为:
class AutoRefreshSwitch extends React.Component {
constructor(props) {
super(props);
this.input = null;
}
handleChange = () => {
console.log('handler ')
this.props.onAutoRefreshClick(!this.props.autoRefreshStatus);
};
render() {
const {classes} = this.props;
return (
<FormControlLabel
control={
<Switch
checked={this.props.autoRefreshStatus}
onChange={this.handleChange}
color="primary"
classes={{
switchBase: classes.switchBase,
checked: classes.checked,
colorPrimary: classes.colorPrimary,
bar: classes.bar,
icon: classes.icon,
root: classes.root,
}}
disableRipple
inputProps={{id: "switch12345"}}
/>
}
label="Auto refresh"
classes={{label: classes.label}}
/>
);
}
}
export default withStyles(styles)(AutoRefreshSwitch);
此组件的放置方式如下:
<Container> -> it has mapToState and mapToProps with this onAutoRefreshClick which is passed as a prop to component and then to AutoRefreshSwitch
<Component>
<AutoRefreshSwitch onAutoRefreshClick={onAutoRefreshClick}
autoRefreshStatus={autoRefreshStatus}
/>
现在我的测试是:
import {applyMiddleware, combineReducers, createStore} from 'redux';
import thunk from 'redux-thunk';
import React from 'react';
import {Provider} from 'react-redux';
import {configure, mount} from 'enzyme';
import {myReducer} from "../../src/reducers/overview";
import AutoRefreshSwitch from "../../src/blahblah/auto-refresh-switch";
import Adapter from 'enzyme-adapter-react-16';
import {setAutoRefreshStatus} from "../../src/actions/overview";
// from https://medium.freecodecamp.org/real-integration-tests-with-react-
// redux-and-react-router-417125212638
export function setupIntegrationTest(reducers, initialRouterState = {}) {
const dispatchSpy = jest.fn(() => ({}));
const reducerSpy = (state, action) => dispatchSpy(action);
const emptyStore = applyMiddleware(thunk)(createStore);
const combinedReducers = combineReducers({
reducerSpy,
...reducers,
});
const store = emptyStore(combinedReducers);
return { store, dispatchSpy };
}
configure({adapter: new Adapter()});
describe('integration tests', () => {
let store;
let dispatchSpy;
let wrapper;
beforeEach(() => {
({store, dispatchSpy} = setupIntegrationTest({myReducer}));
wrapper = mount(
<Provider store={store}>
<AutoRefreshSwitch onAutoRefreshClick={setAutoRefreshStatus}
autoRefreshStatus={store.getState().myReducer.autoRefreshStatus}
/>
</Provider>)
});
it('should change the status', () => {
wrapper.find('#switch12345').simulate('change');
wrapper.update();
expect(store.getState().myReducer.autoRefreshStatus).toBe(false)
});
});
现在的问题是,代码将转到AutoRefreshSwitch中的handleChange,但不会调用其余代码(未触发this.props.onAutoRefreshClick)
我想知道是否是因为我没有安装AutoRefreshSwitch的父母。
这应该是受
启发的集成测试在此先感谢您的帮助:)
答案 0 :(得分:0)
其中缺少调度程序
beforeEach(() => {
({store, dispatchSpy} = setupIntegrationTest({myReducer}));
wrapper = mount(
<Provider store={store}>
<AutoRefreshSwitch onAutoRefreshClick={() => store.dispatch(setAutoRefreshStatus)}
autoRefreshStatus={store.getState().myReducer.autoRefreshStatus}
/>
</Provider>)
});