diff options
Diffstat (limited to 'spec/frontend/tracking')
-rw-r--r-- | spec/frontend/tracking/tracking_spec.js | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/spec/frontend/tracking/tracking_spec.js b/spec/frontend/tracking/tracking_spec.js index d85299cdfc3..665bf44fc77 100644 --- a/spec/frontend/tracking/tracking_spec.js +++ b/spec/frontend/tracking/tracking_spec.js @@ -129,6 +129,72 @@ describe('Tracking', () => { }); }); + describe('.definition', () => { + const TEST_VALID_BASENAME = '202108302307_default_click_button'; + const TEST_EVENT_DATA = { category: undefined, action: 'click_button' }; + let eventSpy; + let dispatcherSpy; + + beforeAll(() => { + Tracking.definitionsManifest = { + '202108302307_default_click_button': 'config/events/202108302307_default_click_button.yml', + }; + }); + + beforeEach(() => { + eventSpy = jest.spyOn(Tracking, 'event'); + dispatcherSpy = jest.spyOn(Tracking, 'dispatchFromDefinition'); + }); + + it('throws an error if the definition does not exists', () => { + const basename = '20220230_default_missing_definition'; + const expectedError = new Error(`Missing Snowplow event definition "${basename}"`); + + expect(() => Tracking.definition(basename)).toThrow(expectedError); + }); + + it('dispatches an event from a definition present in the manifest', () => { + Tracking.definition(TEST_VALID_BASENAME); + + expect(dispatcherSpy).toHaveBeenCalledWith(TEST_VALID_BASENAME, {}); + }); + + it('push events to the queue if not loaded', () => { + Tracking.definitionsLoaded = false; + Tracking.definitionsEventsQueue = []; + + const dispatched = Tracking.definition(TEST_VALID_BASENAME); + + expect(dispatched).toBe(false); + expect(Tracking.definitionsEventsQueue[0]).toStrictEqual([TEST_VALID_BASENAME, {}]); + expect(eventSpy).not.toHaveBeenCalled(); + }); + + it('dispatch events when the definition is loaded', () => { + const definition = { key: TEST_VALID_BASENAME, ...TEST_EVENT_DATA }; + Tracking.definitions = [{ ...definition }]; + Tracking.definitionsEventsQueue = []; + Tracking.definitionsLoaded = true; + + const dispatched = Tracking.definition(TEST_VALID_BASENAME); + + expect(dispatched).not.toBe(false); + expect(Tracking.definitionsEventsQueue).toEqual([]); + expect(eventSpy).toHaveBeenCalledWith(definition.category, definition.action, {}); + }); + + it('lets defined event data takes precedence', () => { + const definition = { key: TEST_VALID_BASENAME, category: undefined, action: 'click_button' }; + const eventData = { category: TEST_CATEGORY }; + Tracking.definitions = [{ ...definition }]; + Tracking.definitionsLoaded = true; + + Tracking.definition(TEST_VALID_BASENAME, eventData); + + expect(eventSpy).toHaveBeenCalledWith(TEST_CATEGORY, definition.action, eventData); + }); + }); + describe('.enableFormTracking', () => { it('tells snowplow to enable form tracking, with only explicit contexts', () => { const config = { forms: { allow: ['form-class1'] }, fields: { allow: ['input-class1'] } }; |