@@ -13,7 +13,6 @@ import { IndexRoute, Route, Router, createMemoryHistory, createRoutes, match } f
1313
1414import type { Match , Route as RouteType } from '../src/reactrouterv3' ;
1515import { reactRouterV3BrowserTracingIntegration } from '../src/reactrouterv3' ;
16- import { reactRouterV3Instrumentation } from '../src/reactrouterv3' ;
1716
1817// Have to manually set types because we are using package-alias
1918declare module 'react-router-3' {
@@ -26,219 +25,6 @@ declare module 'react-router-3' {
2625 export const createRoutes : ( routes : any ) => RouteType [ ] ;
2726}
2827
29- function createMockStartTransaction ( opts : { finish ?: jest . FunctionLike ; setMetadata ?: jest . FunctionLike } = { } ) {
30- const { finish = jest . fn ( ) , setMetadata = jest . fn ( ) } = opts ;
31- return jest . fn ( ) . mockReturnValue ( {
32- end : finish ,
33- setMetadata,
34- } ) ;
35- }
36-
37- describe ( 'reactRouterV3Instrumentation' , ( ) => {
38- const routes = (
39- < Route path = "/" component = { ( { children } : { children : JSX . Element } ) => < div > { children } </ div > } >
40- < IndexRoute component = { ( ) => < div > Home</ div > } />
41- < Route path = "about" component = { ( ) => < div > About</ div > } />
42- < Route path = "features" component = { ( ) => < div > Features</ div > } />
43- < Route
44- path = "users/:userid"
45- component = { ( { params } : { params : Record < string , string > } ) => < div > { params . userid } </ div > }
46- />
47- < Route path = "organizations/" >
48- < Route path = ":orgid" component = { ( ) => < div > OrgId</ div > } />
49- < Route path = ":orgid/v1/:teamid" component = { ( ) => < div > Team</ div > } />
50- </ Route >
51- </ Route >
52- ) ;
53- const history = createMemoryHistory ( ) ;
54-
55- const instrumentationRoutes = createRoutes ( routes ) ;
56- // eslint-disable-next-line deprecation/deprecation
57- const instrumentation = reactRouterV3Instrumentation ( history , instrumentationRoutes , match ) ;
58-
59- it ( 'starts a pageload transaction when instrumentation is started' , ( ) => {
60- const mockStartTransaction = createMockStartTransaction ( ) ;
61- instrumentation ( mockStartTransaction ) ;
62- expect ( mockStartTransaction ) . toHaveBeenCalledTimes ( 1 ) ;
63- expect ( mockStartTransaction ) . toHaveBeenLastCalledWith ( {
64- name : '/' ,
65- attributes : {
66- [ SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ] : 'route' ,
67- [ SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ] : 'auto.pageload.react.reactrouter_v3' ,
68- [ SEMANTIC_ATTRIBUTE_SENTRY_OP ] : 'pageload' ,
69- } ,
70- } ) ;
71- } ) ;
72-
73- it ( 'does not start pageload transaction if option is false' , ( ) => {
74- const mockStartTransaction = createMockStartTransaction ( ) ;
75- instrumentation ( mockStartTransaction , false ) ;
76- expect ( mockStartTransaction ) . toHaveBeenCalledTimes ( 0 ) ;
77- } ) ;
78-
79- it ( 'starts a navigation transaction' , ( ) => {
80- const mockStartTransaction = createMockStartTransaction ( ) ;
81- instrumentation ( mockStartTransaction ) ;
82- render ( < Router history = { history } > { routes } </ Router > ) ;
83-
84- act ( ( ) => {
85- history . push ( '/about' ) ;
86- } ) ;
87- expect ( mockStartTransaction ) . toHaveBeenCalledTimes ( 2 ) ;
88- expect ( mockStartTransaction ) . toHaveBeenLastCalledWith ( {
89- name : '/about' ,
90- attributes : {
91- [ SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ] : 'route' ,
92- [ SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ] : 'auto.navigation.react.reactrouter_v3' ,
93- [ SEMANTIC_ATTRIBUTE_SENTRY_OP ] : 'navigation' ,
94- } ,
95- } ) ;
96-
97- act ( ( ) => {
98- history . push ( '/features' ) ;
99- } ) ;
100- expect ( mockStartTransaction ) . toHaveBeenCalledTimes ( 3 ) ;
101- expect ( mockStartTransaction ) . toHaveBeenLastCalledWith ( {
102- name : '/features' ,
103- attributes : {
104- [ SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ] : 'route' ,
105- [ SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ] : 'auto.navigation.react.reactrouter_v3' ,
106- [ SEMANTIC_ATTRIBUTE_SENTRY_OP ] : 'navigation' ,
107- } ,
108- } ) ;
109- } ) ;
110-
111- it ( 'does not start a transaction if option is false' , ( ) => {
112- const mockStartTransaction = createMockStartTransaction ( ) ;
113- instrumentation ( mockStartTransaction , true , false ) ;
114- render ( < Router history = { history } > { routes } </ Router > ) ;
115- expect ( mockStartTransaction ) . toHaveBeenCalledTimes ( 1 ) ;
116- } ) ;
117-
118- it ( 'only starts a navigation transaction on push' , ( ) => {
119- const mockStartTransaction = createMockStartTransaction ( ) ;
120- instrumentation ( mockStartTransaction ) ;
121- render ( < Router history = { history } > { routes } </ Router > ) ;
122-
123- act ( ( ) => {
124- history . replace ( 'hello' ) ;
125- } ) ;
126- expect ( mockStartTransaction ) . toHaveBeenCalledTimes ( 1 ) ;
127- } ) ;
128-
129- it ( 'finishes a transaction on navigation' , ( ) => {
130- const mockFinish = jest . fn ( ) ;
131- const mockStartTransaction = createMockStartTransaction ( { finish : mockFinish } ) ;
132- instrumentation ( mockStartTransaction ) ;
133- render ( < Router history = { history } > { routes } </ Router > ) ;
134- expect ( mockStartTransaction ) . toHaveBeenCalledTimes ( 1 ) ;
135-
136- act ( ( ) => {
137- history . push ( '/features' ) ;
138- } ) ;
139- expect ( mockFinish ) . toHaveBeenCalledTimes ( 1 ) ;
140- expect ( mockStartTransaction ) . toHaveBeenCalledTimes ( 2 ) ;
141- } ) ;
142-
143- it ( 'normalizes transaction names' , ( ) => {
144- const mockStartTransaction = createMockStartTransaction ( ) ;
145- instrumentation ( mockStartTransaction ) ;
146- const { container } = render ( < Router history = { history } > { routes } </ Router > ) ;
147-
148- act ( ( ) => {
149- history . push ( '/users/123' ) ;
150- } ) ;
151- expect ( container . innerHTML ) . toContain ( '123' ) ;
152-
153- expect ( mockStartTransaction ) . toHaveBeenCalledTimes ( 2 ) ;
154- expect ( mockStartTransaction ) . toHaveBeenLastCalledWith ( {
155- name : '/users/:userid' ,
156- attributes : {
157- [ SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ] : 'route' ,
158- [ SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ] : 'auto.navigation.react.reactrouter_v3' ,
159- [ SEMANTIC_ATTRIBUTE_SENTRY_OP ] : 'navigation' ,
160- } ,
161- } ) ;
162- } ) ;
163-
164- it ( 'normalizes nested transaction names' , ( ) => {
165- const mockStartTransaction = createMockStartTransaction ( ) ;
166- instrumentation ( mockStartTransaction ) ;
167- const { container } = render ( < Router history = { history } > { routes } </ Router > ) ;
168-
169- act ( ( ) => {
170- history . push ( '/organizations/1234/v1/758' ) ;
171- } ) ;
172- expect ( container . innerHTML ) . toContain ( 'Team' ) ;
173-
174- expect ( mockStartTransaction ) . toHaveBeenCalledTimes ( 2 ) ;
175- expect ( mockStartTransaction ) . toHaveBeenLastCalledWith ( {
176- name : '/organizations/:orgid/v1/:teamid' ,
177- attributes : {
178- [ SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ] : 'route' ,
179- [ SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ] : 'auto.navigation.react.reactrouter_v3' ,
180- [ SEMANTIC_ATTRIBUTE_SENTRY_OP ] : 'navigation' ,
181- } ,
182- } ) ;
183-
184- act ( ( ) => {
185- history . push ( '/organizations/543' ) ;
186- } ) ;
187- expect ( container . innerHTML ) . toContain ( 'OrgId' ) ;
188-
189- expect ( mockStartTransaction ) . toHaveBeenCalledTimes ( 3 ) ;
190- expect ( mockStartTransaction ) . toHaveBeenLastCalledWith ( {
191- name : '/organizations/:orgid' ,
192- attributes : {
193- [ SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ] : 'route' ,
194- [ SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ] : 'auto.navigation.react.reactrouter_v3' ,
195- [ SEMANTIC_ATTRIBUTE_SENTRY_OP ] : 'navigation' ,
196- } ,
197- } ) ;
198- } ) ;
199-
200- it ( 'sets metadata to url if on an unknown route' , ( ) => {
201- const mockStartTransaction = createMockStartTransaction ( ) ;
202- instrumentation ( mockStartTransaction ) ;
203- render ( < Router history = { history } > { routes } </ Router > ) ;
204-
205- act ( ( ) => {
206- history . push ( '/organizations/1234/some/other/route' ) ;
207- } ) ;
208-
209- expect ( mockStartTransaction ) . toHaveBeenCalledTimes ( 2 ) ;
210- expect ( mockStartTransaction ) . toHaveBeenLastCalledWith ( {
211- name : '/organizations/1234/some/other/route' ,
212- attributes : {
213- [ SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ] : 'url' ,
214- [ SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ] : 'auto.navigation.react.reactrouter_v3' ,
215- [ SEMANTIC_ATTRIBUTE_SENTRY_OP ] : 'navigation' ,
216- } ,
217- } ) ;
218- } ) ;
219-
220- it ( 'sets metadata to url if no routes are provided' , ( ) => {
221- const fakeRoutes = < div > hello</ div > ;
222- const mockStartTransaction = createMockStartTransaction ( ) ;
223- // eslint-disable-next-line deprecation/deprecation
224- const mockInstrumentation = reactRouterV3Instrumentation ( history , createRoutes ( fakeRoutes ) , match ) ;
225- mockInstrumentation ( mockStartTransaction ) ;
226- // We render here with `routes` instead of `fakeRoutes` from above to validate the case
227- // where users provided the instrumentation with a bad set of routes.
228- render ( < Router history = { history } > { routes } </ Router > ) ;
229-
230- expect ( mockStartTransaction ) . toHaveBeenCalledTimes ( 1 ) ;
231- expect ( mockStartTransaction ) . toHaveBeenLastCalledWith ( {
232- name : '/' ,
233- attributes : {
234- [ SEMANTIC_ATTRIBUTE_SENTRY_SOURCE ] : 'url' ,
235- [ SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN ] : 'auto.pageload.react.reactrouter_v3' ,
236- [ SEMANTIC_ATTRIBUTE_SENTRY_OP ] : 'pageload' ,
237- } ,
238- } ) ;
239- } ) ;
240- } ) ;
241-
24228const mockStartBrowserTracingPageLoadSpan = jest . fn ( ) ;
24329const mockStartBrowserTracingNavigationSpan = jest . fn ( ) ;
24430
0 commit comments