forked from wellguimaraes/actionware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateReducer.test.js
56 lines (45 loc) · 1.8 KB
/
createReducer.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
import { createReducer } from './createReducer'
import { successType } from './createAction'
describe('createReducer', () => {
it('should return a function', () => {
const reducer = createReducer({})
expect(typeof reducer).to.equal('function')
})
it('returned function should call proper function based on given action type', () => {
const loadUserByPk = () => {}
const loadAllUsers = () => {}
const handler = spy()
const reducer = createReducer({})
.on(
loadUserByPk,
loadAllUsers,
handler
)
reducer({}, { type: successType(loadUserByPk) })
reducer({}, { type: successType(loadAllUsers) })
reducer({}, { type: 'loremIpsumDolor' })
expect(handler.calledTwice).to.equal(true)
})
describe('reducer', () => {
const loadUserByPk = () => {}
const reducer = createReducer({})
.on(loadUserByPk, (state, ignore) => ({ ...state, x: 1 }))
.on('anotherActionName', (state, ignore) => ({ ...state, x: 2 }))
const currentState = { x: 0 }
it('should return previous state when no type handler is available', () => {
const newState = reducer(currentState, { type: 'nonExistingActionName' })
expect(newState).to.equal(currentState)
expect(newState.x).to.equal(0)
})
it('should return a new state according to respective type handler (function)', () => {
const newState = reducer(currentState, { type: loadUserByPk._trackedAction._successType })
expect(newState).not.to.equal(currentState)
expect(newState.x).to.equal(1)
})
it('should return a new state according to respective type handler (string)', () => {
const newState = reducer(currentState, { type: 'anotherActionName' })
expect(newState).not.to.equal(currentState)
expect(newState.x).to.equal(2)
})
})
})