forked from adamlaska/circleci-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalytics.test.js
More file actions
100 lines (82 loc) · 2.68 KB
/
analytics.test.js
File metadata and controls
100 lines (82 loc) · 2.68 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
import AnalyticsClient from './analytics.js';
import { default as CookieOrginal } from 'js-cookie';
import glob from '../../../jest/global';
jest.mock('js-cookie');
const Cookie = CookieOrginal;
const options = {
integrations: {
Amplitude: { session_id: expect.any(Number) },
},
};
describe('Analytics Service', () => {
beforeEach(() => {
jest.resetAllMocks();
// These globs are defined as jest.fn() instances in jest/globs.ts
glob.analytics.track.mockReset();
glob.analytics.identify.mockReset();
glob.analytics.page.mockReset();
});
test('getSessionId() returns cookie value', () => {
const spy = jest.spyOn(Cookie, 'get').mockImplementation(() => 123);
expect(AnalyticsClient.getSessionId()).toBe(123);
expect(spy).toHaveBeenCalledWith('amplitude-session-id');
});
describe('trackAction()', () => {
it('should call analytics.track with arguments', () => {
const properties = {
some: 'value',
};
AnalyticsClient.trackAction('event', properties);
expect(glob.analytics.track).toHaveBeenCalledWith(
'event',
properties,
options,
);
});
it('should not call analytics.track if Datadog Synthetics Bot detected', () => {
glob._DATADOG_SYNTHETICS_BROWSER = true;
AnalyticsClient.trackAction('event');
expect(glob.analytics.track).not.toHaveBeenCalled();
delete glob._DATADOG_SYNTHETICS_BROWSER;
});
});
describe('trackUser()', () => {
it('should call analytics.identify with arguments', () => {
const traits = {
some: 'value',
};
AnalyticsClient.trackUser('event', traits);
expect(glob.analytics.identify).toHaveBeenCalledWith(
'event',
traits,
options,
);
});
it('should not call analytics.identify if Datadog Synthetics Bot detected', () => {
glob._DATADOG_SYNTHETICS_BROWSER = true;
AnalyticsClient.trackUser('event');
expect(glob.analytics.identify).not.toHaveBeenCalled();
delete glob._DATADOG_SYNTHETICS_BROWSER;
});
});
describe('trackPage()', () => {
it('should call analytics.page with arguments', () => {
const properties = {
some: 'value',
};
AnalyticsClient.trackPage('event', properties);
expect(glob.analytics.page).toHaveBeenCalledWith(
'docs',
'event',
properties,
options,
);
});
it('should not call analytics.page if Datadog Synthetics Bot detected', () => {
glob._DATADOG_SYNTHETICS_BROWSER = true;
AnalyticsClient.trackPage('event');
expect(glob.analytics.page).not.toHaveBeenCalled();
delete glob._DATADOG_SYNTHETICS_BROWSER;
});
});
});