@@ -15,6 +15,14 @@ declare module 'react-router-3' {
15
15
export const createRoutes : ( routes : any ) => RouteType [ ] ;
16
16
}
17
17
18
+ function createMockStartTransaction ( opts : { finish ?: jest . FunctionLike ; setMetadata ?: jest . FunctionLike } = { } ) {
19
+ const { finish = jest . fn ( ) , setMetadata = jest . fn ( ) } = opts ;
20
+ return jest . fn ( ) . mockReturnValue ( {
21
+ finish,
22
+ setMetadata,
23
+ } ) ;
24
+ }
25
+
18
26
describe ( 'React Router V3' , ( ) => {
19
27
const routes = (
20
28
< Route path = "/" component = { ( { children } : { children : JSX . Element } ) => < div > { children } </ div > } >
@@ -37,24 +45,27 @@ describe('React Router V3', () => {
37
45
const instrumentation = reactRouterV3Instrumentation ( history , instrumentationRoutes , match ) ;
38
46
39
47
it ( 'starts a pageload transaction when instrumentation is started' , ( ) => {
40
- const mockStartTransaction = jest . fn ( ) ;
48
+ const mockStartTransaction = createMockStartTransaction ( ) ;
41
49
instrumentation ( mockStartTransaction ) ;
42
50
expect ( mockStartTransaction ) . toHaveBeenCalledTimes ( 1 ) ;
43
51
expect ( mockStartTransaction ) . toHaveBeenLastCalledWith ( {
44
52
name : '/' ,
45
53
op : 'pageload' ,
46
54
tags : { 'routing.instrumentation' : 'react-router-v3' } ,
55
+ metadata : {
56
+ source : 'route' ,
57
+ } ,
47
58
} ) ;
48
59
} ) ;
49
60
50
61
it ( 'does not start pageload transaction if option is false' , ( ) => {
51
- const mockStartTransaction = jest . fn ( ) ;
62
+ const mockStartTransaction = createMockStartTransaction ( ) ;
52
63
instrumentation ( mockStartTransaction , false ) ;
53
64
expect ( mockStartTransaction ) . toHaveBeenCalledTimes ( 0 ) ;
54
65
} ) ;
55
66
56
67
it ( 'starts a navigation transaction' , ( ) => {
57
- const mockStartTransaction = jest . fn ( ) ;
68
+ const mockStartTransaction = createMockStartTransaction ( ) ;
58
69
instrumentation ( mockStartTransaction ) ;
59
70
render ( < Router history = { history } > { routes } </ Router > ) ;
60
71
@@ -66,6 +77,9 @@ describe('React Router V3', () => {
66
77
name : '/about' ,
67
78
op : 'navigation' ,
68
79
tags : { from : '/' , 'routing.instrumentation' : 'react-router-v3' } ,
80
+ metadata : {
81
+ source : 'route' ,
82
+ } ,
69
83
} ) ;
70
84
71
85
act ( ( ) => {
@@ -76,18 +90,21 @@ describe('React Router V3', () => {
76
90
name : '/features' ,
77
91
op : 'navigation' ,
78
92
tags : { from : '/about' , 'routing.instrumentation' : 'react-router-v3' } ,
93
+ metadata : {
94
+ source : 'route' ,
95
+ } ,
79
96
} ) ;
80
97
} ) ;
81
98
82
99
it ( 'does not start a transaction if option is false' , ( ) => {
83
- const mockStartTransaction = jest . fn ( ) ;
100
+ const mockStartTransaction = createMockStartTransaction ( ) ;
84
101
instrumentation ( mockStartTransaction , true , false ) ;
85
102
render ( < Router history = { history } > { routes } </ Router > ) ;
86
103
expect ( mockStartTransaction ) . toHaveBeenCalledTimes ( 1 ) ;
87
104
} ) ;
88
105
89
106
it ( 'only starts a navigation transaction on push' , ( ) => {
90
- const mockStartTransaction = jest . fn ( ) ;
107
+ const mockStartTransaction = createMockStartTransaction ( ) ;
91
108
instrumentation ( mockStartTransaction ) ;
92
109
render ( < Router history = { history } > { routes } </ Router > ) ;
93
110
@@ -99,7 +116,7 @@ describe('React Router V3', () => {
99
116
100
117
it ( 'finishes a transaction on navigation' , ( ) => {
101
118
const mockFinish = jest . fn ( ) ;
102
- const mockStartTransaction = jest . fn ( ) . mockReturnValue ( { finish : mockFinish } ) ;
119
+ const mockStartTransaction = createMockStartTransaction ( { finish : mockFinish } ) ;
103
120
instrumentation ( mockStartTransaction ) ;
104
121
render ( < Router history = { history } > { routes } </ Router > ) ;
105
122
expect ( mockStartTransaction ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -112,7 +129,7 @@ describe('React Router V3', () => {
112
129
} ) ;
113
130
114
131
it ( 'normalizes transaction names' , ( ) => {
115
- const mockStartTransaction = jest . fn ( ) ;
132
+ const mockStartTransaction = createMockStartTransaction ( ) ;
116
133
instrumentation ( mockStartTransaction ) ;
117
134
const { container } = render ( < Router history = { history } > { routes } </ Router > ) ;
118
135
@@ -126,11 +143,14 @@ describe('React Router V3', () => {
126
143
name : '/users/:userid' ,
127
144
op : 'navigation' ,
128
145
tags : { from : '/' , 'routing.instrumentation' : 'react-router-v3' } ,
146
+ metadata : {
147
+ source : 'route' ,
148
+ } ,
129
149
} ) ;
130
150
} ) ;
131
151
132
152
it ( 'normalizes nested transaction names' , ( ) => {
133
- const mockStartTransaction = jest . fn ( ) ;
153
+ const mockStartTransaction = createMockStartTransaction ( ) ;
134
154
instrumentation ( mockStartTransaction ) ;
135
155
const { container } = render ( < Router history = { history } > { routes } </ Router > ) ;
136
156
@@ -144,6 +164,9 @@ describe('React Router V3', () => {
144
164
name : '/organizations/:orgid/v1/:teamid' ,
145
165
op : 'navigation' ,
146
166
tags : { from : '/' , 'routing.instrumentation' : 'react-router-v3' } ,
167
+ metadata : {
168
+ source : 'route' ,
169
+ } ,
147
170
} ) ;
148
171
149
172
act ( ( ) => {
@@ -156,6 +179,49 @@ describe('React Router V3', () => {
156
179
name : '/organizations/:orgid' ,
157
180
op : 'navigation' ,
158
181
tags : { from : '/organizations/:orgid/v1/:teamid' , 'routing.instrumentation' : 'react-router-v3' } ,
182
+ metadata : {
183
+ source : 'route' ,
184
+ } ,
185
+ } ) ;
186
+ } ) ;
187
+
188
+ it ( 'sets metadata to url if on an unknown route' , ( ) => {
189
+ const mockStartTransaction = createMockStartTransaction ( ) ;
190
+ instrumentation ( mockStartTransaction ) ;
191
+ render ( < Router history = { history } > { routes } </ Router > ) ;
192
+
193
+ act ( ( ) => {
194
+ history . push ( '/organizations/1234/some/other/route' ) ;
195
+ } ) ;
196
+
197
+ expect ( mockStartTransaction ) . toHaveBeenCalledTimes ( 2 ) ;
198
+ expect ( mockStartTransaction ) . toHaveBeenLastCalledWith ( {
199
+ name : '/organizations/1234/some/other/route' ,
200
+ op : 'navigation' ,
201
+ tags : { from : '/' , 'routing.instrumentation' : 'react-router-v3' } ,
202
+ metadata : {
203
+ source : 'url' ,
204
+ } ,
205
+ } ) ;
206
+ } ) ;
207
+
208
+ it ( 'sets metadata to url if no routes are provided' , ( ) => {
209
+ const fakeRoutes = < div > hello</ div > ;
210
+ const mockStartTransaction = createMockStartTransaction ( ) ;
211
+ const mockInstrumentation = reactRouterV3Instrumentation ( history , createRoutes ( fakeRoutes ) , match ) ;
212
+ mockInstrumentation ( mockStartTransaction ) ;
213
+ // We render here with `routes` instead of `fakeRoutes` from above to validate the case
214
+ // where users provided the instrumentation with a bad set of routes.
215
+ render ( < Router history = { history } > { routes } </ Router > ) ;
216
+
217
+ expect ( mockStartTransaction ) . toHaveBeenCalledTimes ( 1 ) ;
218
+ expect ( mockStartTransaction ) . toHaveBeenLastCalledWith ( {
219
+ name : '/' ,
220
+ op : 'pageload' ,
221
+ tags : { 'routing.instrumentation' : 'react-router-v3' } ,
222
+ metadata : {
223
+ source : 'url' ,
224
+ } ,
159
225
} ) ;
160
226
} ) ;
161
227
} ) ;
0 commit comments