|
| 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 | +const App: React.FC = ({ children }) => <div>{children}</div>; |
| 19 | + |
| 20 | +function Home(): JSX.Element { |
| 21 | + return <div>Home</div>; |
| 22 | +} |
| 23 | + |
| 24 | +function About(): JSX.Element { |
| 25 | + return <div>About</div>; |
| 26 | +} |
| 27 | + |
| 28 | +function Features(): JSX.Element { |
| 29 | + return <div>Features</div>; |
| 30 | +} |
| 31 | + |
| 32 | +function Users({ params }: { params: Record<string, string> }): JSX.Element { |
| 33 | + return <div>{params.userid}</div>; |
| 34 | +} |
| 35 | + |
| 36 | +describe('React Router V3', () => { |
| 37 | + const routes = ( |
| 38 | + <Route path="/" component={App}> |
| 39 | + <IndexRoute component={Home} /> |
| 40 | + <Route path="about" component={About} /> |
| 41 | + <Route path="features" component={Features} /> |
| 42 | + <Route path="users/:userid" component={Users} /> |
| 43 | + </Route> |
| 44 | + ); |
| 45 | + const history = createMemoryHistory(); |
| 46 | + |
| 47 | + const instrumentationRoutes = createRoutes(routes); |
| 48 | + const instrumentation = reactRouterV3Instrumentation(history, instrumentationRoutes, match); |
| 49 | + |
| 50 | + it('starts a pageload transaction when instrumentation is started', () => { |
| 51 | + const mockStartTransaction = jest.fn(); |
| 52 | + instrumentation(mockStartTransaction); |
| 53 | + expect(mockStartTransaction).toHaveBeenCalledTimes(1); |
| 54 | + expect(mockStartTransaction).toHaveBeenLastCalledWith({ |
| 55 | + name: '/', |
| 56 | + op: 'pageload', |
| 57 | + tags: { 'routing.instrumentation': 'react-router-v3' }, |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + it('does not start pageload transaction if option is false', () => { |
| 62 | + const mockStartTransaction = jest.fn(); |
| 63 | + instrumentation(mockStartTransaction, false); |
| 64 | + expect(mockStartTransaction).toHaveBeenCalledTimes(0); |
| 65 | + }); |
| 66 | + |
| 67 | + it('starts a navigation transaction', () => { |
| 68 | + const mockStartTransaction = jest.fn(); |
| 69 | + instrumentation(mockStartTransaction); |
| 70 | + render(<Router history={history}>{routes}</Router>); |
| 71 | + |
| 72 | + history.push('/about'); |
| 73 | + expect(mockStartTransaction).toHaveBeenCalledTimes(2); |
| 74 | + expect(mockStartTransaction).toHaveBeenLastCalledWith({ |
| 75 | + name: '/about', |
| 76 | + op: 'navigation', |
| 77 | + tags: { from: '/', 'routing.instrumentation': 'react-router-v3' }, |
| 78 | + }); |
| 79 | + |
| 80 | + history.push('/features'); |
| 81 | + expect(mockStartTransaction).toHaveBeenCalledTimes(3); |
| 82 | + expect(mockStartTransaction).toHaveBeenLastCalledWith({ |
| 83 | + name: '/features', |
| 84 | + op: 'navigation', |
| 85 | + tags: { from: '/about', 'routing.instrumentation': 'react-router-v3' }, |
| 86 | + }); |
| 87 | + }); |
| 88 | + |
| 89 | + it('does not start a transaction if option is false', () => { |
| 90 | + const mockStartTransaction = jest.fn(); |
| 91 | + instrumentation(mockStartTransaction, true, false); |
| 92 | + render(<Router history={history}>{routes}</Router>); |
| 93 | + expect(mockStartTransaction).toHaveBeenCalledTimes(1); |
| 94 | + }); |
| 95 | + |
| 96 | + it('only starts a navigation transaction on push', () => { |
| 97 | + const mockStartTransaction = jest.fn(); |
| 98 | + instrumentation(mockStartTransaction); |
| 99 | + render(<Router history={history}>{routes}</Router>); |
| 100 | + |
| 101 | + history.replace('hello'); |
| 102 | + expect(mockStartTransaction).toHaveBeenCalledTimes(1); |
| 103 | + }); |
| 104 | + |
| 105 | + it('finishes a transaction on navigation', () => { |
| 106 | + const mockFinish = jest.fn(); |
| 107 | + const mockStartTransaction = jest.fn().mockReturnValue({ finish: mockFinish }); |
| 108 | + instrumentation(mockStartTransaction); |
| 109 | + render(<Router history={history}>{routes}</Router>); |
| 110 | + expect(mockStartTransaction).toHaveBeenCalledTimes(1); |
| 111 | + |
| 112 | + history.push('/features'); |
| 113 | + expect(mockFinish).toHaveBeenCalledTimes(1); |
| 114 | + expect(mockStartTransaction).toHaveBeenCalledTimes(2); |
| 115 | + }); |
| 116 | + |
| 117 | + it('normalizes transaction names', () => { |
| 118 | + const mockStartTransaction = jest.fn(); |
| 119 | + instrumentation(mockStartTransaction); |
| 120 | + const { container } = render(<Router history={history}>{routes}</Router>); |
| 121 | + |
| 122 | + history.push('/users/123'); |
| 123 | + expect(container.innerHTML).toContain('123'); |
| 124 | + |
| 125 | + expect(mockStartTransaction).toHaveBeenCalledTimes(2); |
| 126 | + expect(mockStartTransaction).toHaveBeenLastCalledWith({ |
| 127 | + name: '/users/:userid', |
| 128 | + op: 'navigation', |
| 129 | + tags: { from: '/', 'routing.instrumentation': 'react-router-v3' }, |
| 130 | + }); |
| 131 | + }); |
| 132 | +}); |
0 commit comments