gpt4 book ai didi

react-redux - Jest + enzyme : test Redux-form

转载 作者:行者123 更新时间:2023-12-03 14:49:48 26 4
gpt4 key购买 nike

我的应用程序有很多 redux-form。我正在使用 Jest 和 Enzyme 进行单元测试。但是,我无法测试 redux-form。我的组件是一个登录表单,如:

import { login } from './actions';
export class LoginForm extends React.Component<any, any> {

onSubmit(values) {
this.props.login(values, this.props.redirectUrl);
}

render() {
const { handleSubmit, status, invalid } = this.props;

return (
<form onSubmit={handleSubmit(this.onSubmit.bind(this))}>
<TextField label="Email" name="email">
<TextField type="password" label="Password" name="password" autoComplete/>
<Button submit disabled={invalid} loading={status.loading}>
OK
</Button>
</form>
);
}
}

const mapStateToProps = (state) => ({
status: state.login.status,
});

const mapDispatchToProps = { login };

const form = reduxForm({ form: 'login' })(LoginForm);

export default connect(mapStateToProps, mapDispatchToProps)(form);

模拟商店,导入连接组件
redux-form使用 store 来维护表单输入。然后我使用 redux-mock-store :

import ConnectedLoginForm from './LoginForm';

const configureStore = require('redux-mock-store');
const store = mockStore({});
const spy = jest.fn();

const wrapper = shallow(
<Provider store={store}>
<ConnectedLoginForm login={spy}/>
</Provider>);

wrapper.simulate('submit');
expect(spy).toBeCalledWith();

但这样一来, submit没有模拟,我的测试用例失败了:

Expected mock function to have been called with: [] But it was not called.



模拟商店,仅导入 React 组件。

我尝试从测试代码创建 redux 表单:

import { Provider } from 'react-redux';
import ConnectedLoginForm, { LoginForm } from './LoginForm';

const props = {
status: new Status(),
login: spy,
};
const ConnectedForm = reduxForm({
form: 'login',
initialValues: {
email: 'test@test.com',
password: '000000',
},
})(LoginForm);

const wrapper = shallow(
<Provider store={store}>
<ConnectedForm {...props}/>
</Provider>);

console.log(wrapper.html());

wrapper.simulate('submit');
expect(spy).toBeCalledWith({
email: 'test@test.com',
password: '000000',
});

在这种情况下,我仍然收到 function not called 的错误.如果我添加 console.log(wrapper.html()) ,我有错误:

Invariant Violation: Could not find "store" in either the context or props of "Connect(ConnectedField)". Either wrap the root component in a , or explicitly pass "store" as a prop to "Connect(ConnectedField)".



我无法在 redux-form 或 redux 或 jest/enzyme 的官方网站上找到文档,甚至在 Google 上也找不到文档。请帮忙,谢谢。

最佳答案

我使用了真正的商店(因为 redux-mock-store 不支持 reducer )和 redux-form 的 reducer ,它对我有用。代码示例:


import { createStore, Store, combineReducers } from 'redux';
import { Provider } from 'react-redux';
import { reducer as formReducer } from 'redux-form';

const rootReducer = combineReducers({
form: formReducer,
});

let store;

describe('Redux Form', () => {

beforeEach(() => {
store = createStore(rootReducer);
});

it('should submit form with form data', () => {
const initialValues = {...};
const onSubmit = jest.fn();
const wrapper = mount(
<Provider store={store}>
<SomeForm
onSubmit={onSubmit}
initialValues={initialValues}
/>
</Provider>
);

const form = wrapper.find(`form`);
form.simulate('submit');

const expectedFormValue = {...};
expect(onSubmit).toHaveBeenCalledTimes(1);
expect(onSubmit.mock.calls[0][0]).toEqual(expectedFormValue);
});

});

关于react-redux - Jest + enzyme : test Redux-form,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49665349/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com