-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathinitPerfume.spec.ts
145 lines (129 loc) · 4.25 KB
/
initPerfume.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
/**
* @jest-environment jsdom
*/
import { C, WN, WP } from '../src/constants';
import { initPerfume } from '../src/initPerfume';
import * as observe from '../src/observe';
import { visibility } from '../src/onVisibilityChange';
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;
beforeEach(() => {
(WN as any) = mock.navigator();
(WP as any) = mock.performance();
initPerfume({ ...mock.defaultPerfumeConfig });
(window as any).PerformanceObserver = mock.PerformanceObserver;
(C as any).log = (n: any) => n;
(window as any).console.warn = (n: any) => n;
(observe as any).perfObservers = {};
visibility.isHidden = false;
(isPerformanceSupported as jest.Mock).mockImplementation(() => true);
});
afterEach(() => {
if (spy) {
spy.mockReset();
spy.mockRestore();
}
});
describe('constructor', () => {
it('should run with config version A', () => {
initPerfume({});
});
it('should run with config version B', () => {
initPerfume({
resourceTiming: true,
});
});
it('should run with config version C', () => {
initPerfume({
elementTiming: true,
});
});
it('should run with config version D', () => {
initPerfume({
resourceTiming: true,
elementTiming: true,
});
});
it('should run with steps config version B', () => {
initPerfume({
steps: {
load_first_screen_first_journey: {
threshold: IThresholdTier.unavoidable,
marks: ['launch', 'loaded_first_screen_first_journey'],
},
load_second_screen_first_journey: {
threshold: IThresholdTier.instant,
marks: [
'start_navigate_to_second_screen_first_journey',
'loaded_second_screen_first_journey',
],
},
},
});
});
it('should run with steps config version D', () => {
initPerfume({
onMarkStep: () => {},
});
});
it('should run with steps config version E', () => {
initPerfume({
steps: {
load_first_screen_first_journey: {
threshold: IThresholdTier.unavoidable,
marks: ['launch', 'loaded_first_screen_first_journey'],
},
load_second_screen_first_journey: {
threshold: IThresholdTier.instant,
marks: [
'start_navigate_to_second_screen_first_journey',
'loaded_second_screen_first_journey',
],
},
},
});
});
it('should run with all userJourney config', () => {
expect(config).not.toMatchObject(testConfig);
initPerfume(testConfig);
expect(config).toMatchObject(testConfig);
});
it('when navigator is not supported should not call WN.storage.estimate()', () => {
(WN as any) = mock.navigator();
const spy = jest.spyOn(WN.storage, 'estimate');
(WN as any).storage = undefined;
initPerfume();
expect(spy.mock.calls.length).toEqual(0);
});
it('when WN.storage.estimate() is not defined should not call it', () => {
(WN as any) = mock.navigator();
const spy = jest.spyOn(WN.storage, 'estimate');
(WN as any).storage.estimate = undefined;
expect(() => initPerfume()).not.toThrow(TypeError);
expect(spy.mock.calls.length).toEqual(0);
});
it('when navigator is supported should call WN.storage.estimate()', () => {
(WN as any) = mock.navigator();
const spy = jest.spyOn(WN.storage, 'estimate');
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();
});
});
});