1.1.0 • Published 3 years ago

reducer-test-utils v1.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
3 years ago

Reducer-Test-Utils

test status npm version ts

Utilities for testing reducer functions

pipeActions

import { pipeActions } from "reducer-test-utils";

Chains multiple reducer actions and lets you test desired states. (Personally, I see it as a user journey that is treated as a reducer)

Usage:

import { pipeActions } from "reducer-test-utils";
import { myFeatureReducer, Action } from './myFeatureReducer'

describe("test my reducer function", () => {
  it("runs actions", () => {

    const userJourney = pipeActions(
      myFeatureReducer, // your reducer function
      { type: Action.ADD_1 } // actions and functions
      (state) => expect(state).toEqual(2),
      { type: Action.ADD_N, payload: 3 },
      { type: Action.ADD_1 },
      { type: Action.ADD_1 },
      (state) => expect(state).toEqual(7),
      { type: Action.SUB_2 },
      (state) => expect(state).toMatchSnapshot(),
    );

    const initialState = 1;

    userJourney(initialState);
    // userJourney(anotherInitialState);
  });
});

stateChanged

import { stateChanged, pipeActions } from "reducer-test-utils";

You can pass the return value of pipeActions and a initial state to stateChanged. All test functions are executed (exactly like pipeActions) plus a boolean value is returned, indicating whether the state has changed or not. (Deep equality test)

Usage:

import { stateChanged, pipeActions } from "reducer-test-utils";
import { myFeatureReducer, Action } from './myFeatureReducer'

describe("test my reducer function", () => {
  it("runs action", () => {

    const userJourney = pipeActions(
      myFeatureReducer,
      { type: Action.ADD_1 }
      (state) => expect(state).toEqual(2),
      { type: Action.ADD_N, payload: 3 },
      { type: Action.ADD_1 },
      { type: Action.ADD_1 },
      (state) => expect(state).toEqual(7),
    );

    const initialState = 1;
    const changed = stateChanged(userJourney, initialState);

    expect(changed).toBeTruthy();
  });
});
1.1.0

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago