forked from snowplow/snowplow-javascript-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreen.test.ts
More file actions
110 lines (95 loc) · 3.15 KB
/
Copy pathscreen.test.ts
File metadata and controls
110 lines (95 loc) · 3.15 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
import { addTracker, SharedState, EventStore, BrowserTracker } from '@snowplow/browser-tracker-core';
import { ScreenTrackingPlugin, trackScreenView } from '../src';
import { newInMemoryEventStore } from '@snowplow/tracker-core';
import { SCREEN_ENTITY_SCHEMA, SCREEN_VIEW_EVENT_SCHEMA } from '../src/schemata';
describe('ScreenTrackingPlugin', () => {
let idx = 1;
let eventStore: EventStore;
let tracker: BrowserTracker | null;
describe('Enabled screen context tracking', () => {
beforeEach(() => {
eventStore = newInMemoryEventStore({});
const customFetch = async () => new Response(null, { status: 500 });
tracker = addTracker(`sp${idx++}`, `sp${idx++}`, 'js-3.0.0', '', new SharedState(), {
plugins: [ScreenTrackingPlugin()],
eventStore,
customFetch,
contexts: { webPage: false },
});
});
it('adds id and previous screen view references', async () => {
trackScreenView({
id: '1',
name: 'Home',
});
let [{ ue_pr }] = await eventStore.getAllPayloads();
let event = JSON.parse(ue_pr as string).data;
expect(event.schema).toBe(SCREEN_VIEW_EVENT_SCHEMA);
expect(event.data).toMatchObject({
name: 'Home',
id: '1',
});
trackScreenView({
name: 'About',
});
[, , { ue_pr }] = await eventStore.getAllPayloads();
event = JSON.parse(ue_pr as string).data;
expect(event.schema).toBe(SCREEN_VIEW_EVENT_SCHEMA);
expect(event.data).toMatchObject({
name: 'About',
id: expect.any(String),
previousName: 'Home',
previousId: '1',
});
});
it('adds screen context entity to all events', async () => {
trackScreenView({
id: '1',
name: 'Home',
});
let [{ co }] = await eventStore.getAllPayloads();
let context = JSON.parse(co as string).data;
expect(context).toEqual([
{
schema: SCREEN_ENTITY_SCHEMA,
data: {
name: 'Home',
id: '1',
},
},
]);
tracker?.trackPageView();
[, { co }] = await eventStore.getAllPayloads();
context = JSON.parse(co as string).data;
expect(context).toEqual([
{
schema: SCREEN_ENTITY_SCHEMA,
data: {
name: 'Home',
id: '1',
},
},
]);
});
});
describe('Disabled screen context tracking', () => {
beforeEach(() => {
eventStore = newInMemoryEventStore({});
const customFetch = async () => new Response(null, { status: 500 });
tracker = addTracker(`sp${idx++}`, `sp${idx++}`, 'js-3.0.0', '', new SharedState(), {
plugins: [ScreenTrackingPlugin({ screenContext: false })],
eventStore,
customFetch,
contexts: { webPage: false },
});
});
it('does not add screen context entity to events', async () => {
trackScreenView({ name: 'Home' });
let [{ co }] = await eventStore.getAllPayloads();
expect(co).toBeUndefined();
tracker?.trackPageView();
[, { co }] = await eventStore.getAllPayloads();
expect(co).toBeUndefined();
});
});
});