Skip to content

Commit 04add0c

Browse files
committed
test(react): Handle nested parameterized routes in reactrouterv3 transaction normalization
1 parent c64c4fd commit 04add0c

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

packages/react/src/reactrouterv3.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,17 @@ function getRouteStringFromRoutes(routes: Route[]): string {
150150
}
151151
}
152152

153-
return routesWithPaths
153+
const pathParts = routesWithPaths
154154
.slice(index)
155155
.filter(({ path }) => !!path)
156-
.map(({ path }) => path)
157-
.join('');
156+
.map(({ path }) => path);
157+
158+
// Join all parts with '/', then replace multiple slashes with a single one.
159+
let fullPath = pathParts.join('/');
160+
fullPath = fullPath.replace(/\/+/g, '/');
161+
162+
// Edge case: If the path started with multiple slashes and routes,
163+
// like `//foo`, it might become `/foo`. This is generally acceptable.
164+
165+
return fullPath;
158166
}

packages/react/test/reactrouterv3.test.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ describe('browserTracingReactRouterV3', () => {
6464
<Route path=":orgid" component={() => <div>OrgId</div>} />
6565
<Route path=":orgid/v1/:teamid" component={() => <div>Team</div>} />
6666
</Route>
67+
<Route path="teams">
68+
<Route path=":teamId">
69+
<Route path="details" component={() => <div>Team Details</div>} />
70+
</Route>
71+
</Route>
6772
</Route>
6873
);
6974
const history = createMemoryHistory();
@@ -192,6 +197,22 @@ describe('browserTracingReactRouterV3', () => {
192197
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'navigation',
193198
},
194199
});
200+
expect(getCurrentScope().getScopeData().transactionName).toEqual('/users/:userid');
201+
202+
act(() => {
203+
history.push('/teams/456/details');
204+
});
205+
206+
expect(mockStartBrowserTracingNavigationSpan).toHaveBeenCalledTimes(2);
207+
expect(mockStartBrowserTracingNavigationSpan).toHaveBeenLastCalledWith(expect.any(BrowserClient), {
208+
name: '/teams/:teamId/details',
209+
attributes: {
210+
[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route',
211+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.navigation.react.reactrouter_v3',
212+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'navigation',
213+
},
214+
});
215+
expect(getCurrentScope().getScopeData().transactionName).toEqual('/teams/:teamId/details');
195216
});
196217

197218
it("updates the scope's `transactionName` on a navigation", () => {

0 commit comments

Comments
 (0)