forked from snowplow/snowplow-javascript-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient-hints.test.ts
More file actions
170 lines (148 loc) · 4.78 KB
/
Copy pathclient-hints.test.ts
File metadata and controls
170 lines (148 loc) · 4.78 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { buildLinkClick, trackerCore } from '@snowplow/tracker-core';
import { BrowserTracker } from '@snowplow/browser-tracker-core';
import { JSDOM } from 'jsdom';
import { setImmediate } from 'timers';
import { ClientHintsPlugin } from '../src';
declare var jsdom: JSDOM;
describe('Client Hints plugin', () => {
const ctxSchema = 'iglu:com.snowplowanalytics.snowplow/contexts/jsonschema/1-0-0';
const hintsSchema = 'iglu:org.ietf/http_client_hints/jsonschema/1-0-0';
it('Attaches no context on undefined userAgentData', (done) => {
const plugin = ClientHintsPlugin(false);
const core = trackerCore({
corePlugins: [plugin],
callback: (payloadBuilder) => {
const json = payloadBuilder.getJson().filter((e) => e.keyIfEncoded === 'cx');
expect(json.length).toBe(0);
done();
},
});
plugin.activateBrowserPlugin?.({ core } as BrowserTracker);
core.track(buildLinkClick({ targetUrl: 'https://example.com' }));
});
it('Attaches no context on invalid userAgentData(no mobile)', (done) => {
const sampleUserAgentData = {
brands: [
{
brand: 'Opera GX',
version: '98',
},
],
} as any;
Object.defineProperty(jsdom.window.navigator, 'userAgentData', {
value: sampleUserAgentData,
configurable: true,
});
const plugin = ClientHintsPlugin(false);
const core = trackerCore({
corePlugins: [plugin],
callback: (payloadBuilder) => {
const json = payloadBuilder.getJson().filter((e) => e.keyIfEncoded === 'cx');
expect(json.length).toBe(0);
done();
},
});
plugin.activateBrowserPlugin?.({ core } as BrowserTracker);
core.track(buildLinkClick({ targetUrl: 'https://example.com' }));
});
it('Attaches no context on invalid userAgentData(no brands)', (done) => {
const sampleUserAgentData = {
mobile: true,
} as any;
Object.defineProperty(jsdom.window.navigator, 'userAgentData', {
value: sampleUserAgentData,
configurable: true,
});
const plugin = ClientHintsPlugin(false);
const core = trackerCore({
corePlugins: [plugin],
callback: (payloadBuilder) => {
const json = payloadBuilder.getJson().filter((e) => e.keyIfEncoded === 'cx');
expect(json.length).toBe(0);
done();
},
});
plugin.activateBrowserPlugin?.({ core } as BrowserTracker);
core.track(buildLinkClick({ targetUrl: 'https://example.com' }));
});
it('Attaches no context on invalid userAgentData(no valid brands)', (done) => {
const sampleUserAgentData = {
mobile: false,
brands: [
{
brand: 'Opera GX',
version: 98,
},
],
} as any;
Object.defineProperty(jsdom.window.navigator, 'userAgentData', {
value: sampleUserAgentData,
configurable: true,
});
const plugin = ClientHintsPlugin(true);
const core = trackerCore({
corePlugins: [plugin],
callback: (payloadBuilder) => {
const json = payloadBuilder.getJson().filter((e) => e.keyIfEncoded === 'cx');
expect(json.length).toBe(0);
done();
},
});
plugin.activateBrowserPlugin?.({ core } as BrowserTracker);
core.track(buildLinkClick({ targetUrl: 'https://example.com' }));
});
it('Attaches context on valid userAgentData properties', async () => {
const highEntropyVals = {
platform: 'PhoneOS',
platformVersion: '10A',
architecture: 'arm',
model: 'X633GTM',
uaFullVersion: '73.32.AGX.5',
};
const sampleUserAgentData = {
brands: [
{
brand: 'Chromium',
version: '119',
},
{
brand: 'Not?A_Brand',
version: '24',
},
],
mobile: false,
getHighEntropyValues: (_: any) => Promise.resolve(highEntropyVals),
};
const expected = {
schema: ctxSchema,
data: [
{
schema: hintsSchema,
data: Object.assign(
{
isMobile: false,
brands: sampleUserAgentData.brands,
},
highEntropyVals
),
},
],
};
Object.defineProperty(jsdom.window.navigator, 'userAgentData', {
value: sampleUserAgentData,
configurable: true,
});
const plugin = ClientHintsPlugin(true);
const core = trackerCore({
corePlugins: [plugin],
callback: (payloadBuilder) => {
const json = payloadBuilder.getJson().filter((e) => e.keyIfEncoded === 'cx');
expect(json[0].json).toMatchObject(expected);
},
});
plugin.activateBrowserPlugin?.({ core } as BrowserTracker);
const flushPromises = () => Promise.resolve(setImmediate);
await flushPromises();
core.track(buildLinkClick({ targetUrl: 'https://example.com' }));
});
});