-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathobserve.spec.ts
72 lines (64 loc) · 2.16 KB
/
observe.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
/**
* @jest-environment jsdom
*/
import { config } from '../src/config';
import { WP } from '../src/constants';
import { initResourceTiming } from '../src/resourceTiming';
import { initPerformanceObserver } from '../src/observe';
import { initElementTiming } from '../src/element-timing';
import * as po from '../src/performanceObserver';
import mock from './_mock';
describe('observe', () => {
let spy: jest.SpyInstance;
beforeEach(() => {
(WP as any) = mock.performance();
(window as any).PerformanceObserver = mock.PerformanceObserver;
config.isResourceTiming = false;
config.isElementTiming = false;
});
afterEach(() => {
if (spy) {
spy.mockReset();
spy.mockRestore();
}
});
describe('initPerformanceObserver()', () => {
describe.each`
resourceTiming | elementTiming | calls
${false} | ${false} | ${0}
${true} | ${false} | ${1}
${false} | ${true} | ${1}
${true} | ${true} | ${2}
`(
'when resourceTiming is $resourceTiming and elementTiming is $elementTiming',
({ resourceTiming, elementTiming, calls }) => {
beforeEach(() => {
config.isResourceTiming = resourceTiming;
config.isElementTiming = elementTiming;
spy = jest.spyOn(po, 'po');
initPerformanceObserver();
});
it(`should call po $calls times`, () => {
expect(spy.mock.calls.length).toEqual(calls);
});
it('should only call po for resource timing when is enabled', () => {
if (config.isResourceTiming) {
expect(spy).toHaveBeenCalledWith('resource', initResourceTiming);
} else {
expect(spy).not.toHaveBeenCalledWith(
'resource',
initResourceTiming,
);
}
});
it('should only call po for element timing when is enabled', () => {
if (config.isElementTiming) {
expect(spy).toHaveBeenCalledWith('element', initElementTiming);
} else {
expect(spy).not.toHaveBeenCalledWith('element', initElementTiming);
}
});
},
);
});
});