-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathAggregateReducer.test.js
82 lines (71 loc) · 2.22 KB
/
AggregateReducer.test.js
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const assert = require('assert')
const reduce = require('./reduce')
const AggregateReducer = require('./AggregateReducer')
describe('AggregateReducer', () => {
it('Combines reducers', async () => {
const reducerA = (state, action) => {
if (action.type == 'A') {
return { ...state, A: true }
}
return state
}
const reducerB = (state, action) => {
if (action.type == 'B') {
return { ...state, B: true }
}
return state
}
const reducerC = (state, action) => {
if (action.type == 'C') {
return { ...state, C: true }
}
return state
}
const combinedReducer = AggregateReducer([
reducerA,
reducerB,
reducerC,
])
const stateA = [{ type: 'A' }].reduce(combinedReducer, {})
assert.deepEqual(stateA, { A: true })
const stateAB = [{ type: 'A' }, { type: 'B' }].reduce(combinedReducer, {})
assert.deepEqual(stateAB, { A: true, B: true })
const stateABC = [{ type: 'A' }, { type: 'B' }, { type: 'C' }].reduce(combinedReducer, {})
assert.deepEqual(stateABC, { A: true, B: true, C: true })
})
it('Combines async reducers', async () => {
const reducerA = async (state, action) => {
if (action.type == 'A') {
return { ...state, A: true }
}
return state
}
const reducerB = async (state, action) => {
if (action.type == 'B') {
return { ...state, B: true }
}
return state
}
const reducerC = async (state, action) => {
if (action.type == 'C') {
return { ...state, C: true }
}
return state
}
const combinedReducer = AggregateReducer([
reducerA,
reducerB,
reducerC,
])
const stateA = await reduce([{ type: 'A' }], combinedReducer, {})
assert.deepEqual(stateA, { A: true })
const stateAB = await reduce([{ type: 'A' }, { type: 'B' }], combinedReducer, {})
assert.deepEqual(stateAB, { A: true, B: true })
const stateABC = await reduce([{ type: 'A' }, { type: 'B' }, { type: 'C' }], combinedReducer, {})
assert.deepEqual(stateABC, { A: true, B: true, C: true })
})
it('Identity', async () => {
const identity = AggregateReducer([])
assert.equal(1, identity(1))
})
})