redux Testing Redux apps Testing a Redux store with Mocha and Chai

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

import { expect } from 'chai'; 
import { createStore } from 'redux';

describe('redux store test demonstration', () => {
  describe('testReducer', () => {
    it('should increment value on TEST_ACTION', () => {
      // define a test reducer with initial state: test: 0
      const testReducer = (state = { test: 0 }, action) => {
        switch (action.type) {
          case 'TEST_ACTION':
            return { test: state.test + 1 };
          default:
            return state;
        }
      });

      // create a redux store from reducer
      const store = createStore(testReducer);

      // establish baseline values (should return initial state)
      expect(store.getState().test).to.equal(0);

      // dispatch TEST_ACTION and expect test to be incremented
      store.dispatch({ type: 'TEST_ACTION' });
      expect(store.getState().test).to.equal(1);

      // dispatch an unknown action and expect state to remain unchanged
      store.dispatch({ type: 'UNKNOWN_ACTION' });
      expect(store.getState().test).to.equal(1);
    });
  });
});


Got any redux Question?