Reducer-Test-Utils
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,
{ 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),
{ type: Action.SUB_2 },
(state) => expect(state).toMatchSnapshot(),
);
const initialState = 1;
userJourney(initialState);
});
});
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();
});
});