Skip to content

Commit 4a7e091

Browse files
committed
test(redux): Add test case for Redux state attachment
New test cases to validate the attachment of Redux state to Sentry events. The test ensures that the attachment logic correctly adds the Redux state when available and does not add it when it's empty or undefined and when not of type redux.
1 parent cd754fa commit 4a7e091

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import * as Sentry from '@sentry/browser';
2+
import * as Redux from 'redux';
3+
4+
import { createReduxEnhancer } from '../src/redux';
5+
6+
const mockAddBreadcrumb = jest.fn();
7+
const mockSetContext = jest.fn();
8+
9+
jest.mock('@sentry/browser', () => ({
10+
...jest.requireActual('@sentry/browser'),
11+
}));
12+
13+
afterEach(() => {
14+
mockAddBreadcrumb.mockReset();
15+
mockSetContext.mockReset();
16+
});
17+
18+
describe('Redux State Attachments', () => {
19+
it('attaches Redux state to Sentry scope', () => {
20+
const enhancer = createReduxEnhancer();
21+
22+
const initialState = {
23+
value: 'initial',
24+
};
25+
26+
const store = Redux.createStore((state = initialState) => state, enhancer);
27+
28+
const updateAction = { type: 'UPDATE_VALUE', value: 'updated' };
29+
30+
store.dispatch(updateAction);
31+
32+
const error = new Error('test');
33+
Sentry.captureException(error);
34+
35+
Sentry.configureScope(scope => {
36+
expect(scope.getAttachments()).toContainEqual(
37+
expect.objectContaining({
38+
filename: 'redux_state.json',
39+
data: JSON.stringify({
40+
value: 'updated',
41+
}),
42+
}),
43+
);
44+
});
45+
});
46+
47+
it('does not attach when attachReduxState is false', () => {
48+
const enhancer = createReduxEnhancer({ attachReduxState: false });
49+
50+
const initialState = {
51+
value: 'initial',
52+
};
53+
54+
const store = Redux.createStore((state = initialState) => state, enhancer);
55+
56+
const updateAction = { type: 'UPDATE_VALUE', value: 'updated' };
57+
58+
store.dispatch(updateAction);
59+
60+
const error = new Error('test');
61+
Sentry.captureException(error);
62+
63+
Sentry.configureScope(scope => {
64+
expect(scope.getAttachments()).not.toContainEqual(
65+
expect.objectContaining({
66+
filename: 'redux_state.json',
67+
data: expect.anything(),
68+
}),
69+
);
70+
});
71+
});
72+
73+
it('does not attach when state.type is not redux', () => {
74+
const enhancer = createReduxEnhancer();
75+
76+
const initialState = {
77+
value: 'initial',
78+
};
79+
80+
Redux.createStore((state = initialState) => state, enhancer);
81+
82+
Sentry.configureScope(scope => {
83+
scope.setContext('state', {
84+
state: {
85+
type: 'not_redux',
86+
value: {
87+
value: 'updated',
88+
},
89+
},
90+
});
91+
});
92+
93+
const error = new Error('test');
94+
Sentry.captureException(error);
95+
96+
Sentry.configureScope(scope => {
97+
expect(scope.getAttachments()).not.toContainEqual(
98+
expect.objectContaining({
99+
filename: 'redux_state.json',
100+
data: expect.anything(),
101+
}),
102+
);
103+
});
104+
});
105+
});

0 commit comments

Comments
 (0)