/
redux.test.ts
230 lines (186 loc) · 5.56 KB
/
redux.test.ts
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
// tslint:disable-next-line: no-implicit-dependencies
import * as Sentry from '@sentry/minimal';
import { Scope } from '@sentry/types';
import * as Redux from 'redux';
import { createReduxEnhancer } from '../src/redux';
const mockAddBreadcrumb = jest.fn();
const mockSetContext = jest.fn();
jest.mock('@sentry/minimal', () => ({
configureScope: (callback: (scope: any) => Partial<Scope>) =>
callback({
addBreadcrumb: mockAddBreadcrumb,
setContext: mockSetContext,
}),
}));
afterEach(() => {
mockAddBreadcrumb.mockReset();
mockSetContext.mockReset();
});
describe('createReduxEnhancer', () => {
it('logs redux action as breadcrumb', () => {
const enhancer = createReduxEnhancer();
const initialState = {};
const store = Redux.createStore(() => initialState, enhancer);
const action = { type: 'TEST_ACTION' };
store.dispatch(action);
expect(mockAddBreadcrumb).toBeCalledWith({
category: 'redux.action',
data: action,
type: 'info',
});
});
it('sets latest state on to scope', () => {
const enhancer = createReduxEnhancer();
const initialState = {
value: 'initial',
};
const ACTION_TYPE = 'UPDATE_VALUE';
const store = Redux.createStore((state: object = initialState, action: { type: string; newValue: any }) => {
if (action.type === ACTION_TYPE) {
return {
...state,
value: action.newValue,
};
}
return state;
}, enhancer);
const updateAction = { type: ACTION_TYPE, newValue: 'updated' };
store.dispatch(updateAction);
expect(mockSetContext).toBeCalledWith('redux.state', {
value: 'updated',
});
});
describe('transformers', () => {
it('transforms state', () => {
const enhancer = createReduxEnhancer({
stateTransformer: state => ({
...state,
superSecret: 'REDACTED',
}),
});
const initialState = {
superSecret: 'SECRET!',
value: 123,
};
Redux.createStore((state = initialState) => state, enhancer);
expect(mockSetContext).toBeCalledWith('redux.state', {
superSecret: 'REDACTED',
value: 123,
});
});
it('clears state if transformer returns null', () => {
const enhancer = createReduxEnhancer({
stateTransformer: () => null,
});
const initialState = {
superSecret: 'SECRET!',
value: 123,
};
Redux.createStore((state = initialState) => state, enhancer);
// Check that state is cleared
expect(mockSetContext).toBeCalledWith('redux.state', null);
});
it('transforms actions', () => {
const ACTION_TYPES = {
SAFE: 'SAFE_ACTION',
SECRET: 'SUPER_SECRET_ACTION',
};
const enhancer = createReduxEnhancer({
actionTransformer: action => {
if (action.type === ACTION_TYPES.SECRET) {
return {
...action,
secret: 'I love pizza',
};
}
return action;
},
});
const initialState = {};
const store = Redux.createStore(() => initialState, enhancer);
store.dispatch({
secret: 'The Nuclear Launch Code is: Pizza',
type: ACTION_TYPES.SECRET,
});
expect(mockAddBreadcrumb).toBeCalledWith({
category: 'redux.action',
data: {
secret: 'I love pizza',
type: ACTION_TYPES.SECRET,
},
type: 'info',
});
const safeAction = {
secret: 'Not really a secret am I',
type: ACTION_TYPES.SAFE,
};
store.dispatch(safeAction);
expect(mockAddBreadcrumb).toBeCalledWith({
category: 'redux.action',
data: safeAction,
type: 'info',
});
// first time is redux initialize
expect(mockAddBreadcrumb).toBeCalledTimes(3);
});
it("doesn't send action if transformer returns null", () => {
const enhancer = createReduxEnhancer({
actionTransformer: action => {
if (action.type === 'COCA_COLA_RECIPE') {
return null;
}
return action;
},
});
const initialState = {};
const store = Redux.createStore((state = initialState) => state, enhancer);
const safeAction = {
type: 'SAFE',
};
store.dispatch(safeAction);
const secretAction = {
cocaColaRecipe: {
everythingElse: '10ml',
sugar: '990ml',
},
type: 'COCA_COLA_RECIPE',
};
store.dispatch(secretAction);
// first time is redux initialize
expect(mockAddBreadcrumb).toBeCalledTimes(2);
expect(mockAddBreadcrumb).toBeCalledWith({
category: 'redux.action',
data: safeAction,
type: 'info',
});
});
});
it('configureScopeWithState is passed latest state', () => {
const configureScopeWithState = jest.fn();
const enhancer = createReduxEnhancer({
configureScopeWithState,
});
const initialState = {
value: 'outdated',
};
const UPDATE_VALUE = 'UPDATE_VALUE';
const store = Redux.createStore((state: object = initialState, action: { type: string; value: any }) => {
if (action.type === UPDATE_VALUE) {
return {
...state,
value: action.value,
};
}
return state;
}, enhancer);
store.dispatch({
type: UPDATE_VALUE,
value: 'latest',
});
let scopeRef;
Sentry.configureScope(scope => (scopeRef = scope));
expect(configureScopeWithState).toBeCalledWith(scopeRef, {
value: 'latest',
});
});
});