import { InQueueManager } from '../../src/in_queue'; import { enableActivityTracking, setVisitorCookieTimeout, trackPageView, updatePageActivity, setUserId, } from '@mailbiz/browser-tracker'; import { BrowserTracker, addTracker, isFunction, getTrackers } from '@mailbiz/browser-tracker-core'; jest.mock('@mailbiz/browser-tracker'); jest.mock('@mailbiz/browser-tracker-core'); const mockNewTracker = addTracker as jest.Mock; const mockGetTrackers = getTrackers as jest.Mock>; const mockEnableActivityTracking = enableActivityTracking as jest.Mock; const mockSetVisitorCookieTimeout = setVisitorCookieTimeout as jest.Mock; const mockTrackPageView = trackPageView as jest.Mock; const mockUpdatePageActivity = updatePageActivity as jest.Mock; const mockSetUserId = setUserId as jest.Mock; const mockIsFunction = isFunction as jest.Mock; describe('InQueueManager', () => { let output = 0; let userId: string | null | undefined; const newTracker = (trackerId: string): any => { let attribute = 10; return { id: trackerId, enableActivityTracking: function ({ n }: { n: number }) { attribute += n; }, setVisitorCookieTimeout: function ({ p }: { p: number }) { attribute = p; }, setUserId: function (s?: string | null) { userId = s; }, trackPageView: function () { output = attribute; }, updatePageActivity: function () { output += attribute; }, }; }; const mockTrackers: Record = {}; mockNewTracker.mockImplementation((name: string) => { mockTrackers[name] = newTracker(name); return mockTrackers[name]; }); mockGetTrackers.mockImplementation((_: Array) => { return Object.values(mockTrackers); }); mockEnableActivityTracking.mockImplementation(function (event: { n: number }, trackers: string[]) { trackers.forEach((t) => { mockTrackers[t].enableActivityTracking(event); }); }); mockSetVisitorCookieTimeout.mockImplementation(function (event: { p: number }, trackers: string[]) { trackers.forEach((t) => { mockTrackers[t].setVisitorCookieTimeout(event); }); }); mockTrackPageView.mockImplementation(function (trackers: string[]) { trackers.forEach((t) => { mockTrackers[t].trackPageView(); }); }); mockUpdatePageActivity.mockImplementation(function (trackers: string[]) { trackers.forEach((t) => { mockTrackers[t].updatePageActivity(); }); }); mockSetUserId.mockImplementation(function (userId: string | null | undefined, trackers: string[]) { trackers.forEach((t) => { mockTrackers[t].setUserId(userId); }); }); mockIsFunction.mockImplementation(function (func: any) { if (func && typeof func === 'function') { return true; } return false; }); const asyncQueueOps = [ ['newTracker', 'firstTracker', 'firstEndpoint'], ['enableActivityTracking', { n: 5 }], ['trackPageView'], ]; const asyncQueue = InQueueManager('mbone', asyncQueueOps); it('Make a proxy, Function originally stored in asyncQueue is executed when asyncQueue becomes an AsyncQueueProxy', () => { expect(output).toEqual(15); }); it('Add to asyncQueue after conversion, Function added to asyncQueue after it becomes an AsyncQueueProxy is executed', () => { asyncQueue.push(['setVisitorCookieTimeout', { p: 7 }]); asyncQueue.push(['trackPageView']); expect(output).toEqual(7); }); it('Set UserId to String, null and undefined', () => { asyncQueue.push(['setUserId', 'snow123']); expect(userId).toEqual('snow123'); asyncQueue.push(['setUserId', null]); expect(userId).toEqual(null); asyncQueue.push(['setUserId', undefined]); expect(userId).toEqual(undefined); }); it("Backward compatibility: Create a tracker using the legacy setCollectorUrl method, A second tracker is created and both trackers' attributes are added to output", () => { asyncQueue.push(['newTracker', 'secondTracker', 'secondEndpoint']); asyncQueue.push(['updatePageActivity']); expect(output).toEqual(24); }); it("Use 'function:tracker1;tracker2' syntax to control which trackers execute which functions, Set the attributes of the two trackers individually, then add both to output", () => { asyncQueue.push(['setVisitorCookieTimeout:firstTracker', { p: 2 }]); asyncQueue.push(['setVisitorCookieTimeout:secondTracker', { p: 3 }]); asyncQueue.push(['updatePageActivity:firstTracker;secondTracker']); expect(output).toEqual(29); }); });