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

标签 react-redux jestjs jsx redux-form enzyme

我的应用程序有很多 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/

相关文章:

reactjs - React-Redux @connect 语法错误

reactjs - Jest - 模拟函数,从另一个文件导入

javascript - 如何在使用 Enzyme、Jest + Create React App 的测试中获取窗口值

reactjs - React Router 总是将我重定向到不同的 url

javascript - react : Access ref passed between components via render props

javascript - 插画脚本: How to style when injecting text?

javascript - react /归零 : TypeError: Cannot read property 'target' of undefined

reactjs - 如何为 Material-ui 的组件设置主浅色/深色?我正在使用像这里这样的自定义主题

jestjs - 如何在 Windows 10 上修复 Jest "No Tests Found"?

javascript - react + redux 中 reducer 的好策略?