Skip to content

Commit 0537330

Browse files
authored
chore: added test coverage (#234)
1 parent b833f67 commit 0537330

7 files changed

+61
-8
lines changed

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

+14-2
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@
44
import { WP } from '../../src/constants';
55
import mock from '../_mock';
66
import { initPerfume } from '../../src/initPerfume';
7-
import { markStep } from '../../src/steps/markStep';
7+
import { markStep, end } from '../../src/steps/markStep';
88
import { steps } from '../../src/steps/steps';
99

10-
import { testConfig } from '../stepsTestConstants';
10+
import { testConfig, navigationTestConfig } from '../stepsTestConstants';
11+
import { config } from '../../src/config';
1112

1213
describe('markStep', () => {
1314
let spy: jest.SpyInstance;
1415

1516
beforeEach(() => {
1617
(WP as any) = mock.performance();
18+
(window as any).PerformanceObserver = mock.PerformanceObserver;
1719
initPerfume(testConfig);
1820
});
1921

@@ -45,5 +47,15 @@ describe('markStep', () => {
4547
'mark.start_navigate_to_second_screen_first_journey',
4648
);
4749
});
50+
it('shouldnt mark anything if performance is not supported', () => {
51+
spy = jest.spyOn(WP, 'mark');
52+
// @ts-ignore
53+
delete window.performance.mark;
54+
markStep('start_navigate_to_second_screen_first_journey');
55+
expect(spy.mock.calls.length).toBe(0);
56+
end('test');
57+
expect(spy.mock.calls.length).toBe(0);
58+
})
59+
4860
});
4961
});

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

+10
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { markStep } from '../../src/steps/markStep';
88
import { config } from '../../src/config';
99

1010
import { testConfig } from '../stepsTestConstants';
11+
import { measureStep } from '../../src/steps/measureStep';
1112

1213
describe('measureStep', () => {
1314
let spy: jest.SpyInstance;
@@ -138,5 +139,14 @@ describe('measureStep', () => {
138139
},
139140
});
140141
});
142+
it('should return when the start mark doesnt exist', () => {
143+
measureSpy = jest.spyOn(WP, 'measure');
144+
// ============ Mock Data ============
145+
jest.spyOn(WP, 'getEntriesByName').mockImplementationOnce(name => {
146+
return [];
147+
});
148+
measureStep('not-valid-step', 'startMark', 'endmark');
149+
expect(measureSpy).toBeCalledTimes(0);
150+
})
141151
});
142152
});

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

+25
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
} from '../../src/steps/navigationSteps';
1515

1616
import { navigationTestConfig } from '../stepsTestConstants';
17+
import { config } from '../../src/config';
1718

1819
describe('navSteps', () => {
1920
let spy: jest.SpyInstance;
@@ -66,6 +67,16 @@ describe('navSteps', () => {
6667
expect(getActiveStepsFromNavigationSteps()).toEqual({});
6768
});
6869

70+
it('it should run the navigationbased active steps', () => {
71+
config.onMarkStep = () => {};
72+
spy = jest.spyOn(config, 'onMarkStep');
73+
markStep('start_navigate_to_second_screen_first_journey');
74+
expect(spy).toHaveBeenCalledWith(
75+
'start_navigate_to_second_screen_first_journey',
76+
['load_second_screen_first_journey'],
77+
);
78+
})
79+
6980
it('returns load_home_screen as an active step', () => {
7081
markStep('start_navigate_to_second_screen_first_journey');
7182
expect(getActiveStepsFromNavigationSteps()).toEqual({
@@ -149,6 +160,20 @@ describe('navSteps', () => {
149160
load_fourth_screen_first_journey: true,
150161
load_third_screen_first_journey: true,
151162
});
163+
164+
// closing most recent step
165+
markStep('loaded_third_screen_first_journey');
166+
167+
expect(getActiveStepsFromNavigationSteps()).toEqual({
168+
load_fourth_screen_first_journey: true,
169+
load_third_screen_first_journey: true,
170+
});
152171
});
172+
173+
it('departs endmark if theres no start mark', () => {
174+
markStep('loaded_third_screen_first_journey');
175+
// never encountered a start mark so nothing is active
176+
expect(getActiveStepsFromNavigationSteps()).toEqual({});
177+
})
153178
});
154179
});

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

+8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { WP } from '../../src/constants';
55
import mock from '../_mock';
66
import { initPerfume } from '../../src/initPerfume';
77
import { steps } from '../../src/steps/steps';
8+
import { setStepsMap } from '../../src/steps/setStepsMap';
89

910
import { testConfig } from '../stepsTestConstants';
1011

@@ -85,5 +86,12 @@ describe('setSteps', () => {
8586
},
8687
});
8788
});
89+
90+
it('returns when no steps are provided', () => {
91+
initPerfume({});
92+
setStepsMap();
93+
expect(steps.finalMarkToStepsMap).toMatchObject({});
94+
expect(steps.finalMarkToStepsMap).toMatchObject({});
95+
})
8896
});
8997
});

Diff for: __tests__/stepsTestConstants.ts

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

104105
export type TestConfig = IPerfumeConfig;

Diff for: src/steps/measureStep.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,13 @@ export const measureStep = (
1212
const stepMetricName = S + step;
1313
const startMarkExists = WP.getEntriesByName(M + startMark).length > 0;
1414
const endMarkExists = WP.getEntriesByName(M + endMark).length > 0;
15-
if (!endMarkExists || !config.steps) {
15+
if (!endMarkExists || !startMarkExists || !config.steps || !config.steps[step]) {
1616
return;
1717
}
1818

1919
const { maxOutlierThreshold, vitalsThresholds } =
2020
STEP_THRESHOLDS[config.steps[step].threshold];
2121

22-
if (!startMarkExists) {
23-
return;
24-
}
2522
const stepMeasure = WP.measure(stepMetricName, M + startMark, M + endMark);
2623
const { duration } = stepMeasure;
2724
if (duration <= maxOutlierThreshold) {

Diff for: src/steps/measureSteps.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ export const measureSteps = (mark: string) => {
1717
const possibleSteps = finalSteps[startMark];
1818
possibleSteps.forEach(removeActiveStep);
1919
Promise.all(
20-
possibleSteps.map(async step => {
20+
possibleSteps.map(step => {
2121
// measure
22-
await measureStep(step, startMark, mark);
22+
measureStep(step, startMark, mark);
2323
}),
2424
).catch(() => {
2525
// TODO @zizzamia log error

0 commit comments

Comments
 (0)