|
| 1 | +import { afterEach, beforeEach, describe, expect, jest, test } from '@jest/globals'; |
| 2 | + |
| 3 | +import * as mocks from '../mocks'; |
| 4 | +import { sdkWindow, EUID, __euidInternalHandleScriptLoad } from '../euidSdk'; |
| 5 | +import { EventType, Uid2CallbackHandler } from '../uid2CallbackManager'; |
| 6 | + |
| 7 | +let callback: any; |
| 8 | +let asyncCallback: jest.Mock<Uid2CallbackHandler>; |
| 9 | +let euid: EUID; |
| 10 | +let xhrMock: any; |
| 11 | + |
| 12 | +const debugOutput = false; |
| 13 | + |
| 14 | +mocks.setupFakeTime(); |
| 15 | + |
| 16 | +beforeEach(() => { |
| 17 | + jest.clearAllMocks(); |
| 18 | + mocks.resetFakeTime(); |
| 19 | + jest.runOnlyPendingTimers(); |
| 20 | + |
| 21 | + callback = jest.fn(); |
| 22 | + xhrMock = new mocks.XhrMock(sdkWindow); |
| 23 | + mocks.setCookieMock(sdkWindow.document); |
| 24 | + asyncCallback = jest.fn((event, payload) => { |
| 25 | + if (debugOutput) { |
| 26 | + console.log('Async Callback Event:', event); |
| 27 | + console.log('Payload:', payload); |
| 28 | + } |
| 29 | + }); |
| 30 | +}); |
| 31 | + |
| 32 | +afterEach(() => { |
| 33 | + mocks.resetFakeTime(); |
| 34 | +}); |
| 35 | + |
| 36 | +const makeIdentity = mocks.makeIdentityV2; |
| 37 | + |
| 38 | +describe('when a callback is provided', () => { |
| 39 | + const refreshFrom = Date.now() + 100; |
| 40 | + const identity = { ...makeIdentity(), refresh_from: refreshFrom }; |
| 41 | + const refreshedIdentity = { |
| 42 | + ...makeIdentity(), |
| 43 | + advertising_token: 'refreshed_token', |
| 44 | + }; |
| 45 | + describe('before constructor is called', () => { |
| 46 | + test('it should be called during the construction process', () => { |
| 47 | + sdkWindow.__euid = { callbacks: [asyncCallback] }; |
| 48 | + const calls = asyncCallback.mock.calls.length; |
| 49 | + __euidInternalHandleScriptLoad(); |
| 50 | + expect(asyncCallback).toBeCalledTimes(calls + 1); |
| 51 | + expect(asyncCallback).toBeCalledWith(EventType.SdkLoaded, expect.anything()); |
| 52 | + }); |
| 53 | + test('it should not be called by the constructor itself', () => { |
| 54 | + sdkWindow.__euid = { callbacks: [asyncCallback] }; |
| 55 | + const calls = asyncCallback.mock.calls.length; |
| 56 | + new EUID([asyncCallback]); |
| 57 | + expect(asyncCallback).toBeCalledTimes(calls); |
| 58 | + }); |
| 59 | + }); |
| 60 | + describe('before construction but the window global has already been assigned', () => { |
| 61 | + // N.B. this is an artificial situation to check an edge case. |
| 62 | + test('it should be called during construction', () => { |
| 63 | + sdkWindow.__euid = new EUID(); |
| 64 | + const calls = asyncCallback.mock.calls.length; |
| 65 | + new EUID([asyncCallback]); |
| 66 | + expect(asyncCallback).toBeCalledTimes(calls + 1); |
| 67 | + }); |
| 68 | + }); |
| 69 | + describe('after construction', () => { |
| 70 | + test('the SDKLoaded event is sent immediately', () => { |
| 71 | + sdkWindow.__euid = new EUID(); |
| 72 | + const calls = asyncCallback.mock.calls.length; |
| 73 | + sdkWindow.__euid.callbacks!.push(asyncCallback); |
| 74 | + expect(asyncCallback).toBeCalledTimes(calls + 1); |
| 75 | + expect(asyncCallback).toBeCalledWith(EventType.SdkLoaded, expect.anything()); |
| 76 | + }); |
| 77 | + }); |
| 78 | +}); |
0 commit comments