forked from snowplow/snowplow-javascript-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_lifecycle.test.ts
More file actions
97 lines (81 loc) · 2.82 KB
/
app_lifecycle.test.ts
File metadata and controls
97 lines (81 loc) · 2.82 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 { AppState } from 'react-native';
import { BACKGROUND_EVENT_SCHEMA, LIFECYCLE_CONTEXT_SCHEMA } from '../../src/constants';
import { buildPageView, Payload, trackerCore } from '@snowplow/tracker-core';
import { newAppLifecyclePlugin } from '../../src/plugins/app_lifecycle';
describe('Application lifecycle plugin', () => {
afterEach(() => {
jest.clearAllMocks();
});
it('tracks events on app state changes', async () => {
const appStateSpy = jest.spyOn(AppState, 'addEventListener');
const payloads: Payload[] = [];
const tracker = trackerCore({
callback: (pb) => payloads.push(pb.build()),
base64: false,
});
const appLifecyclePlugin = await newAppLifecyclePlugin({}, tracker);
tracker.addPlugin(appLifecyclePlugin);
appStateSpy.mock.calls?.[0]?.[1]('background');
expect(payloads.length).toBe(1);
expect(payloads[0]?.ue_pr ?? '').toContain(BACKGROUND_EVENT_SCHEMA);
});
it('attaches lifecycle context to events with the correct properties', async () => {
const appStateSpy = jest.spyOn(AppState, 'addEventListener');
const payloads: Payload[] = [];
const tracker = trackerCore({
callback: (pb) => payloads.push(pb.build()),
base64: false,
});
const appLifecyclePlugin = await newAppLifecyclePlugin({}, tracker);
tracker.addPlugin(appLifecyclePlugin);
tracker.track(buildPageView({ pageUrl: 'http://localhost' }));
expect(payloads.length).toBe(1);
expect(JSON.parse(payloads[0]?.co as string).data).toEqual([
{
schema: LIFECYCLE_CONTEXT_SCHEMA,
data: {
isVisible: true,
index: 1,
},
},
]);
payloads.length = 0;
appStateSpy.mock.calls?.[0]?.[1]('background');
expect(payloads.length).toBe(1);
expect(JSON.parse(payloads[0]?.co as string).data).toEqual([
{
schema: LIFECYCLE_CONTEXT_SCHEMA,
data: {
isVisible: false,
index: 1,
},
},
]);
payloads.length = 0;
appStateSpy.mock.calls?.[0]?.[1]('active');
expect(payloads.length).toBe(1);
expect(JSON.parse(payloads[0]?.co as string).data).toEqual([
{
schema: LIFECYCLE_CONTEXT_SCHEMA,
data: {
isVisible: true,
index: 2,
},
},
]);
});
it('removes subscription on tracker deactivation', async () => {
const appStateSpy = jest.spyOn(AppState, 'addEventListener');
const removeSpy = jest.fn();
appStateSpy.mockReturnValue({ remove: removeSpy });
const tracker = trackerCore({
callback: () => {},
base64: false,
});
const appLifecyclePlugin = await newAppLifecyclePlugin({}, tracker);
tracker.addPlugin(appLifecyclePlugin);
expect(appStateSpy).toHaveBeenCalledTimes(1);
tracker.deactivate();
expect(removeSpy).toHaveBeenCalledTimes(1);
});
});