我想测试我的react login组件,但是还没有调用mock函数:
登录:
export default class LoginPage extends PureComponent {
static propTypes = {
login: PropTypes.func.isRequired,
history: PropTypes.shape({
replace: PropTypes.func.isRequired,
}).isRequired,
};
onSubmit = (e) => {
this.props.login(this.state.username, this.state.password)
.then(() => {
this.props.history.replace('/');
});
};
render() {
return (
<form onSubmit={this.onSubmit}>
...
</form>
);
}
}
Run Code Online (Sandbox Code Playgroud)
我用jest+ enzyme来测试这个:
const props = {
login: jest.fn(() => Promise.resolve('success')),
history: {
replace: jest.fn()
},
};
const wrapper = mount(<LoginPage {...props}/>);
const form = wrapper.find(Form);
const inputs = form.find('input');
const username …Run Code Online (Sandbox Code Playgroud)