forked from snowplow/snowplow-javascript-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebview.test.ts
More file actions
202 lines (176 loc) · 5.77 KB
/
Copy pathwebview.test.ts
File metadata and controls
202 lines (176 loc) · 5.77 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import { addTracker, SharedState, EventStore, BrowserTracker } from '@snowplow/browser-tracker-core';
import { newInMemoryEventStore, buildSelfDescribingEvent } from '@snowplow/tracker-core';
import { WebViewPlugin } from '../src';
import { hasMobileInterface, trackWebViewEvent } from '@snowplow/webview-tracker';
jest.mock('@snowplow/webview-tracker');
describe('WebView plugin', () => {
afterEach(() => {
jest.clearAllMocks();
});
let idx = 1;
let eventStore: EventStore;
let tracker: BrowserTracker | null;
let mockHasMobileInterface = hasMobileInterface as jest.Mock<boolean>;
let mockTrackWebViewEvent = trackWebViewEvent as jest.Mock<void>;
it('Does not filter events if mobile interface not found', async () => {
mockHasMobileInterface.mockImplementation(() => {
return false;
});
eventStore = newInMemoryEventStore({});
const customFetch = async () => new Response(null, { status: 500 });
tracker = addTracker(`sp${idx++}`, `sp${idx++}`, 'js-4.0.0', '', new SharedState(), {
plugins: [WebViewPlugin()],
eventStore,
customFetch,
});
tracker?.trackPageView();
let events = await eventStore.getAllPayloads();
expect(events).toHaveLength(1);
expect(mockTrackWebViewEvent).not.toHaveBeenCalled();
});
it('Filters out the events if a mobile interface is present', async () => {
mockHasMobileInterface.mockImplementation(() => {
return true;
});
eventStore = newInMemoryEventStore({});
const customFetch = async () => new Response(null, { status: 500 });
tracker = addTracker(`sp${idx++}`, `sp${idx++}`, 'js-4.0.0', '', new SharedState(), {
plugins: [WebViewPlugin()],
eventStore,
customFetch,
});
tracker?.trackPageView();
tracker?.core.track(
buildSelfDescribingEvent({
event: {
schema: 'iglu:com.snowplowanalytics.snowplow.media/play_event/jsonschema/1-0-0',
data: {
a: 'b',
},
},
})
);
let events = await eventStore.getAllPayloads();
expect(events).toHaveLength(0);
expect(mockTrackWebViewEvent).toHaveBeenCalled();
let calls = mockTrackWebViewEvent.mock.calls;
// tracked two events
expect(calls).toHaveLength(2);
// page view event properties
expect(calls[0][0]).toMatchObject({
properties: {
eventName: 'pv',
trackerVersion: 'js-4.0.0',
useragent: expect.any(String),
pageUrl: expect.any(String),
},
context: [
{
schema: 'iglu:com.snowplowanalytics.snowplow/web_page/jsonschema/1-0-0',
data: {
id: expect.any(String),
},
},
],
});
// self-describing event properties
expect(calls[1][0]).toMatchObject({
properties: {
eventName: 'ue',
trackerVersion: 'js-4.0.0',
useragent: expect.any(String),
pageUrl: expect.any(String),
pageTitle: undefined,
referrer: undefined,
category: undefined,
action: undefined,
label: undefined,
property: undefined,
value: undefined,
pingXOffsetMin: undefined,
pingXOffsetMax: undefined,
pingYOffsetMin: undefined,
pingYOffsetMax: undefined,
},
event: {
schema: 'iglu:com.snowplowanalytics.snowplow.media/play_event/jsonschema/1-0-0',
data: { a: 'b' },
},
context: [
{
schema: 'iglu:com.snowplowanalytics.snowplow/web_page/jsonschema/1-0-0',
data: {
id: expect.any(String),
},
},
],
});
// no tracker namespaces provided
expect(calls[0][1]).toBeUndefined();
});
it('Decodes base64-encoded payloads', async () => {
mockHasMobileInterface.mockImplementation(() => {
return true;
});
eventStore = newInMemoryEventStore({});
const customFetch = async () => new Response(null, { status: 500 });
tracker = addTracker(`sp${idx++}`, `sp${idx++}`, 'js-4.0.0', '', new SharedState(), {
plugins: [WebViewPlugin()],
eventStore,
customFetch,
});
tracker?.core.setBase64Encoding(true);
tracker?.core.track(
buildSelfDescribingEvent({
event: {
schema: 'iglu:com.snowplowanalytics.snowplow/example/jsonschema/1-0-0',
data: {
hello: 'world',
},
},
})
);
let events = await eventStore.getAllPayloads();
expect(events).toHaveLength(0);
expect(mockTrackWebViewEvent).toHaveBeenCalled();
let calls = mockTrackWebViewEvent.mock.calls;
expect(calls).toHaveLength(1);
// decoded event properties
expect(calls[0][0]).toMatchObject({
properties: {
eventName: 'ue',
trackerVersion: 'js-4.0.0',
useragent: expect.any(String),
pageUrl: expect.any(String),
},
event: {
schema: 'iglu:com.snowplowanalytics.snowplow/example/jsonschema/1-0-0',
data: { hello: 'world' },
},
context: [
{
schema: 'iglu:com.snowplowanalytics.snowplow/web_page/jsonschema/1-0-0',
data: {
id: expect.any(String),
},
},
],
});
});
it('Passes a configured list of tracker namespaces', async () => {
mockHasMobileInterface.mockImplementation(() => {
return true;
});
eventStore = newInMemoryEventStore({});
const customFetch = async () => new Response(null, { status: 500 });
tracker = addTracker(`sp${idx++}`, `sp${idx++}`, 'js-4.0.0', '', new SharedState(), {
plugins: [WebViewPlugin({ trackerNamespaces: ['sp1', 'sp2'] })],
eventStore,
customFetch,
});
tracker?.trackPageView();
let calls = mockTrackWebViewEvent.mock.calls;
expect(calls).toHaveLength(1);
expect(calls[0][1]).toEqual(['sp1', 'sp2']);
});
});