-
-
Notifications
You must be signed in to change notification settings - Fork 15.1k
Expand file tree
/
Copy pathreduceReducers.spec.ts
More file actions
34 lines (30 loc) · 1.5 KB
/
reduceReducers.spec.ts
File metadata and controls
34 lines (30 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import reduceReducers from "@internal/reduceReducers";
describe('Utils', () => {
describe('reduceReducers', () => {
const incrementReducer = (state = 0, action: { type: "increment" }) =>
action.type === 'increment' ? state + 1 : state
const decrementReducer = (state = 0, action: { type: "decrement" }) =>
action.type === 'decrement' ? state - 1 : state
it("runs multiple reducers in sequence and returns the result of the last one", () => {
const combined = reduceReducers(incrementReducer, decrementReducer)
expect(combined(0, { type: 'increment' })).toBe(1)
expect(combined(1, { type: 'decrement' })).toBe(0)
})
it("accepts an initial state argument", () => {
const combined = reduceReducers(2, incrementReducer, decrementReducer)
expect(combined(undefined, { type: "increment" })).toBe(3)
})
it("can accept the preloaded state of the first reducer", () => {
const parserReducer = (state: number | string = 0) =>
typeof state === 'string' ? parseInt(state, 10) : state
const combined = reduceReducers(parserReducer, incrementReducer)
expect(combined("1", { type: "increment"})).toBe(2)
const combined2 = reduceReducers("1", parserReducer, incrementReducer)
expect(combined2(undefined, { type: "increment"})).toBe(2)
})
it("accepts undefined as initial state", () => {
const combined = reduceReducers(undefined, incrementReducer)
expect(combined(undefined, { type: "increment" })).toBe(1)
})
});
})