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
|
import * as Sentry from '~/sentry/sentry_browser_wrapper';
import { dispatchSnowplowEvent } from '~/tracking/dispatch_snowplow_event';
import getStandardContext from '~/tracking/get_standard_context';
import { extraContext, servicePingContext } from './mock_data';
jest.mock('~/sentry/sentry_browser_wrapper');
jest.mock('~/tracking/get_standard_context');
const category = 'Incident Management';
const action = 'view_incident_details';
describe('dispatchSnowplowEvent', () => {
const snowplowMock = jest.fn();
global.window.snowplow = snowplowMock;
const mockStandardContext = { some: 'context' };
getStandardContext.mockReturnValue(mockStandardContext);
beforeEach(() => {
snowplowMock.mockClear();
Sentry.captureException.mockClear();
});
it('calls snowplow trackStructEvent with correct arguments', () => {
const data = {
label: 'Show Incident',
property: 'click_event',
value: '12',
context: extraContext,
extra: { namespace: 'GitLab' },
};
dispatchSnowplowEvent(category, action, data);
expect(snowplowMock).toHaveBeenCalledWith('trackStructEvent', {
category,
action,
label: data.label,
property: data.property,
value: Number(data.value),
context: [mockStandardContext, data.context],
});
});
it('throws an error if no category is provided', () => {
expect(() => {
dispatchSnowplowEvent(undefined, 'some-action', {});
}).toThrow('Tracking: no category provided for tracking.');
});
it('handles an array of contexts', () => {
const data = {
context: [extraContext, servicePingContext],
extra: { namespace: 'GitLab' },
};
dispatchSnowplowEvent(category, action, data);
expect(snowplowMock).toHaveBeenCalledWith('trackStructEvent', {
category,
action,
context: [mockStandardContext, ...data.context],
});
});
it('handles Sentry error capturing', () => {
snowplowMock.mockImplementation(() => {
throw new Error('some error');
});
dispatchSnowplowEvent(category, action, {});
expect(Sentry.captureException).toHaveBeenCalledTimes(1);
});
});
|