-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.test.js
269 lines (242 loc) · 6.5 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
import reduxMiddleware, {
decycle,
setToValue,
sanitize
} from './index'
test('non-errors are ignored', () => {
let store = jest.fn()
let next = jest.fn()
let Rollbar = jest.fn()
let middleware = reduxMiddleware(Rollbar)
let action = {
type: 'NOT_AN_ERROR',
payload: {a: 1}
}
middleware(store)(next)(action)
expect(next).toBeCalledWith(action)
expect(Rollbar).not.toHaveBeenCalled()
})
describe('wrapping action', () => {
let action = {
type: 'I_AM_BAD',
payload: 'bork'
}
let goodAction = {
type: 'I_AM_GOOD',
payload: 'bork'
}
let store = {
getState: jest.fn(() => {
return {
a: 1,
b: 42
}
})
}
let next = jest.fn((a) => {
if (a.type === 'I_AM_BAD') {
throw new Error(a.payload)
} else {
return a.payload
}
})
let Rollbar = {
error: jest.fn()
}
let middleware = reduxMiddleware(Rollbar, null, true)
beforeEach(() => {
jest.clearAllMocks()
})
describe('when action fails', () => {
test('rollbar is called', () => {
middleware(store)(next)(action)
expect(Rollbar.error).toHaveBeenCalled()
})
test('rollbar gets the expected data', () => {
middleware(store)(next)(action)
expect(Rollbar.error).toBeCalledWith(new Error(action.payload), {action: JSON.stringify(action), state: '{"a":1,"b":42}'})
})
})
describe('when action does not fail', () => {
test('rollbar is not called', () => {
middleware(store)(next)(goodAction)
expect(Rollbar.error).not.toHaveBeenCalled()
})
})
})
describe('with an error', () => {
let action = {
type: 'THIS_IS_BROKEN',
payload: new Error('bork'),
error: true
}
let store = {
getState: jest.fn(() => {
return {
a: 1,
b: 42
}
})
}
let next = jest.fn()
let Rollbar = {
error: jest.fn()
}
let middleware = reduxMiddleware(Rollbar)
beforeEach(() => {
jest.clearAllMocks()
})
test('rollbar is called', () => {
middleware(store)(next)(action)
expect(Rollbar.error).toHaveBeenCalled()
})
test('rollbar gets the expected data', () => {
middleware(store)(next)(action)
expect(Rollbar.error).toBeCalledWith(action.payload, {state: '{"a":1,"b":42}'})
})
test('the store has its state fetched', () => {
middleware(store)(next)(action)
expect(store.getState).toHaveBeenCalled()
})
test('next is still called', () => {
middleware(store)(next)(action)
expect(next).toBeCalledWith(action)
})
})
describe('with an error and keypaths', () => {
let action = {
type: 'THIS_IS_BROKEN',
payload: new Error('bork'),
error: true
}
let store = {
getState: jest.fn(() => {
return {
a: 1,
b: 42
}
})
}
let next = jest.fn()
let Rollbar = {
error: jest.fn()
}
let keyPaths = ['a']
let middleware = reduxMiddleware(Rollbar, keyPaths)
beforeEach(() => {
jest.clearAllMocks()
})
test('rollbar is called', () => {
middleware(store)(next)(action)
expect(Rollbar.error).toHaveBeenCalled()
})
test('rollbar gets the expected data', () => {
middleware(store)(next)(action)
expect(Rollbar.error).toBeCalledWith(action.payload, {state: '{"a":"********","b":42}'})
})
test('the store has its state fetched', () => {
middleware(store)(next)(action)
expect(store.getState).toHaveBeenCalled()
})
test('next is still called', () => {
middleware(store)(next)(action)
expect(next).toBeCalledWith(action)
})
})
describe('decycle', () => {
test('a normal object works', () => {
let normalObject = {a: {b: 1}, d: [1,2,3]}
let str = '{"a":{"b":1},"d":[1,2,3]}'
expect(decycle(normalObject)).toBe(str)
})
test('an object with a cycle is stringified', () => {
let obj = {a: 1, c: {a: 1}}
obj.b = obj
let str = '{"a":1,"c":{"a":1}}'
expect(decycle(obj)).toBe(str)
})
test('a non-object works', () => {
expect(decycle(undefined)).toBe(undefined)
expect(decycle(null)).toBe('null')
expect(decycle(42)).toBe('42')
})
})
describe('setToValue', () => {
test('null input', () => {
let obj = null
setToValue(obj, 42, 'a')
expect(obj).toBe(null)
})
test('undefined input', () => {
let obj = undefined
setToValue(obj, 42, 'a')
expect(obj).toBe(undefined)
})
test('string input', () => {
let obj = 'some string'
setToValue(obj, 42, 'a')
expect(obj).toBe('some string')
})
test('simple object key exists', () => {
let obj = {a: 1}
setToValue(obj, 42, 'a')
expect(obj.a).toBe(42)
})
test('simple object key missing', () => {
let obj = {a: 1}
setToValue(obj, 42, 'b')
expect(obj.a).toBe(1)
expect(obj.b).toBe(42)
})
test('deep object key exists', () => {
let obj = {a: {b: {c: 'hello', y: 'bork'}, d: {e: 42}}, x: 99}
setToValue(obj, 'world', 'a.b.c')
expect(obj.a.b.c).toBe('world')
expect(obj.a.b.y).toBe('bork')
expect(obj.a.d.e).toBe(42)
expect(obj.x).toBe(99)
})
test('deep object key missing at end', () => {
let obj = {a: {b: {c: 'hello', y: 'bork'}, d: {e: 42}}, x: 99}
setToValue(obj, 'world', 'a.b.z')
expect(obj.a.b.c).toBe('hello')
expect(obj.a.b.z).toBe('world')
expect(obj.a.b.y).toBe('bork')
expect(obj.a.d.e).toBe(42)
expect(obj.x).toBe(99)
})
test('deep object key missing in middle', () => {
let obj = {a: {b: {c: 'hello', y: 'bork'}, d: {e: 42}}, x: 99}
setToValue(obj, 'world', 'a.z.x')
expect(obj.a.b.c).toBe('hello')
expect(obj.a.z.x).toBe('world')
expect(obj.a.b.y).toBe('bork')
expect(obj.a.d.e).toBe(42)
expect(obj.x).toBe(99)
})
})
describe('sanitize', () => {
test('if keyPaths is a function it just calls is', () => {
let keyPaths = jest.fn((state) => {
return Object.assign({bork: true}, state)
})
let state = {a: 1}
let newState = sanitize(state, keyPaths)
expect(newState.a).toBe(1)
expect(newState.bork).toBe(true)
})
test('if keyPaths is an array, it replaces what it should', () => {
let keyPaths = ['a.b', 'c.d', 'e']
let state = {a: {b: 'bad', x: 'good'}, c: {d: 'bad2', y: 'good2'}, e: 'worst', f: 'bork'}
let newState = sanitize(state, keyPaths)
expect(state.a.b).toBe('bad')
expect(newState.a.b).toBe('********')
expect(newState.a.x).toBe('good')
expect(state.c.d).toBe('bad2')
expect(newState.c.d).toBe('********')
expect(newState.c.y).toBe('good2')
expect(state.e).toBe('worst')
expect(newState.e).toBe('********')
expect(newState.f).toBe('bork')
})
})