-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest.js
182 lines (153 loc) · 4.1 KB
/
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
"use strict"
const { createStore } = require('redux')
const test = require('tape')
const { createMachine } = require('./index.js')
// BEGIN FIXTURES
const users = ['userFoo', 'userBar', 'userBaz']
const initReducer = (state = {}, action) => {
switch (action.type) {
case 'FETCH_USERS':
return Object.assign({}, state, {
error: null,
status: 'IN_PROGRESS'
})
default:
return state
}
}
const inProgressReducer = (state = {}, action) => {
switch (action.type) {
case 'FETCH_USERS_RESPONSE':
return Object.assign({}, state, {
error: null,
users: action.payload.users,
status: 'INIT'
})
case 'FETCH_USERS_FAIL':
return Object.assign({}, state, {
error: action.payload,
status: 'INIT'
})
default:
return state
}
}
const fetchUsersReducer = createMachine({
'INIT': initReducer,
'IN_PROGRESS': inProgressReducer
})
// END FIXTURES
// run the reducer and go through several status transitions
// to test a typical scenario—making an API call
test('should transition between states', t => {
let state = undefined
let prevState = undefined
const store = createStore(fetchUsersReducer, state)
const expectState = (expected, maybeMessage) => t.deepEquals(state, expected, maybeMessage)
const action = (type, payload) => {
prevState = state
store.dispatch({type, payload})
state = store.getState()
}
action('DUMMY')
expectState({
status: 'INIT',
}, 'Should set initial status to "INIT"')
action('DUMMY AGAIN')
t.equals(state, prevState, 'Should not change the state when an action is unhandled')
action('FETCH_USERS_RESPONSE', {users})
expectState(prevState, 'Should ignore messages when not handled by current status')
action('FETCH_USERS')
expectState({
error: null,
status: 'IN_PROGRESS'
})
action('FETCH_USERS_FAIL', 'timeout')
expectState({
error: 'timeout',
status: 'INIT'
})
action('FETCH_USERS')
expectState({
error: null,
status: 'IN_PROGRESS'
})
action('FETCH_USERS')
expectState(prevState)
action('FETCH_USERS_RESPONSE', {users})
expectState({
error: null,
status: 'INIT',
users
})
t.end()
})
test('should be able to pass an extraArgument to inner reducers', t => {
const configReducer = (state = {}, action) => state;
const gameReducer = (state = {}, action, isMuted) => {
switch (action.type) {
case 'START':
const audio = (isMuted === true) ? 'OFF' : 'ON'
return Object.assign({}, state, {
audio: audio
})
default:
return state
}
}
const innerReducer = (state = {}, action) => {
const isMuted = state.config ? state.config.isMuted : undefined
return {
'game': gameReducer(state.game, action, isMuted),
'config': configReducer(state.config, action)
}
}
const fsmReducer = createMachine({
'INIT': innerReducer
});
const store = createStore(fsmReducer, undefined)
store.dispatch({
type: 'START'
})
t.deepEquals(store.getState(), {
status: 'INIT',
game: {
audio: 'ON'
},
config: {}
})
const silentStore = createStore(fsmReducer, {
config: {
isMuted: true
}
})
silentStore.dispatch({
type: 'START'
})
t.deepEquals(silentStore.getState(), {
status: 'INIT',
game: {
audio: 'OFF'
},
config: {
isMuted: true
}
})
t.end()
})
test('should error on status not found', t => {
let store = {status: 'STATUS_NOT_IN_CREATE_MACHINE'}
const reducer = createMachine({})
let error
try {
reducer(store, {type: 'DUMMY'})
}
catch(err) {
error = err
}
t.equals(
error.message,
'reducersObject missing reducer for status STATUS_NOT_IN_CREATE_MACHINE'
)
t.end()
})