-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtrackEvent.test.ts
93 lines (86 loc) · 2.67 KB
/
trackEvent.test.ts
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
import { describe, test, expect, beforeEach, vi } from 'vitest';
import { trackEvent } from './trackEvent';
import { getConfig, getIdentity, getStorage } from './storage/storage';
import { init as setConfig } from './storage/config';
import * as time from './utils/time';
import { timeStone } from './utils/time';
describe('trackEvent', () => {
Object.defineProperty(time, 'getNow', {
configurable: true,
value: vi.fn().mockImplementation(() => 1583872606122),
});
Object.assign(timeStone, { timeStart: 1583872606122 });
beforeEach(() => {
const identity = getIdentity();
Object.assign(identity, { isOptOut: false });
const config = setConfig({
platform: 'web',
projectName: 'testing',
apiEndpoint: 'https://client.analytics',
});
Object.assign(getStorage().config, config);
});
test('should resolve event with default values and enhancers', async () => {
const event = await trackEvent({
action: 'test',
component: 'testComponent',
name: 'testName',
});
expect(event).toEqual({
action: 'test',
component: 'testComponent',
name: 'testName',
auth: 'notLoggedIn',
locale: null,
platform: 'web',
project_name: 'testing',
session_lcc_id: null,
time_start: 1583872606122,
timestamp: 1583872606122,
});
});
test('should return null when disableEventApi is true', async () => {
const config = getConfig();
Object.assign(config, { disableEventApi: true });
const event = await trackEvent({
action: 'test',
component: 'testComponent',
name: 'testName',
});
expect(event).toBe(null);
});
test('should return null when identity.isOptOut is true', async () => {
const identity = getIdentity();
Object.assign(identity, { isOptOut: true });
const event = await trackEvent({
action: 'test',
component: 'testComponent',
name: 'testName',
});
expect(event).toBe(null);
});
test('should return null when platform is not web', async () => {
const config = setConfig({
platform: 'unknown',
projectName: 'testing',
apiEndpoint: 'https://client.analytics',
});
Object.assign(getStorage().config, config);
const event = await trackEvent({
action: 'test',
component: 'testComponent',
name: 'testName',
});
expect(event).toBe(null);
});
test('should return null when the config is disabled', async () => {
const config = getConfig();
Object.assign(config, { disabled: true });
const event = await trackEvent({
action: 'test',
component: 'testComponent',
name: 'testName',
});
expect(event).toBe(null);
});
});