-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathrouter.test.ts
122 lines (103 loc) · 4.4 KB
/
router.test.ts
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
import { InstrumentHandlerCallback, InstrumentHandlerType } from '@sentry/utils';
import { JSDOM } from 'jsdom';
import { instrumentRoutingWithDefaults } from '../../src/browser/router';
let mockChangeHistory: ({ to, from }: { to: string; from?: string }) => void = () => undefined;
let addInstrumentationHandlerType: string = '';
jest.mock('@sentry/utils', () => {
const actual = jest.requireActual('@sentry/utils');
return {
...actual,
addInstrumentationHandler: (type: InstrumentHandlerType, callback: InstrumentHandlerCallback): void => {
addInstrumentationHandlerType = type;
mockChangeHistory = callback;
},
};
});
describe('instrumentRoutingWithDefaults', () => {
const mockFinish = jest.fn();
const customStartTransaction = jest.fn().mockReturnValue({ finish: mockFinish });
beforeEach(() => {
const dom = new JSDOM();
// @ts-ignore need to override global document
global.document = dom.window.document;
// @ts-ignore need to override global document
global.window = dom.window;
// @ts-ignore need to override global document
global.location = dom.window.location;
customStartTransaction.mockClear();
mockFinish.mockClear();
});
it('does not start transactions if global location is undefined', () => {
// @ts-ignore need to override global document
global.location = undefined;
instrumentRoutingWithDefaults(customStartTransaction);
expect(customStartTransaction).toHaveBeenCalledTimes(0);
});
it('starts a pageload transaction', () => {
instrumentRoutingWithDefaults(customStartTransaction);
expect(customStartTransaction).toHaveBeenCalledTimes(1);
expect(customStartTransaction).toHaveBeenLastCalledWith({
name: 'blank',
op: 'pageload',
metadata: { source: 'url' },
});
});
it('does not start a pageload transaction if startTransactionOnPageLoad is false', () => {
instrumentRoutingWithDefaults(customStartTransaction, false);
expect(customStartTransaction).toHaveBeenCalledTimes(0);
});
describe('navigation transaction', () => {
beforeEach(() => {
mockChangeHistory = () => undefined;
addInstrumentationHandlerType = '';
});
it('it is not created automatically', () => {
instrumentRoutingWithDefaults(customStartTransaction);
expect(customStartTransaction).not.toHaveBeenLastCalledWith({
name: 'blank',
op: 'navigation',
metadata: { source: 'url' },
});
});
it('is created on location change', () => {
instrumentRoutingWithDefaults(customStartTransaction);
mockChangeHistory({ to: 'here', from: 'there' });
expect(addInstrumentationHandlerType).toBe('history');
expect(customStartTransaction).toHaveBeenCalledTimes(2);
expect(customStartTransaction).toHaveBeenLastCalledWith({
name: 'blank',
op: 'navigation',
metadata: { source: 'url' },
});
});
it('is not created if startTransactionOnLocationChange is false', () => {
instrumentRoutingWithDefaults(customStartTransaction, true, false);
mockChangeHistory({ to: 'here', from: 'there' });
expect(addInstrumentationHandlerType).toBe('');
expect(customStartTransaction).toHaveBeenCalledTimes(1);
});
it('finishes the last active transaction', () => {
instrumentRoutingWithDefaults(customStartTransaction);
expect(mockFinish).toHaveBeenCalledTimes(0);
mockChangeHistory({ to: 'here', from: 'there' });
expect(mockFinish).toHaveBeenCalledTimes(1);
});
it('will finish active transaction multiple times', () => {
instrumentRoutingWithDefaults(customStartTransaction);
expect(mockFinish).toHaveBeenCalledTimes(0);
mockChangeHistory({ to: 'here', from: 'there' });
expect(mockFinish).toHaveBeenCalledTimes(1);
mockChangeHistory({ to: 'over/there', from: 'here' });
expect(mockFinish).toHaveBeenCalledTimes(2);
mockChangeHistory({ to: 'nowhere', from: 'over/there' });
expect(mockFinish).toHaveBeenCalledTimes(3);
});
it('not created if `from` is equal to `to`', () => {
instrumentRoutingWithDefaults(customStartTransaction);
mockChangeHistory({ to: 'first/path', from: 'first/path' });
expect(addInstrumentationHandlerType).toBe('history');
expect(customStartTransaction).toHaveBeenCalledTimes(1);
expect(customStartTransaction).not.toHaveBeenLastCalledWith('navigation');
});
});
});