|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 2 | +import { |
| 3 | + MessageQueue, |
| 4 | + createConsentManager, |
| 5 | + deleteCookie, |
| 6 | +} from '@imtbl/audience-core'; |
| 7 | +import { ImmutableAudienceSDK } from './sdk'; |
| 8 | + |
| 9 | +// Mock core modules |
| 10 | +jest.mock('@imtbl/audience-core', () => { |
| 11 | + const actual = jest.requireActual('@imtbl/audience-core'); |
| 12 | + |
| 13 | + const mockQueue = { |
| 14 | + start: jest.fn(), |
| 15 | + stop: jest.fn(), |
| 16 | + destroy: jest.fn(), |
| 17 | + enqueue: jest.fn(), |
| 18 | + flush: jest.fn(), |
| 19 | + flushUnload: jest.fn(), |
| 20 | + purge: jest.fn(), |
| 21 | + transform: jest.fn(), |
| 22 | + clear: jest.fn(), |
| 23 | + get length() { return 0; }, |
| 24 | + }; |
| 25 | + |
| 26 | + let consentLevel = 'none'; |
| 27 | + const mockConsent = { |
| 28 | + get level() { return consentLevel as 'none' | 'anonymous' | 'full'; }, |
| 29 | + setLevel: jest.fn((next: string) => { consentLevel = next; }), |
| 30 | + }; |
| 31 | + |
| 32 | + return { |
| 33 | + ...actual, |
| 34 | + MessageQueue: jest.fn(() => mockQueue), |
| 35 | + createConsentManager: jest.fn(() => { |
| 36 | + consentLevel = 'none'; |
| 37 | + return mockConsent; |
| 38 | + }), |
| 39 | + getOrCreateAnonymousId: jest.fn(() => 'anon-123'), |
| 40 | + getOrCreateSession: jest.fn(() => ({ sessionId: 'sess-456', isNew: false })), |
| 41 | + collectAttribution: jest.fn(() => ({})), |
| 42 | + deleteCookie: jest.fn(), |
| 43 | + httpTransport: { send: jest.fn() }, |
| 44 | + generateId: actual.generateId, |
| 45 | + getTimestamp: actual.getTimestamp, |
| 46 | + getBaseUrl: actual.getBaseUrl, |
| 47 | + }; |
| 48 | +}); |
| 49 | + |
| 50 | +beforeEach(() => { |
| 51 | + jest.clearAllMocks(); |
| 52 | +}); |
| 53 | + |
| 54 | +function createSDK(overrides: Record<string, any> = {}) { |
| 55 | + return new ImmutableAudienceSDK({ |
| 56 | + publishableKey: 'pk_test', |
| 57 | + environment: 'sandbox', |
| 58 | + ...overrides, |
| 59 | + }); |
| 60 | +} |
| 61 | + |
| 62 | +describe('ImmutableAudienceSDK', () => { |
| 63 | + it('initialises with default consent none', () => { |
| 64 | + createSDK(); |
| 65 | + expect(createConsentManager).toHaveBeenCalledWith( |
| 66 | + expect.anything(), |
| 67 | + 'pk_test', |
| 68 | + 'anon-123', |
| 69 | + 'sandbox', |
| 70 | + 'none', |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + it('initialises with provided consent level', () => { |
| 75 | + createSDK({ consent: 'full' }); |
| 76 | + expect(createConsentManager).toHaveBeenCalledWith( |
| 77 | + expect.anything(), |
| 78 | + 'pk_test', |
| 79 | + 'anon-123', |
| 80 | + 'sandbox', |
| 81 | + 'full', |
| 82 | + ); |
| 83 | + }); |
| 84 | + |
| 85 | + it('starts the queue on construction', () => { |
| 86 | + createSDK(); |
| 87 | + const queue = (MessageQueue as jest.Mock).mock.results[0].value; |
| 88 | + expect(queue.start).toHaveBeenCalled(); |
| 89 | + }); |
| 90 | + |
| 91 | + it('does not enqueue when consent is none', () => { |
| 92 | + const sdk = createSDK(); |
| 93 | + const queue = (MessageQueue as jest.Mock).mock.results[0].value; |
| 94 | + |
| 95 | + sdk.page(); |
| 96 | + sdk.track('click'); |
| 97 | + |
| 98 | + expect(queue.enqueue).not.toHaveBeenCalled(); |
| 99 | + }); |
| 100 | + |
| 101 | + it('enqueues page event when consent allows', () => { |
| 102 | + const sdk = createSDK({ consent: 'anonymous' }); |
| 103 | + const consent = (createConsentManager as jest.Mock).mock.results[0].value; |
| 104 | + consent.setLevel('anonymous'); |
| 105 | + const queue = (MessageQueue as jest.Mock).mock.results[0].value; |
| 106 | + |
| 107 | + sdk.page({ section: 'shop' }); |
| 108 | + |
| 109 | + expect(queue.enqueue).toHaveBeenCalledWith( |
| 110 | + expect.objectContaining({ |
| 111 | + type: 'page', |
| 112 | + surface: 'web', |
| 113 | + anonymousId: 'anon-123', |
| 114 | + }), |
| 115 | + ); |
| 116 | + }); |
| 117 | + |
| 118 | + it('enqueues track event with eventName', () => { |
| 119 | + const sdk = createSDK({ consent: 'anonymous' }); |
| 120 | + const consent = (createConsentManager as jest.Mock).mock.results[0].value; |
| 121 | + consent.setLevel('anonymous'); |
| 122 | + const queue = (MessageQueue as jest.Mock).mock.results[0].value; |
| 123 | + |
| 124 | + sdk.track('purchase', { value: 9.99 }); |
| 125 | + |
| 126 | + expect(queue.enqueue).toHaveBeenCalledWith( |
| 127 | + expect.objectContaining({ |
| 128 | + type: 'track', |
| 129 | + eventName: 'purchase', |
| 130 | + }), |
| 131 | + ); |
| 132 | + }); |
| 133 | + |
| 134 | + it('only allows identify at full consent', () => { |
| 135 | + const sdk = createSDK(); |
| 136 | + const consent = (createConsentManager as jest.Mock).mock.results[0].value; |
| 137 | + const queue = (MessageQueue as jest.Mock).mock.results[0].value; |
| 138 | + |
| 139 | + consent.setLevel('anonymous'); |
| 140 | + sdk.identify('user-1', { name: 'Test' }); |
| 141 | + expect(queue.enqueue).not.toHaveBeenCalled(); |
| 142 | + |
| 143 | + consent.setLevel('full'); |
| 144 | + sdk.identify('user-1', { name: 'Test' }); |
| 145 | + expect(queue.enqueue).toHaveBeenCalledWith( |
| 146 | + expect.objectContaining({ |
| 147 | + type: 'identify', |
| 148 | + userId: 'user-1', |
| 149 | + }), |
| 150 | + ); |
| 151 | + }); |
| 152 | + |
| 153 | + it('clears cookies when consent set to none', () => { |
| 154 | + const sdk = createSDK(); |
| 155 | + const consent = (createConsentManager as jest.Mock).mock.results[0].value; |
| 156 | + consent.setLevel('full'); |
| 157 | + |
| 158 | + sdk.setConsent('none'); |
| 159 | + |
| 160 | + expect(consent.setLevel).toHaveBeenCalledWith('none'); |
| 161 | + expect(deleteCookie).toHaveBeenCalledTimes(2); |
| 162 | + }); |
| 163 | + |
| 164 | + it('destroys queue on destroy', () => { |
| 165 | + const sdk = createSDK(); |
| 166 | + const queue = (MessageQueue as jest.Mock).mock.results[0].value; |
| 167 | + |
| 168 | + sdk.destroy(); |
| 169 | + expect(queue.destroy).toHaveBeenCalled(); |
| 170 | + |
| 171 | + sdk.track('late-event'); |
| 172 | + expect(queue.enqueue).not.toHaveBeenCalled(); |
| 173 | + }); |
| 174 | +}); |
0 commit comments