-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathreportPerf.spec.ts
160 lines (146 loc) · 4.84 KB
/
reportPerf.spec.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* @jest-environment jsdom
*/
import { config } from '../src/config';
import { W, WN, WP } from '../src/constants';
import { visibility } from '../src/onVisibilityChange';
import { reportPerf } from '../src/reportPerf';
import mock from './_mock';
describe('reportPerf', () => {
let spy: jest.SpyInstance;
beforeEach(() => {
visibility.isHidden = false;
(WN as any) = mock.navigator();
(WP as any) = mock.performance();
});
describe('reportPerf()', () => {
it('should not call analyticsTracker() if isHidden is true', () => {
visibility.isHidden = true;
config.analyticsTracker = () => {};
spy = jest.spyOn(config, 'analyticsTracker');
reportPerf('FCP', 0.01, 'good', {});
expect(spy.mock.calls.length).toEqual(0);
});
it('should not call analyticsTracker() if isHidden is true and measureName is CLS', () => {
visibility.isHidden = true;
config.analyticsTracker = () => {};
spy = jest.spyOn(config, 'analyticsTracker');
reportPerf('CLS', 123, 'good', {});
expect(spy.mock.calls.length).toEqual(1);
expect(spy).toHaveBeenCalledWith({
metricName: 'CLS',
data: 123,
attribution: {},
navigatorInformation: {
deviceMemory: 8,
hardwareConcurrency: 12,
isLowEndDevice: false,
isLowEndExperience: false,
serviceWorkerStatus: 'unsupported',
},
rating: 'good',
});
});
it('should not call analyticsTracker() if isHidden is true and measureName is INP', () => {
visibility.isHidden = true;
config.analyticsTracker = () => {};
spy = jest.spyOn(config, 'analyticsTracker');
reportPerf('INP', 123, 'good', {});
expect(spy.mock.calls.length).toEqual(1);
expect(spy).toHaveBeenCalledWith({
metricName: 'INP',
data: 123,
attribution: {},
navigatorInformation: {
deviceMemory: 8,
hardwareConcurrency: 12,
isLowEndDevice: false,
isLowEndExperience: false,
serviceWorkerStatus: 'unsupported',
},
rating: 'good',
});
});
it('should call analyticsTracker() if analyticsTracker is defined', () => {
config.analyticsTracker = () => {};
spy = jest.spyOn(config, 'analyticsTracker');
reportPerf('FCP', 123, 'good', {});
expect(spy.mock.calls.length).toEqual(1);
expect(spy).toHaveBeenCalledWith({
metricName: 'FCP',
data: 123,
attribution: {},
navigatorInformation: {
deviceMemory: 8,
hardwareConcurrency: 12,
isLowEndDevice: false,
isLowEndExperience: false,
serviceWorkerStatus: 'unsupported',
},
rating: 'good',
});
});
it('should call analyticsTracker() with customProperties', () => {
config.analyticsTracker = () => {};
spy = jest.spyOn(config, 'analyticsTracker');
reportPerf('TBT', 423, 'needsImprovement', { mare: 'sea' });
expect(spy.mock.calls.length).toEqual(1);
expect(spy).toHaveBeenCalledWith({
metricName: 'TBT',
data: 423,
attribution: { mare: 'sea' },
navigatorInformation: {
deviceMemory: 8,
hardwareConcurrency: 12,
isLowEndDevice: false,
isLowEndExperience: false,
serviceWorkerStatus: 'unsupported',
},
rating: 'needsImprovement',
});
});
it('CLS and INP should be reported immediately without requestIdleCallback', () => {
config.analyticsTracker = () => {};
spy = jest.spyOn(config, 'analyticsTracker');
// emulate requestIdleCallback delay
const tasks: Function[] = [];
(W as any).requestIdleCallback = (cb: Function) => tasks.push(cb);
const immediatelyReportedMetrics = ['CLS', 'INP'];
const delayedMetrics = ['TTFB', 'FCP', 'LCP', 'FID', 'TBT'];
const defaultData = {
data: 123,
attribution: {},
navigationType: undefined,
navigatorInformation: {
deviceMemory: 8,
hardwareConcurrency: 12,
isLowEndDevice: false,
isLowEndExperience: false,
serviceWorkerStatus: 'unsupported',
},
rating: 'good',
};
[...immediatelyReportedMetrics, ...delayedMetrics].forEach(metric => {
reportPerf(metric, 123, 'good', {});
});
expect(spy.mock.calls).toEqual(
immediatelyReportedMetrics.map(metric => [
{
metricName: metric,
...defaultData,
},
]),
);
// run requestIdleCallback tasks
tasks.forEach(task => task());
expect(spy.mock.calls).toEqual(
[...immediatelyReportedMetrics, ...delayedMetrics].map(metric => [
{
metricName: metric,
...defaultData,
},
]),
);
});
});
});