-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.js
117 lines (100 loc) · 3.58 KB
/
index.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import {createStore} from 'redux'
import {combineReducers} from './index'
const defState = {}
const reducer1 = {
copyAction: (state = defState, action) => {
return {...state, ...action}
},
both: (state = defState, action) => {
return {...state, ...action}
},
other: (state = defState, action) => {
return state
}
}
const reducer2 = {
both: (state = defState, action) => {
return {...state, ...action}
},
other: (state = defState, action) => {
return state
}
}
describe('test with options(defaultReducerFunctionName:other, useCache:true, strictMode:false,}', () => {
const reducer = combineReducers({
reducer1,
reducer2
}, {
defaultReducerFunctionName : 'other',
useCache : true,
strictMode:false
})
let store = createStore(reducer)
it('should create function', () => {
expect(reducer).toEqual(expect.any(Function))
})
it('should handle initial state', () => {
expect(
reducer({}, { type: '@@redux/INIT' })
).toEqual({ reducer1: {}, reducer2: {} })
})
it('should reducer1.copyAction = action.par', () => {
let action = {type: 'reducer1.copyAction', par: 'Hi'}
store.dispatch(action)
expect(store.getState().reducer1.par).toEqual('Hi')
})
it('should called reducer1.both and reducer2.both and seat par ="both"' , () => {
let action = {type: '[reducer1, reducer2].both', par: 'both'}
store.dispatch(action)
expect(store.getState().reducer1.par).toEqual('both')
expect(store.getState().reducer2.par).toEqual('both')
})
it('should continuee work with cached functions', () => {
let action = {type: '[reducer1, reducer2].both', par: 'both2'}
store.dispatch(action)
expect(store.getState().reducer1.par).toEqual('both2')
expect(store.getState().reducer2.par).toEqual('both2')
action = {type: '[reducer1, reducer2].both', par: 'both3'}
store.dispatch(action)
expect(store.getState().reducer1.par).toEqual('both3')
expect(store.getState().reducer2.par).toEqual('both3')
})
it('should called reducer1.both only and set par = "only1" and preserve state for reducer2' , () => {
action = {type: '[reducer1, reducer2].both', par: 'both3'}
store.dispatch(action)
let action = {type: 'reducer1.both', par: 'only1'}
store.dispatch(action)
expect(store.getState().reducer1.par).toEqual('only1')
expect(store.getState().reducer2.par).toBeDefined()
expect(store.getState().reducer2.par).toEqual('both3')
})
})
describe('test with options(defaultReducerFunctionName:other, useCache:true, strictMode:true,}', () => {
const reducer = combineReducers({
reducer1,
reducer2
}, {
defaultReducerFunctionName : 'other',
useCache : true,
strictMode:true
})
let store = createStore(reducer)
it('should reducer1.copyAction = action.par', () => {
let action = {type: 'reducer1.copyAction', par: 'Hi world'}
store.dispatch(action)
expect(store.getState().reducer1.par).toEqual('Hi world')
})
it('should called reducer1.both and reducer2.both and seat par ="both"' , () => {
let action = {type: '[reducer1, reducer2].both', par: 'both'}
store.dispatch(action)
expect(store.getState().reducer1.par).toEqual('both')
expect(store.getState().reducer2.par).toEqual('both')
})
it('should called reducer1.both only and set par ="only1"' , () => {
let action = {type: 'reducer1.both', par: 'only1'}
store.dispatch(action)
expect(store.getState().reducer1.par).toEqual('only1')
expect(store.getState().reducer2.par).toBeDefined()
expect(store.getState().reducer2.par).toEqual('both')
})
})