Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redux form test using jest

I am trying to test redux form submit for some code similar to this file.

https://github.com/marmelab/admin-on-rest/blob/master/src/mui/auth/Login.js

My code is like this

  const middlewares = [];
  const mockStore = configureMockStore(middlewares);

  it("submit button", () => {
    userLogin = jest.fn();
    const initialState = {
      admin: {
        notification: {
          text: "",
          type: "info"
        }
      },
    };
    store = mockStore(initialState);
    tn = label => label;

    const props = {
      submitting: false,
      theme: customTheme,
      translate: tn,
      store,
      location: {
        state: {
          nextPathname: "/"
        }
      },
      userLogin: userLogin
    };
    container = mount(
        <Provider store={store}>
          <TranslationProvider locale="en">
            <Login {...props} />    //Login is connected component
          </TranslationProvider>
      </Provider>
      ,
      {
        context: { store: mockStore(initialState) }
      }
    );
    const username = container.find("TextField").first();
    username.simulate("change", { target: { value: "admin" } });
    const password = container.find("TextField").last();
    password.simulate("change", { target: { value: "Superuser" } });

    const form = container.find("form");
    form.simulate("submit");
    console.log(username.debug());

    expect(userLogin).toHaveBeenCalled();
});

I face two problems.

  1. When I print the username then I dont see the update value that I update through simulate.
  2. Secondly, the expect clause fails. How do I ensure that userLogin function in my code got called.

    Expected mock function to have been called.

like image 556
tariq zafar Avatar asked Oct 25 '17 12:10

tariq zafar


People also ask

How to test Redux application?

Redux can be tested with any test runner, since it's just plain JavaScript. One common option is Jest, a widely used test runner that comes with Create-React-App, and is used by the Redux library repos. If you're using Vite to build your project, you may be using Vitest as your test runner.

How do I test mapStateToProps using jest?

Steps: extract each mapDispatchToProps property as a separate action creator function in another file. extract each mapStateToProps property as a separate selector function in another file. write tests for the selectors and action creators.

How do you test saga?

There are two main ways to test Sagas: testing the saga generator function step-by-step or running the full saga and asserting the side effects.


1 Answers

This is how I test my redux-forms using JEST snapshot testing.

import React from 'react'
import renderer from 'react-test-renderer'
import { createStore } from 'redux'
import { Provider } from 'react-redux'
import YourReduxFormComponent from 'path where it sitting in your project'
import { reduxForm } from 'redux-form'

jest.mock('react-dom')

const spy = jest.fn()
const initialStateValues = {/* initial state values that your form component expects */}
const Decorated = reduxForm({ 
     form: 'testForm', onSubmit: { spy }
})(YourReduxFormComponent)

const formFieldValues = {/*Form field values*/}

it('YourReduxFormComponent renders correctly', () => {
  const store = createStore((state) => state, initialStateValues)
  const tree = renderer.create(
    <Provider store={store}>
      <Decorated
        {...formFieldValues}
      />
    </Provider>
  ).toJSON()
  expect(tree).toMatchSnapshot()
})

//Make your own tests like above to test the values
like image 70
bh4r4th Avatar answered Oct 14 '22 07:10

bh4r4th