forked from snowplow/snowplow-javascript-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.test.ts
More file actions
270 lines (225 loc) · 9.48 KB
/
session.test.ts
File metadata and controls
270 lines (225 loc) · 9.48 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import { buildPageView, buildSelfDescribingEvent, Payload, trackerCore } from '@snowplow/tracker-core';
import { newSessionPlugin } from '../../src/plugins/session';
import AsyncStorage from '@react-native-async-storage/async-storage';
function createAsyncStorageMock() {
const storageState: Record<string, string> = {};
return {
getItem: (key: string) => Promise.resolve(storageState[key] ?? null),
setItem: (key: string, value: string) => {
storageState[key] = value;
return Promise.resolve();
},
};
}
describe('Session plugin', () => {
beforeAll(() => {
jest.useFakeTimers();
});
beforeEach(async () => {
await AsyncStorage.clear();
});
afterAll(() => {
jest.clearAllTimers();
});
it('starts a new session when necessary', async () => {
jest.setSystemTime(new Date('2022-04-17T00:00:00.000Z'));
const sessionPlugin = await newSessionPlugin({
asyncStorage: AsyncStorage,
namespace: 'test',
foregroundSessionTimeout: 5,
backgroundSessionTimeout: 5,
});
const sessionState = await sessionPlugin.getSessionState();
expect(sessionState.sessionIndex).toBe(1);
jest.setSystemTime(new Date('2022-04-17T00:00:10.000Z'));
const tracker = trackerCore({ corePlugins: [sessionPlugin.plugin] });
tracker.track(buildPageView({ pageUrl: 'http://localhost' }));
const newSessionState = await sessionPlugin.getSessionState();
expect(newSessionState.sessionIndex).toBe(2);
expect(newSessionState.previousSessionId).toBe(sessionState.sessionId);
});
it('attaches session context to events with the correct properties', async () => {
jest.setSystemTime(new Date('2022-04-17T00:00:00.000Z'));
const sessionPlugin = await newSessionPlugin({
asyncStorage: AsyncStorage,
namespace: 'test',
foregroundSessionTimeout: 5,
backgroundSessionTimeout: 5,
});
const payloads: Payload[] = [];
const tracker = trackerCore({
corePlugins: [sessionPlugin.plugin],
callback: (pb) => payloads.push(pb.build()),
base64: false,
});
tracker.track(buildPageView({ pageUrl: 'http://localhost' }));
tracker.track(buildPageView({ pageUrl: 'http://localhost' }));
expect(payloads.length).toBe(2);
expect(JSON.parse(payloads[0]?.co as string).data).toEqual([
{
schema: 'iglu:com.snowplowanalytics.snowplow/client_session/jsonschema/1-0-2',
data: {
userId: await sessionPlugin.getSessionUserId(),
sessionId: await sessionPlugin.getSessionId(),
sessionIndex: 1,
storageMechanism: 'LOCAL_STORAGE',
eventIndex: 1,
firstEventId: payloads[0]?.eid,
firstEventTimestamp: '2022-04-17T00:00:00.000Z',
},
},
]);
expect(JSON.parse(payloads[1]?.co as string).data).toEqual([
{
schema: 'iglu:com.snowplowanalytics.snowplow/client_session/jsonschema/1-0-2',
data: {
userId: await sessionPlugin.getSessionUserId(),
sessionId: await sessionPlugin.getSessionId(),
sessionIndex: 1,
storageMechanism: 'LOCAL_STORAGE',
eventIndex: 2,
firstEventId: payloads[0]?.eid,
firstEventTimestamp: '2022-04-17T00:00:00.000Z',
},
},
]);
});
it('creates a new session when new tracker is created', async () => {
jest.setSystemTime(new Date('2022-04-17T00:00:00.000Z'));
const sessionPlugin = await newSessionPlugin({
asyncStorage: AsyncStorage,
namespace: 'test',
foregroundSessionTimeout: 5,
backgroundSessionTimeout: 5,
});
const tracker1 = trackerCore({ corePlugins: [sessionPlugin.plugin] });
tracker1.track(buildPageView({ pageUrl: 'http://localhost' }));
const sessionState = await sessionPlugin.getSessionState();
expect(sessionState.sessionIndex).toBe(1);
const sessionPlugin2 = await newSessionPlugin({
asyncStorage: AsyncStorage,
namespace: 'test',
foregroundSessionTimeout: 5,
backgroundSessionTimeout: 5,
});
const newSessionState = await sessionPlugin2.getSessionState();
expect(newSessionState.sessionIndex).toBe(2);
expect(newSessionState.previousSessionId).toBe(sessionState.sessionId);
});
it('uses a background timeout when in background', async () => {
jest.setSystemTime(new Date('2022-04-17T00:00:00.000Z'));
const sessionPlugin = await newSessionPlugin({
asyncStorage: AsyncStorage,
namespace: 'test',
foregroundSessionTimeout: 1000,
backgroundSessionTimeout: 5,
});
const tracker = trackerCore({ corePlugins: [sessionPlugin.plugin] });
tracker.track(
buildSelfDescribingEvent({
event: {
schema: 'iglu:com.snowplowanalytics.snowplow/application_background/jsonschema/1-0-0',
data: {},
},
})
);
let sessionState = await sessionPlugin.getSessionState();
expect(sessionState.sessionIndex).toBe(1);
const firstSessionId = sessionState.sessionId;
jest.setSystemTime(new Date('2022-04-17T00:00:05.000Z'));
tracker.track(buildPageView({ pageUrl: 'http://localhost' }));
sessionState = await sessionPlugin.getSessionState();
expect(sessionState.sessionIndex).toBe(1);
jest.setSystemTime(new Date('2022-04-17T00:00:15.000Z'));
tracker.track(buildPageView({ pageUrl: 'http://localhost' }));
sessionState = await sessionPlugin.getSessionState();
expect(sessionState.sessionIndex).toBe(2);
expect(sessionState.previousSessionId).toBe(firstSessionId);
});
it('uses a foreground timeout when in foreground', async () => {
jest.setSystemTime(new Date('2022-04-17T00:00:00.000Z'));
const sessionPlugin = await newSessionPlugin({
asyncStorage: AsyncStorage,
namespace: 'test',
foregroundSessionTimeout: 5,
backgroundSessionTimeout: 1,
});
const tracker = trackerCore({ corePlugins: [sessionPlugin.plugin] });
let sessionState = await sessionPlugin.getSessionState();
expect(sessionState.sessionIndex).toBe(1);
const firstSessionId = sessionState.sessionId;
jest.setSystemTime(new Date('2022-04-17T00:00:02.000Z'));
tracker.track(buildPageView({ pageUrl: 'http://localhost' }));
expect(sessionState.sessionIndex).toBe(1);
jest.setSystemTime(new Date('2022-04-17T00:00:10.000Z'));
tracker.track(buildPageView({ pageUrl: 'http://localhost' }));
sessionState = await sessionPlugin.getSessionState();
expect(sessionState.sessionIndex).toBe(2);
expect(sessionState.previousSessionId).toBe(firstSessionId);
});
it('has separate session state for different namespaces', async () => {
jest.setSystemTime(new Date('2022-04-17T00:00:00.000Z'));
const sessionPlugin1 = await newSessionPlugin({
asyncStorage: AsyncStorage,
namespace: 'test1',
foregroundSessionTimeout: 5,
backgroundSessionTimeout: 5,
});
const sessionPlugin2 = await newSessionPlugin({
asyncStorage: AsyncStorage,
namespace: 'test2',
foregroundSessionTimeout: 5,
backgroundSessionTimeout: 5,
});
const tracker1 = trackerCore({ corePlugins: [sessionPlugin1.plugin] });
const tracker2 = trackerCore({ corePlugins: [sessionPlugin2.plugin] });
tracker1.track(buildPageView({ pageUrl: 'http://localhost' }));
tracker2.track(buildPageView({ pageUrl: 'http://localhost' }));
const sessionState1 = await sessionPlugin1.getSessionState();
const sessionState2 = await sessionPlugin2.getSessionState();
expect(sessionState1.sessionIndex).toBe(1);
expect(sessionState2.sessionIndex).toBe(1);
});
it('retrieves the correct information from the session plugin', async () => {
jest.setSystemTime(new Date('2022-04-17T00:00:00.000Z'));
const sessionPlugin = await newSessionPlugin({
asyncStorage: AsyncStorage,
namespace: 'test',
foregroundSessionTimeout: 5,
backgroundSessionTimeout: 5,
});
const tracker = trackerCore({ corePlugins: [sessionPlugin.plugin] });
tracker.track(buildPageView({ pageUrl: 'http://localhost' }));
const userId = await sessionPlugin.getSessionUserId();
const sessionId = await sessionPlugin.getSessionId();
const sessionIndex = await sessionPlugin.getSessionIndex();
const sessionState = await sessionPlugin.getSessionState();
expect(userId).toBeDefined();
expect(sessionId).toBeDefined();
expect(sessionIndex).toBe(1);
expect(sessionState.userId).toEqual(userId);
expect(sessionState.sessionId).toEqual(sessionId);
expect(sessionState.sessionIndex).toEqual(sessionIndex);
});
it('retrieves the correct information from the session plugin with a custom async storage', async () => {
jest.setSystemTime(new Date('2022-04-17T00:00:00.000Z'));
const sessionPlugin = await newSessionPlugin({
asyncStorage: createAsyncStorageMock(),
namespace: 'test',
foregroundSessionTimeout: 5,
backgroundSessionTimeout: 5,
});
const tracker = trackerCore({ corePlugins: [sessionPlugin.plugin] });
tracker.track(buildPageView({ pageUrl: 'http://localhost' }));
const userId = await sessionPlugin.getSessionUserId();
const sessionId = await sessionPlugin.getSessionId();
const sessionIndex = await sessionPlugin.getSessionIndex();
const sessionState = await sessionPlugin.getSessionState();
expect(userId).toBeDefined();
expect(sessionId).toBeDefined();
expect(sessionIndex).toBe(1);
expect(sessionState.userId).toEqual(userId);
expect(sessionState.sessionId).toEqual(sessionId);
expect(sessionState.sessionIndex).toEqual(sessionIndex);
});
});