Test-driving a reducer
In this section, we’ll test-drive a new reducer function, and then pull out some repeated code.
A reducer is a simple function that takes an action and the current store state as input and returns a new state object as output. Let’s build that now, as follows:
- Create a new file (in a new directory) named
test/reducers/customer.test.js
. Add the following first test, which checks that if the reducer is invoked with an unknown action, our reducer should return a default state for our object. This is standard behavior for Redux reducers, so you should always start with a test like this:import { reducer } from "../../src/reducers/customer"; describe("customer reducer", () => { it("returns a default state for an undefined existing state", () => { expect(reducer(undefined, {})).toEqual({ customer: {}, ...