Skip to content

Commit ba31b87

Browse files
lkuangPRlkuangAlt
andauthored
fix: Increase Test Coverage (#249)
Co-authored-by: Lawrence Kuang <lawrence.kuang@coinbase.com>
1 parent f07e8a3 commit ba31b87

9 files changed

+94
-14
lines changed

Diff for: __tests__/initPerfume.spec.ts

+16
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ import { IThresholdTier } from '../src/types';
99
import { config } from '../src/config';
1010
import { testConfig } from './stepsTestConstants';
1111
import mock from './_mock';
12+
import { isPerformanceSupported } from '../src/isSupported';
13+
14+
jest.mock('../src/isSupported', () => ({
15+
isPerformanceSupported: jest.fn(),
16+
}));
1217

1318
describe('Perfume', () => {
1419
let spy: jest.SpyInstance;
@@ -22,6 +27,7 @@ describe('Perfume', () => {
2227
(window as any).console.warn = (n: any) => n;
2328
(observe as any).perfObservers = {};
2429
visibility.isHidden = false;
30+
(isPerformanceSupported as jest.Mock).mockImplementation(() => true);
2531
});
2632

2733
afterEach(() => {
@@ -125,5 +131,15 @@ describe('Perfume', () => {
125131
initPerfume();
126132
expect(spy.mock.calls.length).toEqual(1);
127133
});
134+
135+
it('should not initiate if isPerformanceSupported is false', () => {
136+
(isPerformanceSupported as jest.Mock).mockImplementation(() => false);
137+
const initPerformanceObserverSpy = jest.spyOn(
138+
observe,
139+
'initPerformanceObserver',
140+
);
141+
initPerfume(testConfig);
142+
expect(initPerformanceObserverSpy).not.toHaveBeenCalled();
143+
});
128144
});
129145
});

Diff for: __tests__/log.spec.ts

+52
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ import * as reportPerf from '../src/reportPerf';
77
import * as totalBlockingTime from '../src/totalBlockingTime';
88
import * as utils from '../src/utils';
99
import mock from './_mock';
10+
import * as onVisibilityChange from '../src/onVisibilityChange';
11+
12+
jest.mock('../src/onVisibilityChange', () => {
13+
const originalModule = jest.requireActual('../src/onVisibilityChange');
14+
return {
15+
...originalModule,
16+
visibility: {
17+
isHidden: false,
18+
didChange: false,
19+
},
20+
};
21+
});
1022

1123
describe('log', () => {
1224
let spy: jest.SpyInstance;
@@ -21,6 +33,7 @@ describe('log', () => {
2133
if (spy) {
2234
spy.mockReset();
2335
spy.mockRestore();
36+
jest.resetAllMocks();
2437
}
2538
});
2639

@@ -98,5 +111,44 @@ describe('log', () => {
98111
{ duration: 4, name: 'mousedown' },
99112
]);
100113
});
114+
115+
it('should call logData with FID metrics', () => {
116+
jest.useFakeTimers();
117+
const logDataSpy = jest.spyOn(log, 'logData');
118+
log.logMetric({
119+
attribution: {},
120+
name: 'FID',
121+
rating: 'good',
122+
value: 0,
123+
navigationType: 'navigate',
124+
});
125+
jest.advanceTimersByTime(10000);
126+
expect(logDataSpy.mock.calls.length).toEqual(1);
127+
expect(logDataSpy).toHaveBeenCalledWith('dataConsumption', {
128+
beacon: 0,
129+
css: 0,
130+
fetch: 0,
131+
img: 0,
132+
other: 0,
133+
script: 0,
134+
total: 0,
135+
xmlhttprequest: 0,
136+
});
137+
});
138+
139+
it('should not call logData with FID metrics if visibility did change', () => {
140+
jest.useFakeTimers();
141+
const logDataSpy = jest.spyOn(log, 'logData');
142+
onVisibilityChange.visibility.didChange = true;
143+
log.logMetric({
144+
attribution: {},
145+
name: 'FID',
146+
rating: 'good',
147+
value: 0,
148+
navigationType: 'navigate',
149+
});
150+
jest.advanceTimersByTime(10000);
151+
expect(logDataSpy.mock.calls.length).toEqual(0);
152+
});
101153
});
102154
});

Diff for: __tests__/markNTBT.spec.ts

+9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { initPerfume } from '../src/perfume';
66
import { markNTBT } from '../src/markNTBT';
77
import { ntbt } from '../src/metrics';
88
import * as observe from '../src/observe';
9+
import * as log from '../src/log';
910
import { visibility } from '../src/onVisibilityChange';
1011
import mock from './_mock';
1112

@@ -40,5 +41,13 @@ describe('Perfume', () => {
4041
markNTBT();
4142
expect(ntbt.value).toEqual(0);
4243
});
44+
45+
it('should call logMetric', () => {
46+
jest.useFakeTimers();
47+
const logMetricSpy = jest.spyOn(log, 'logMetric');
48+
markNTBT();
49+
jest.advanceTimersByTime(2000);
50+
expect(logMetricSpy).toHaveBeenCalled();
51+
});
4352
});
4453
});

Diff for: __tests__/steps/measureStep.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ describe('measureStep', () => {
132132
navigationType: undefined,
133133
navigatorInformation: {
134134
deviceMemory: 0,
135-
hardwareConcurrency: 12,
135+
hardwareConcurrency: 16,
136136
isLowEndDevice: false,
137137
isLowEndExperience: false,
138138
serviceWorkerStatus: 'unsupported',

Diff for: __tests__/steps/onMarkJourney.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('markStep', () => {
4040
markStep('loaded_second_screen_first_journey');
4141
expect(onMarkStepSpy).toHaveBeenLastCalledWith(
4242
'loaded_second_screen_first_journey',
43-
[],
43+
['load_second_screen_first_journey'],
4444
);
4545
});
4646
});

Diff for: __tests__/stepsTestConstants.ts

-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ export const testConfig: IPerfumeOptions = {
9999
export const navigationTestConfig: IPerfumeOptions = {
100100
steps,
101101
onMarkStep: jest.fn(),
102-
enableNavigationTracking: true,
103102
};
104103

105104
export type TestConfig = IPerfumeConfig;

Diff for: __tests__/vitalsScore.spec.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* @jest-environment jsdom
33
*/
4-
import { webVitalsScore, getVitalsScore } from '../src/vitalsScore';
4+
import { webVitalsScore, getVitalsScore, getRating } from '../src/vitalsScore';
55

66
describe('vitalsScore', () => {
77
describe('webVitalsScore', () => {
@@ -34,5 +34,17 @@ describe('vitalsScore', () => {
3434
expect(getVitalsScore('NTBT', 201)).toEqual('needsImprovement');
3535
expect(getVitalsScore('NTBT', 700)).toEqual('poor');
3636
});
37+
38+
it('should return null if vital does not exist', () => {
39+
expect(getVitalsScore('InvalidVital', 100)).toBeNull();
40+
});
41+
});
42+
43+
describe('getRating()', () => {
44+
it('should return the correct values', () => {
45+
expect(getRating(50, [100, 200])).toEqual('good');
46+
expect(getRating(150, [100, 200])).toEqual('needsImprovement');
47+
expect(getRating(250, [100, 200])).toEqual('poor');
48+
});
3749
});
3850
});

Diff for: src/isLowEnd.ts

-3
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,10 @@ export const getIsLowEndExperience = (
2222
switch (et) {
2323
case 'slow-2g':
2424
return true;
25-
break;
2625
case '2g':
2726
return true;
28-
break;
2927
case '3g':
3028
return true;
31-
break;
3229
default:
3330
// Data Saver preference
3431
return getIsLowEndDevice() || sd;

Diff for: src/vitalsScore.ts

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import {
2-
IPerfumeData,
3-
IThresholdTier,
4-
IStepsThresholds,
5-
IVitalsScore,
6-
} from './types';
1+
import { IThresholdTier, IStepsThresholds, IVitalsScore } from './types';
72

83
const rtScore = [100, 200];
94
const tbtScore = [200, 600];
@@ -50,7 +45,7 @@ export const getRating = (
5045

5146
export const getVitalsScore = (
5247
measureName: string,
53-
value: IPerfumeData,
48+
value: number,
5449
): IVitalsScore => {
5550
if (!webVitalsScore[measureName]) {
5651
return null;

0 commit comments

Comments
 (0)