|
| 1 | +/* eslint-disable @typescript-eslint/no-require-imports, global-require */ |
| 2 | + |
| 3 | +// We need to control when bootstrap.ts runs, so we use dynamic require |
| 4 | +// after setting up the window stub and mocks. |
| 5 | + |
| 6 | +const mockInit = jest.fn(); |
| 7 | +const mockPage = jest.fn(); |
| 8 | +const mockIdentify = jest.fn(); |
| 9 | +const mockSetConsent = jest.fn(); |
| 10 | +const mockDestroy = jest.fn(); |
| 11 | + |
| 12 | +jest.mock('./pixel', () => ({ |
| 13 | + Pixel: jest.fn().mockImplementation(() => ({ |
| 14 | + init: mockInit, |
| 15 | + page: mockPage, |
| 16 | + identify: mockIdentify, |
| 17 | + setConsent: mockSetConsent, |
| 18 | + destroy: mockDestroy, |
| 19 | + })), |
| 20 | +})); |
| 21 | + |
| 22 | +jest.mock('@imtbl/audience-core', () => ({ |
| 23 | + MessageQueue: jest.fn(), |
| 24 | + httpTransport: {}, |
| 25 | + getBaseUrl: jest.fn(), |
| 26 | + INGEST_PATH: '', |
| 27 | + FLUSH_INTERVAL_MS: 5000, |
| 28 | + FLUSH_SIZE: 20, |
| 29 | + getOrCreateAnonymousId: jest.fn(), |
| 30 | + collectContext: jest.fn(), |
| 31 | + generateId: jest.fn(), |
| 32 | + getTimestamp: jest.fn(), |
| 33 | + isBrowser: jest.fn().mockReturnValue(true), |
| 34 | + getCookie: jest.fn(), |
| 35 | + setCookie: jest.fn(), |
| 36 | + collectAttribution: jest.fn().mockReturnValue({}), |
| 37 | + getOrCreateSession: jest.fn().mockReturnValue({ sessionId: 's', isNew: false }), |
| 38 | + createConsentManager: jest.fn().mockReturnValue({ level: 'none', setLevel: jest.fn() }), |
| 39 | +})); |
| 40 | + |
| 41 | +beforeEach(() => { |
| 42 | + jest.clearAllMocks(); |
| 43 | + delete (window as Record<string, unknown>).__imtbl; |
| 44 | + // Re-isolate module so the side-effect runs fresh |
| 45 | + jest.resetModules(); |
| 46 | +}); |
| 47 | + |
| 48 | +describe('bootstrap', () => { |
| 49 | + it('replays queued init command from snippet stub', () => { |
| 50 | + // Simulate snippet having queued an init command |
| 51 | + (window as Record<string, unknown>).__imtbl = [ |
| 52 | + ['init', { key: 'pk_test', environment: 'dev', consent: 'anonymous' }], |
| 53 | + ]; |
| 54 | + |
| 55 | + require('./bootstrap'); |
| 56 | + |
| 57 | + expect(mockInit).toHaveBeenCalledWith({ |
| 58 | + key: 'pk_test', |
| 59 | + environment: 'dev', |
| 60 | + consent: 'anonymous', |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + it('replays multiple queued commands in order', () => { |
| 65 | + (window as Record<string, unknown>).__imtbl = [ |
| 66 | + ['init', { key: 'pk_test' }], |
| 67 | + ['consent', 'full'], |
| 68 | + ['identify', 'user-1', { email: 'a@b.com' }], |
| 69 | + ]; |
| 70 | + |
| 71 | + require('./bootstrap'); |
| 72 | + |
| 73 | + expect(mockInit).toHaveBeenCalledWith({ key: 'pk_test' }); |
| 74 | + expect(mockSetConsent).toHaveBeenCalledWith('full'); |
| 75 | + expect(mockIdentify).toHaveBeenCalledWith('user-1', { email: 'a@b.com' }); |
| 76 | + }); |
| 77 | + |
| 78 | + it('installs loader and handles new commands after load', () => { |
| 79 | + require('./bootstrap'); |
| 80 | + |
| 81 | + const loader = (window as Record<string, unknown>).__imtbl as { |
| 82 | + push: (...args: unknown[]) => void; |
| 83 | + _loaded: boolean; |
| 84 | + }; |
| 85 | + |
| 86 | + expect(loader._loaded).toBe(true); |
| 87 | + |
| 88 | + loader.push(['page', { custom: 'prop' }]); |
| 89 | + expect(mockPage).toHaveBeenCalledWith({ custom: 'prop' }); |
| 90 | + |
| 91 | + loader.push(['consent', 'anonymous']); |
| 92 | + expect(mockSetConsent).toHaveBeenCalledWith('anonymous'); |
| 93 | + }); |
| 94 | + |
| 95 | + it('ignores unknown commands', () => { |
| 96 | + (window as Record<string, unknown>).__imtbl = [ |
| 97 | + ['nonexistent', 'arg1'], |
| 98 | + ]; |
| 99 | + |
| 100 | + // Should not throw |
| 101 | + expect(() => require('./bootstrap')).not.toThrow(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('works when no stub exists on window', () => { |
| 105 | + expect(() => require('./bootstrap')).not.toThrow(); |
| 106 | + |
| 107 | + const loader = (window as Record<string, unknown>).__imtbl as { |
| 108 | + _loaded: boolean; |
| 109 | + }; |
| 110 | + expect(loader._loaded).toBe(true); |
| 111 | + }); |
| 112 | +}); |
0 commit comments