-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathreactrouterv5.test.tsx
257 lines (230 loc) · 9.03 KB
/
reactrouterv5.test.tsx
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
import { act,render } from '@testing-library/react';
import { createMemoryHistory } from 'history-4';
import * as React from 'react';
import { matchPath, Route, Router, Switch } from 'react-router-5';
import { reactRouterV5Instrumentation, withSentryRouting } from '../src';
import { RouteConfig } from '../src/reactrouter';
describe('React Router v5', () => {
function createInstrumentation(_opts?: {
startTransactionOnPageLoad?: boolean;
startTransactionOnLocationChange?: boolean;
routes?: RouteConfig[];
}): [jest.Mock, any, { mockSetName: jest.Mock; mockFinish: jest.Mock }] {
const options = {
matchPath: _opts && _opts.routes !== undefined ? matchPath : undefined,
routes: undefined,
startTransactionOnLocationChange: true,
startTransactionOnPageLoad: true,
..._opts,
};
const history = createMemoryHistory();
const mockFinish = jest.fn();
const mockSetName = jest.fn();
const mockStartTransaction = jest.fn().mockReturnValue({ setName: mockSetName, finish: mockFinish });
reactRouterV5Instrumentation(history, options.routes, options.matchPath)(
mockStartTransaction,
options.startTransactionOnPageLoad,
options.startTransactionOnLocationChange,
);
return [mockStartTransaction, history, { mockSetName, mockFinish }];
}
it('starts a pageload transaction when instrumentation is started', () => {
const [mockStartTransaction] = createInstrumentation();
expect(mockStartTransaction).toHaveBeenCalledTimes(1);
expect(mockStartTransaction).toHaveBeenLastCalledWith({
name: '/',
op: 'pageload',
tags: { 'routing.instrumentation': 'react-router-v5' },
});
});
it('does not start pageload transaction if option is false', () => {
const [mockStartTransaction] = createInstrumentation({ startTransactionOnPageLoad: false });
expect(mockStartTransaction).toHaveBeenCalledTimes(0);
});
it('starts a navigation transaction', () => {
const [mockStartTransaction, history] = createInstrumentation();
render(
<Router history={history}>
<Switch>
<Route path="/features" component={() => <div>Features</div>} />
<Route path="/about" component={() => <div>About</div>} />
<Route path="/" component={() => <div>Home</div>} />
</Switch>
</Router>,
);
history.push('/about');
expect(mockStartTransaction).toHaveBeenCalledTimes(2);
expect(mockStartTransaction).toHaveBeenLastCalledWith({
name: '/about',
op: 'navigation',
tags: { 'routing.instrumentation': 'react-router-v5' },
});
history.push('/features');
expect(mockStartTransaction).toHaveBeenCalledTimes(3);
expect(mockStartTransaction).toHaveBeenLastCalledWith({
name: '/features',
op: 'navigation',
tags: { 'routing.instrumentation': 'react-router-v5' },
});
});
it('does not start a transaction if option is false', () => {
const [mockStartTransaction, history] = createInstrumentation({ startTransactionOnLocationChange: false });
render(
<Router history={history}>
<Switch>
<Route path="/features" component={() => <div>Features</div>} />
<Route path="/about" component={() => <div>About</div>} />
<Route path="/" component={() => <div>Home</div>} />
</Switch>
</Router>,
);
expect(mockStartTransaction).toHaveBeenCalledTimes(1);
});
it('only starts a navigation transaction on push', () => {
const [mockStartTransaction, history] = createInstrumentation();
render(
<Router history={history}>
<Switch>
<Route path="/features" component={() => <div>Features</div>} />
<Route path="/about" component={() => <div>About</div>} />
<Route path="/" component={() => <div>Home</div>} />
</Switch>
</Router>,
);
history.replace('hello');
expect(mockStartTransaction).toHaveBeenCalledTimes(1);
});
it('finishes a transaction on navigation', () => {
const [mockStartTransaction, history, { mockFinish }] = createInstrumentation();
render(
<Router history={history}>
<Switch>
<Route path="/features" component={() => <div>Features</div>} />
<Route path="/about" component={() => <div>About</div>} />
<Route path="/" component={() => <div>Home</div>} />
</Switch>
</Router>,
);
expect(mockStartTransaction).toHaveBeenCalledTimes(1);
history.push('/features');
expect(mockFinish).toHaveBeenCalledTimes(1);
expect(mockStartTransaction).toHaveBeenCalledTimes(2);
});
it('does not normalize transaction name ', () => {
const [mockStartTransaction, history] = createInstrumentation();
const { getByText } = render(
<Router history={history}>
<Switch>
<Route path="/users/:userid" component={() => <div>UserId</div>} />
<Route path="/users" component={() => <div>Users</div>} />
<Route path="/" component={() => <div>Home</div>} />
</Switch>
</Router>,
);
act(() => {
history.push('/users/123');
});
getByText('UserId');
expect(mockStartTransaction).toHaveBeenCalledTimes(2);
expect(mockStartTransaction).toHaveBeenLastCalledWith({
name: '/users/123',
op: 'navigation',
tags: { 'routing.instrumentation': 'react-router-v5' },
});
});
it('normalizes transaction name with custom Route', () => {
const [mockStartTransaction, history, { mockSetName }] = createInstrumentation();
const SentryRoute = withSentryRouting(Route);
const { getByText } = render(
<Router history={history}>
<Switch>
<SentryRoute path="/users/:userid" component={() => <div>UserId</div>} />
<SentryRoute path="/users" component={() => <div>Users</div>} />
<SentryRoute path="/" component={() => <div>Home</div>} />
</Switch>
</Router>,
);
act(() => {
history.push('/users/123');
});
getByText('UserId');
expect(mockStartTransaction).toHaveBeenCalledTimes(2);
expect(mockStartTransaction).toHaveBeenLastCalledWith({
name: '/users/123',
op: 'navigation',
tags: { 'routing.instrumentation': 'react-router-v5' },
});
expect(mockSetName).toHaveBeenCalledTimes(2);
expect(mockSetName).toHaveBeenLastCalledWith('/users/:userid');
});
it('normalizes nested transaction names with custom Route', () => {
const [mockStartTransaction, history, { mockSetName }] = createInstrumentation();
const SentryRoute = withSentryRouting(Route);
const { getByText } = render(
<Router history={history}>
<Switch>
<SentryRoute path="/organizations/:orgid/v1/:teamid" component={() => <div>Team</div>} />
<SentryRoute path="/organizations/:orgid" component={() => <div>OrgId</div>} />
<SentryRoute path="/" component={() => <div>Home</div>} />
</Switch>
</Router>,
);
act(() => {
history.push('/organizations/1234/v1/758');
});
getByText('Team');
expect(mockStartTransaction).toHaveBeenCalledTimes(2);
expect(mockStartTransaction).toHaveBeenLastCalledWith({
name: '/organizations/1234/v1/758',
op: 'navigation',
tags: { 'routing.instrumentation': 'react-router-v5' },
});
expect(mockSetName).toHaveBeenCalledTimes(2);
expect(mockSetName).toHaveBeenLastCalledWith('/organizations/:orgid/v1/:teamid');
act(() => {
history.push('/organizations/543');
});
getByText('OrgId');
expect(mockStartTransaction).toHaveBeenCalledTimes(3);
expect(mockStartTransaction).toHaveBeenLastCalledWith({
name: '/organizations/543',
op: 'navigation',
tags: { 'routing.instrumentation': 'react-router-v5' },
});
expect(mockSetName).toHaveBeenCalledTimes(3);
expect(mockSetName).toHaveBeenLastCalledWith('/organizations/:orgid');
});
it('matches with route object', () => {
const routes: RouteConfig[] = [
{
path: '/organizations/:orgid/v1/:teamid',
},
{ path: '/organizations/:orgid' },
{ path: '/' },
];
const [mockStartTransaction, history] = createInstrumentation({ routes });
render(
<Router history={history}>
<Switch>
<Route path="/organizations/:orgid/v1/:teamid" component={() => <div>Team</div>} />
<Route path="/organizations/:orgid" component={() => <div>OrgId</div>} />
<Route path="/" component={() => <div>Home</div>} />
</Switch>
</Router>,
);
history.push('/organizations/1234/v1/758');
expect(mockStartTransaction).toHaveBeenCalledTimes(2);
expect(mockStartTransaction).toHaveBeenLastCalledWith({
name: '/organizations/:orgid/v1/:teamid',
op: 'navigation',
tags: { 'routing.instrumentation': 'react-router-v5' },
});
history.push('/organizations/1234');
expect(mockStartTransaction).toHaveBeenCalledTimes(3);
expect(mockStartTransaction).toHaveBeenLastCalledWith({
name: '/organizations/:orgid',
op: 'navigation',
tags: { 'routing.instrumentation': 'react-router-v5' },
});
});
});