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