-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathlog.spec.ts
154 lines (140 loc) · 4.14 KB
/
log.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
/**
* @jest-environment jsdom
*/
import { C, WN, WP } from '../src/constants';
import * as log from '../src/log';
import * as reportPerf from '../src/reportPerf';
import * as totalBlockingTime from '../src/totalBlockingTime';
import * as utils from '../src/utils';
import mock from './_mock';
import * as onVisibilityChange from '../src/onVisibilityChange';
jest.mock('../src/onVisibilityChange', () => {
const originalModule = jest.requireActual('../src/onVisibilityChange');
return {
...originalModule,
visibility: {
isHidden: false,
didChange: false,
},
};
});
describe('log', () => {
let spy: jest.SpyInstance;
beforeEach(() => {
(WN as any) = mock.navigator();
(WP as any) = mock.performance();
(C as any).log = (n: any) => n;
});
afterEach(() => {
if (spy) {
spy.mockReset();
spy.mockRestore();
jest.resetAllMocks();
}
});
describe('.logData()', () => {
it('should call pushTask', () => {
spy = jest.spyOn(utils, 'pushTask');
log.logData('measureName', {});
expect(spy.mock.calls.length).toEqual(1);
});
});
describe('.logMetric()', () => {
it('should call reportPerf() when value is 0', () => {
spy = jest.spyOn(reportPerf, 'reportPerf');
log.logMetric({
attribution: {},
name: 'CLS',
rating: 'good',
value: 0,
navigationType: 'navigate',
});
expect(spy.mock.calls.length).toEqual(1);
expect(spy).toHaveBeenCalledWith('CLS', 0, 'good', {}, 'navigate');
});
it('should call reportPerf() with the correct arguments', () => {
spy = jest.spyOn(reportPerf, 'reportPerf');
log.logMetric({
attribution: {},
name: 'FCP',
rating: 'good',
value: 1,
navigationType: 'navigate',
});
expect(spy.mock.calls.length).toEqual(1);
expect(spy).toHaveBeenCalledWith('FCP', 1, 'good', {}, 'navigate');
});
it('should not call reportPerf() when duration is higher of config.maxTime', () => {
spy = jest.spyOn(reportPerf, 'reportPerf');
log.logMetric({
attribution: {},
name: 'FCP',
rating: 'good',
value: 40000,
});
expect(spy.mock.calls.length).toEqual(0);
});
it('should call initTotalBlockingTime only once', () => {
const tbtSpy = jest.spyOn(totalBlockingTime, 'initTotalBlockingTime');
(window as any).PerformanceObserver = mock.PerformanceObserver;
// FCP and LCP both trigger TBT measurement
log.logMetric({
attribution: {},
name: 'FCP',
rating: 'good',
value: 1,
navigationType: 'navigate',
});
log.logMetric({
attribution: {},
name: 'LCP',
rating: 'good',
value: 1,
navigationType: 'navigate',
});
expect(tbtSpy.mock.calls.length).toEqual(1);
expect(tbtSpy).toHaveBeenCalledWith([
{ name: 'first-paint', startTime: 1 },
{ name: 'first-contentful-paint', startTime: 1 },
{ duration: 4, name: 'mousedown' },
]);
});
it('should call logData with FID metrics', () => {
jest.useFakeTimers();
const logDataSpy = jest.spyOn(log, 'logData');
log.logMetric({
attribution: {},
name: 'FID',
rating: 'good',
value: 0,
navigationType: 'navigate',
});
jest.advanceTimersByTime(10000);
expect(logDataSpy.mock.calls.length).toEqual(1);
expect(logDataSpy).toHaveBeenCalledWith('dataConsumption', {
beacon: 0,
css: 0,
fetch: 0,
img: 0,
other: 0,
script: 0,
total: 0,
xmlhttprequest: 0,
});
});
it('should not call logData with FID metrics if visibility did change', () => {
jest.useFakeTimers();
const logDataSpy = jest.spyOn(log, 'logData');
onVisibilityChange.visibility.didChange = true;
log.logMetric({
attribution: {},
name: 'FID',
rating: 'good',
value: 0,
navigationType: 'navigate',
});
jest.advanceTimersByTime(10000);
expect(logDataSpy.mock.calls.length).toEqual(0);
});
});
});