Skip to content

Commit

Permalink
fix: Increase Test Coverage (#249)
Browse files Browse the repository at this point in the history
Co-authored-by: Lawrence Kuang <lawrence.kuang@coinbase.com>
  • Loading branch information
lkuangPR and lkuangAlt committed Nov 10, 2023
1 parent f07e8a3 commit ba31b87
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 14 deletions.
16 changes: 16 additions & 0 deletions __tests__/initPerfume.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import { IThresholdTier } from '../src/types';
import { config } from '../src/config';
import { testConfig } from './stepsTestConstants';
import mock from './_mock';
import { isPerformanceSupported } from '../src/isSupported';

jest.mock('../src/isSupported', () => ({
isPerformanceSupported: jest.fn(),
}));

describe('Perfume', () => {
let spy: jest.SpyInstance;
Expand All @@ -22,6 +27,7 @@ describe('Perfume', () => {
(window as any).console.warn = (n: any) => n;
(observe as any).perfObservers = {};
visibility.isHidden = false;
(isPerformanceSupported as jest.Mock).mockImplementation(() => true);
});

afterEach(() => {
Expand Down Expand Up @@ -125,5 +131,15 @@ describe('Perfume', () => {
initPerfume();
expect(spy.mock.calls.length).toEqual(1);
});

it('should not initiate if isPerformanceSupported is false', () => {
(isPerformanceSupported as jest.Mock).mockImplementation(() => false);
const initPerformanceObserverSpy = jest.spyOn(
observe,
'initPerformanceObserver',
);
initPerfume(testConfig);
expect(initPerformanceObserverSpy).not.toHaveBeenCalled();
});
});
});
52 changes: 52 additions & 0 deletions __tests__/log.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ 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;
Expand All @@ -21,6 +33,7 @@ describe('log', () => {
if (spy) {
spy.mockReset();
spy.mockRestore();
jest.resetAllMocks();
}
});

Expand Down Expand Up @@ -98,5 +111,44 @@ describe('log', () => {
{ 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);
});
});
});
9 changes: 9 additions & 0 deletions __tests__/markNTBT.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { initPerfume } from '../src/perfume';
import { markNTBT } from '../src/markNTBT';
import { ntbt } from '../src/metrics';
import * as observe from '../src/observe';
import * as log from '../src/log';
import { visibility } from '../src/onVisibilityChange';
import mock from './_mock';

Expand Down Expand Up @@ -40,5 +41,13 @@ describe('Perfume', () => {
markNTBT();
expect(ntbt.value).toEqual(0);
});

it('should call logMetric', () => {
jest.useFakeTimers();
const logMetricSpy = jest.spyOn(log, 'logMetric');
markNTBT();
jest.advanceTimersByTime(2000);
expect(logMetricSpy).toHaveBeenCalled();
});
});
});
2 changes: 1 addition & 1 deletion __tests__/steps/measureStep.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ describe('measureStep', () => {
navigationType: undefined,
navigatorInformation: {
deviceMemory: 0,
hardwareConcurrency: 12,
hardwareConcurrency: 16,
isLowEndDevice: false,
isLowEndExperience: false,
serviceWorkerStatus: 'unsupported',
Expand Down
2 changes: 1 addition & 1 deletion __tests__/steps/onMarkJourney.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('markStep', () => {
markStep('loaded_second_screen_first_journey');
expect(onMarkStepSpy).toHaveBeenLastCalledWith(
'loaded_second_screen_first_journey',
[],
['load_second_screen_first_journey'],
);
});
});
Expand Down
1 change: 0 additions & 1 deletion __tests__/stepsTestConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ export const testConfig: IPerfumeOptions = {
export const navigationTestConfig: IPerfumeOptions = {
steps,
onMarkStep: jest.fn(),
enableNavigationTracking: true,
};

export type TestConfig = IPerfumeConfig;
14 changes: 13 additions & 1 deletion __tests__/vitalsScore.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @jest-environment jsdom
*/
import { webVitalsScore, getVitalsScore } from '../src/vitalsScore';
import { webVitalsScore, getVitalsScore, getRating } from '../src/vitalsScore';

describe('vitalsScore', () => {
describe('webVitalsScore', () => {
Expand Down Expand Up @@ -34,5 +34,17 @@ describe('vitalsScore', () => {
expect(getVitalsScore('NTBT', 201)).toEqual('needsImprovement');
expect(getVitalsScore('NTBT', 700)).toEqual('poor');
});

it('should return null if vital does not exist', () => {
expect(getVitalsScore('InvalidVital', 100)).toBeNull();
});
});

describe('getRating()', () => {
it('should return the correct values', () => {
expect(getRating(50, [100, 200])).toEqual('good');
expect(getRating(150, [100, 200])).toEqual('needsImprovement');
expect(getRating(250, [100, 200])).toEqual('poor');
});
});
});
3 changes: 0 additions & 3 deletions src/isLowEnd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@ export const getIsLowEndExperience = (
switch (et) {
case 'slow-2g':
return true;
break;
case '2g':
return true;
break;
case '3g':
return true;
break;
default:
// Data Saver preference
return getIsLowEndDevice() || sd;
Expand Down
9 changes: 2 additions & 7 deletions src/vitalsScore.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
IPerfumeData,
IThresholdTier,
IStepsThresholds,
IVitalsScore,
} from './types';
import { IThresholdTier, IStepsThresholds, IVitalsScore } from './types';

const rtScore = [100, 200];
const tbtScore = [200, 600];
Expand Down Expand Up @@ -50,7 +45,7 @@ export const getRating = (

export const getVitalsScore = (
measureName: string,
value: IPerfumeData,
value: number,
): IVitalsScore => {
if (!webVitalsScore[measureName]) {
return null;
Expand Down

0 comments on commit ba31b87

Please sign in to comment.