This repository was archived by the owner on Jan 15, 2026. It is now read-only.
forked from snowplow/snowplow-javascript-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplatform_context_android.test.ts
More file actions
81 lines (73 loc) · 2.57 KB
/
Copy pathplatform_context_android.test.ts
File metadata and controls
81 lines (73 loc) · 2.57 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
import { buildPageView, Payload, trackerCore } from '@snowplow/tracker-core';
import { newPlatformContextPlugin } from '../../src/plugins/platform_context';
import { MOBILE_CONTEXT_SCHEMA } from '../../src/constants';
import { NativeModules } from 'react-native';
import { Dimensions } from 'react-native';
describe('PlatformContextPlugin on Android', () => {
beforeAll(() => {
jest.mock('react-native/Libraries/Utilities/Platform', () => ({
OS: 'android',
Version: 33,
constants: {
Brand: 'google',
Manufacturer: 'Google',
Model: 'sdk_gphone64_arm64',
Release: '13',
Version: 33,
},
select: () => null,
}));
jest.spyOn(Dimensions, 'get').mockReturnValue({
width: 123.4567,
height: 89.1234,
scale: 0,
fontScale: 0,
});
NativeModules.I18nManager = {
localeIdentifier: 'en-GB',
};
});
afterAll(() => {
jest.clearAllMocks();
});
it('adds platform context to events', async () => {
const sessionPlugin = await newPlatformContextPlugin();
const payloads: Payload[] = [];
const tracker = trackerCore({
corePlugins: [sessionPlugin.plugin],
callback: (pb) => payloads.push(pb.build()),
base64: false,
});
tracker.track(buildPageView({ pageUrl: 'http://localhost' }));
expect(payloads.length).toBe(1);
const [payload] = payloads;
expect(payload?.co).toBeDefined();
expect(payload?.co).toContain(MOBILE_CONTEXT_SCHEMA);
expect(payload?.co).toContain('"13"');
expect(payload?.co).toContain('"sdk_gphone64_arm64"');
expect(payload?.co).toContain('"Google"');
expect(payload?.co).toContain('"Google"');
expect(payload?.co).toContain('"en-GB"');
expect(payload?.co).toContain('"123x89"');
});
it('truncates language to 8 characters', async () => {
const sessionPlugin = await newPlatformContextPlugin({
platformContextRetriever: {
getLanguage: () => Promise.resolve('1234567890'),
},
});
const payloads: Payload[] = [];
const tracker = trackerCore({
corePlugins: [sessionPlugin.plugin],
callback: (pb) => payloads.push(pb.build()),
base64: false,
});
tracker.track(buildPageView({ pageUrl: 'http://localhost' }));
expect(payloads.length).toBe(1);
const [payload] = payloads;
expect(payload?.co).toBeDefined();
const entities = JSON.parse(payload?.co as string).data;
const mobileContext = entities.find((entity: any) => entity.schema === MOBILE_CONTEXT_SCHEMA);
expect(mobileContext?.data.language).toBe('12345678');
});
});