PLee
PLee

Reputation: 453

How to mock getFormValues for redux form

I am using the following code to test my action creator. Because my action creator uses store state, I need to mock the getFormValues and isValid. However, it will throw

TypeError: (0 , _reduxForm.getFormValues)(...) is not a function.

Can anyone help me with this?

import * as reduxForm from 'redux-form';
it('creates SAVE_PROPERTY when saving a property', () => {
  reduxForm.getFormValues = jest.fn(() => ({ field1: 'test', field2: 'test2' }));
  reduxForm.isValid = jest.fn(() => false);

  store.dispatch(saveProperty(1));
  expect(store.getActions()).toMatchSnapshot();
  reduxForm.getFormValues.mockRestore();
  reduxForm.isValid.mockRestore();
});

Upvotes: 3

Views: 771

Answers (2)

Konstantin Yarish
Konstantin Yarish

Reputation: 196

jest.mock('redux-form', () => ({
  getFormValues: jest.fn(() => () => mockWorkActivityFormValues),
}));

Upvotes: 0

PLee
PLee

Reputation: 453

After thinking this for a long time, I got a solution. Hope it can help others

it("creates SAVE_PROPERTY when saving a property and form values are missing", () => {
    reduxForm.getFormValues = jest.fn(() => () => ({
      field1: "test",
      field2: "test2"
    }));
    reduxForm.isValid = jest.fn(() => () => false);

    store.dispatch(saveProperty(1));
    expect(store.getActions()).toMatchSnapshot();
    reduxForm.getFormValues.mockRestore();
    reduxForm.isValid.mockRestore();
  });

Upvotes: 1

Related Questions