Reputation: 453
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
Reputation: 196
jest.mock('redux-form', () => ({
getFormValues: jest.fn(() => () => mockWorkActivityFormValues),
}));
Upvotes: 0
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