|
| 1 | +import { render } from '@testing-library/react'; |
| 2 | +import * as React from 'react'; |
| 3 | +import { createMemoryHistory, createRoutes, IndexRoute, match, Route, Router } from 'react-router-3'; |
| 4 | + |
| 5 | +import { Match, reactRouterV3Instrumentation, Route as RouteType } from '../src/reactrouter'; |
| 6 | + |
| 7 | +// Have to manually set types because we are using package-alias |
| 8 | +declare module 'react-router-3' { |
| 9 | + type History = { replace: Function; push: Function }; |
| 10 | + export function createMemoryHistory(): History; |
| 11 | + export const Router: React.ComponentType<{ history: History }>; |
| 12 | + export const Route: React.ComponentType<{ path: string; component?: React.ComponentType<any> }>; |
| 13 | + export const IndexRoute: React.ComponentType<{ component: React.ComponentType<any> }>; |
| 14 | + export const match: Match; |
| 15 | + export const createRoutes: (routes: any) => RouteType[]; |
| 16 | +} |
| 17 | + |
| 18 | +describe('React Router V3', () => { |
| 19 | + const routes = ( |
| 20 | + <Route path="/" component={({ children }: { children: JSX.Element }) => <div>{children}</div>}> |
| 21 | + <IndexRoute component={() => <div>Home</div>} /> |
| 22 | + <Route path="about" component={() => <div>About</div>} /> |
| 23 | + <Route path="features" component={() => <div>Features</div>} /> |
| 24 | + <Route |
| 25 | + path="users/:userid" |
| 26 | + component={({ params }: { params: Record<string, string> }) => <div>{params.userid}</div>} |
| 27 | + /> |
| 28 | + <Route path="organizations/"> |
| 29 | + <Route path=":orgid" component={() => <div>OrgId</div>} /> |
| 30 | + <Route path=":orgid/v1/:teamid" component={() => <div>Team</div>} /> |
| 31 | + </Route> |
| 32 | + </Route> |
| 33 | + ); |
| 34 | + const history = createMemoryHistory(); |
| 35 | + |
| 36 | + const instrumentationRoutes = createRoutes(routes); |
| 37 | + const instrumentation = reactRouterV3Instrumentation(history, instrumentationRoutes, match); |
| 38 | + |
| 39 | + it('starts a pageload transaction when instrumentation is started', () => { |
| 40 | + const mockStartTransaction = jest.fn(); |
| 41 | + instrumentation(mockStartTransaction); |
| 42 | + expect(mockStartTransaction).toHaveBeenCalledTimes(1); |
| 43 | + expect(mockStartTransaction).toHaveBeenLastCalledWith({ |
| 44 | + name: '/', |
| 45 | + op: 'pageload', |
| 46 | + tags: { 'routing.instrumentation': 'react-router-v3' }, |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + it('does not start pageload transaction if option is false', () => { |
| 51 | + const mockStartTransaction = jest.fn(); |
| 52 | + instrumentation(mockStartTransaction, false); |
| 53 | + expect(mockStartTransaction).toHaveBeenCalledTimes(0); |
| 54 | + }); |
| 55 | + |
| 56 | + it('starts a navigation transaction', () => { |
| 57 | + const mockStartTransaction = jest.fn(); |
| 58 | + instrumentation(mockStartTransaction); |
| 59 | + render(<Router history={history}>{routes}</Router>); |
| 60 | + |
| 61 | + history.push('/about'); |
| 62 | + expect(mockStartTransaction).toHaveBeenCalledTimes(2); |
| 63 | + expect(mockStartTransaction).toHaveBeenLastCalledWith({ |
| 64 | + name: '/about', |
| 65 | + op: 'navigation', |
| 66 | + tags: { from: '/', 'routing.instrumentation': 'react-router-v3' }, |
| 67 | + }); |
| 68 | + |
| 69 | + history.push('/features'); |
| 70 | + expect(mockStartTransaction).toHaveBeenCalledTimes(3); |
| 71 | + expect(mockStartTransaction).toHaveBeenLastCalledWith({ |
| 72 | + name: '/features', |
| 73 | + op: 'navigation', |
| 74 | + tags: { from: '/about', 'routing.instrumentation': 'react-router-v3' }, |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + it('does not start a transaction if option is false', () => { |
| 79 | + const mockStartTransaction = jest.fn(); |
| 80 | + instrumentation(mockStartTransaction, true, false); |
| 81 | + render(<Router history={history}>{routes}</Router>); |
| 82 | + expect(mockStartTransaction).toHaveBeenCalledTimes(1); |
| 83 | + }); |
| 84 | + |
| 85 | + it('only starts a navigation transaction on push', () => { |
| 86 | + const mockStartTransaction = jest.fn(); |
| 87 | + instrumentation(mockStartTransaction); |
| 88 | + render(<Router history={history}>{routes}</Router>); |
| 89 | + |
| 90 | + history.replace('hello'); |
| 91 | + expect(mockStartTransaction).toHaveBeenCalledTimes(1); |
| 92 | + }); |
| 93 | + |
| 94 | + it('finishes a transaction on navigation', () => { |
| 95 | + const mockFinish = jest.fn(); |
| 96 | + const mockStartTransaction = jest.fn().mockReturnValue({ finish: mockFinish }); |
| 97 | + instrumentation(mockStartTransaction); |
| 98 | + render(<Router history={history}>{routes}</Router>); |
| 99 | + expect(mockStartTransaction).toHaveBeenCalledTimes(1); |
| 100 | + |
| 101 | + history.push('/features'); |
| 102 | + expect(mockFinish).toHaveBeenCalledTimes(1); |
| 103 | + expect(mockStartTransaction).toHaveBeenCalledTimes(2); |
| 104 | + }); |
| 105 | + |
| 106 | + it('normalizes transaction names', () => { |
| 107 | + const mockStartTransaction = jest.fn(); |
| 108 | + instrumentation(mockStartTransaction); |
| 109 | + const { container } = render(<Router history={history}>{routes}</Router>); |
| 110 | + |
| 111 | + history.push('/users/123'); |
| 112 | + expect(container.innerHTML).toContain('123'); |
| 113 | + |
| 114 | + expect(mockStartTransaction).toHaveBeenCalledTimes(2); |
| 115 | + expect(mockStartTransaction).toHaveBeenLastCalledWith({ |
| 116 | + name: '/users/:userid', |
| 117 | + op: 'navigation', |
| 118 | + tags: { from: '/', 'routing.instrumentation': 'react-router-v3' }, |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + it('normalizes nested transaction names', () => { |
| 123 | + const mockStartTransaction = jest.fn(); |
| 124 | + instrumentation(mockStartTransaction); |
| 125 | + const { container } = render(<Router history={history}>{routes}</Router>); |
| 126 | + |
| 127 | + history.push('/organizations/1234/v1/758'); |
| 128 | + expect(container.innerHTML).toContain('Team'); |
| 129 | + |
| 130 | + expect(mockStartTransaction).toHaveBeenCalledTimes(2); |
| 131 | + expect(mockStartTransaction).toHaveBeenLastCalledWith({ |
| 132 | + name: '/organizations/:orgid/v1/:teamid', |
| 133 | + op: 'navigation', |
| 134 | + tags: { from: '/', 'routing.instrumentation': 'react-router-v3' }, |
| 135 | + }); |
| 136 | + |
| 137 | + history.push('/organizations/543'); |
| 138 | + expect(container.innerHTML).toContain('OrgId'); |
| 139 | + |
| 140 | + expect(mockStartTransaction).toHaveBeenCalledTimes(3); |
| 141 | + expect(mockStartTransaction).toHaveBeenLastCalledWith({ |
| 142 | + name: '/organizations/:orgid', |
| 143 | + op: 'navigation', |
| 144 | + tags: { from: '/organizations/:orgid/v1/:teamid', 'routing.instrumentation': 'react-router-v3' }, |
| 145 | + }); |
| 146 | + }); |
| 147 | +}); |
0 commit comments