diff options
Diffstat (limited to 'spec/frontend/tracking/dispatch_snowplow_event_spec.js')
-rw-r--r-- | spec/frontend/tracking/dispatch_snowplow_event_spec.js | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/spec/frontend/tracking/dispatch_snowplow_event_spec.js b/spec/frontend/tracking/dispatch_snowplow_event_spec.js new file mode 100644 index 00000000000..5f4d065d504 --- /dev/null +++ b/spec/frontend/tracking/dispatch_snowplow_event_spec.js @@ -0,0 +1,76 @@ +import * as Sentry from '@sentry/browser'; + +import { dispatchSnowplowEvent } from '~/tracking/dispatch_snowplow_event'; +import getStandardContext from '~/tracking/get_standard_context'; +import { extraContext, servicePingContext } from './mock_data'; + +jest.mock('@sentry/browser'); +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); + }); +}); |