forked from snowplow/snowplow-javascript-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin_features.test.ts
More file actions
97 lines (86 loc) · 3.22 KB
/
Copy pathplugin_features.test.ts
File metadata and controls
97 lines (86 loc) · 3.22 KB
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
import { SelfDescribingJson } from '@snowplow/tracker-core';
import { Plugins } from '../../src/features';
describe('Performance Navigation Timing', () => {
let windowSpy: any;
const otherContexts = {
webPage: false,
session: false,
performanceTiming: false,
gaCookies: false,
geolocation: false,
clientHints: false,
webVitals: false,
};
const hasPerformanceNavigationTimingContext = (plugins: ReturnType<typeof Plugins>): boolean => {
const pluginContexts = plugins.map((plugin) => plugin[0]?.contexts?.());
const hasPerformanceContext = pluginContexts.some((contexts?: SelfDescribingJson[]) =>
contexts?.some(
(context: { schema?: string }) => context.schema === 'iglu:org.w3/PerformanceNavigationTiming/jsonschema/1-0-0'
)
);
return hasPerformanceContext;
};
beforeEach(() => {
windowSpy = jest.spyOn(global, 'window', 'get');
// The PerformanceNavigationTiming context will only be added if the plugin can:
// - Access the `performance` object on the window
// - See that a value is returned from `getEntriesByType`
windowSpy.mockImplementation(() => ({
performance: {
getEntriesByType: () => [{}],
},
}));
});
it('Is enabled if contexts.performanceNavigationTiming is true', () => {
const plugins = Plugins({
contexts: {
performanceNavigationTiming: true,
...otherContexts,
},
});
expect(hasPerformanceNavigationTimingContext(plugins)).toBe(true);
});
it('Is disabled if contexts.performanceNavigationTiming is false', () => {
const plugins = Plugins({
contexts: {
performanceNavigationTiming: false,
...otherContexts,
},
});
expect(hasPerformanceNavigationTimingContext(plugins)).toBe(false);
});
});
describe('WebView plugin', () => {
it('WebViewPlugin is not activated when webView flag is false', () => {
jest.isolateModules(() => {
const mockWebViewPlugin = jest.fn(() => ({}));
jest.mock('@snowplow/browser-plugin-webview', () => ({
WebViewPlugin: mockWebViewPlugin,
}));
jest.mock('../../tracker.config', () => ({ webView: false }));
// Vimeo player crashes on fresh module load in jsdom; mock it to prevent that
jest.mock('@snowplow/browser-plugin-vimeo-tracking', () => ({
VimeoTrackingPlugin: jest.fn(() => ({})),
}));
const { Plugins: PluginsFresh } = require('../../src/features');
PluginsFresh({});
expect(mockWebViewPlugin).not.toHaveBeenCalled();
});
});
it('WebViewPlugin is activated when webView flag is true', () => {
jest.isolateModules(() => {
const mockWebViewPlugin = jest.fn(() => ({}));
jest.mock('@snowplow/browser-plugin-webview', () => ({
WebViewPlugin: mockWebViewPlugin,
}));
jest.mock('../../tracker.config', () => ({ webView: true }));
// Vimeo player crashes on fresh module load in jsdom; mock it to prevent that
jest.mock('@snowplow/browser-plugin-vimeo-tracking', () => ({
VimeoTrackingPlugin: jest.fn(() => ({})),
}));
const { Plugins: PluginsFresh } = require('../../src/features');
PluginsFresh({});
expect(mockWebViewPlugin).toHaveBeenCalled();
});
});
});