Skip to content
53 changes: 53 additions & 0 deletions src/integrationTests/secureSignal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ describe('Secure Signal Tests', () => {

afterEach(() => {
mocks.resetFakeTime();
mocks.removeUid2LocalStorage();
});

test('should send signal to Google ESP when SDK initialized', async () => {
Expand All @@ -117,6 +118,57 @@ describe('Secure Signal Tests', () => {
identity.advertising_token
);
});

test('should send signal to Google ESP when SDK initialized and Identity is available', async () => {
mocks.setUid2LocalStorage(identity);
__uid2SSProviderScriptLoad();
__uid2InternalHandleScriptLoad();
(window.__uid2 as UID2).init({});
expect(secureSignalProvidersPushMock).toHaveBeenCalled();
await expect(secureSignalProvidersPushMock).toHaveBeenCalledWith(
expect.objectContaining({
id: 'uidapi.com',
})
);
await mocks.flushPromises();
expect(await secureSignalProvidersPushMock.mock.results[0].value).toBe(
identity.advertising_token
);
});

test('should send signal to Google ESP when SDK initialized and Identity is set after', async () => {
__uid2SSProviderScriptLoad();
__uid2InternalHandleScriptLoad();
(window.__uid2 as UID2).init({});
(window.__uid2 as UID2).setIdentity(identity);
expect(secureSignalProvidersPushMock).toHaveBeenCalled();
await expect(secureSignalProvidersPushMock).toHaveBeenCalledWith(
expect.objectContaining({
id: 'uidapi.com',
})
);
await mocks.flushPromises();
expect(await secureSignalProvidersPushMock.mock.results[0].value).toBe(
identity.advertising_token
);
});

test('should send signal to Google ESP when SDK initialized and SS script is included later', async () => {
__uid2InternalHandleScriptLoad();
(window.__uid2 as UID2).init({ identity });
__uid2SSProviderScriptLoad();
// will fire on both InitCompleted and SdkLoaded
expect(secureSignalProvidersPushMock).toHaveBeenCalledTimes(2);
await expect(secureSignalProvidersPushMock).toHaveBeenCalledWith(
expect.objectContaining({
id: 'uidapi.com',
})
);
await mocks.flushPromises();
expect(await secureSignalProvidersPushMock.mock.results[0].value).toBe(
identity.advertising_token
);
});
});

describe('When script loaded after SDK loaded', () => {
Expand Down Expand Up @@ -184,6 +236,7 @@ describe('Secure Signal Tests', () => {
describe('When SDK initialized after both SDK and SS script loaded - UID2', () => {
beforeEach(() => {
window.__uid2 = new UID2();
mocks.removeUid2LocalStorage();
});
test('should send identity to Google ESP', async () => {
__uid2InternalHandleScriptLoad();
Expand Down
16 changes: 11 additions & 5 deletions src/secureSignalEuid.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@ts-nocheck
import { isDebugModeOn, UidSecureSignalProvider } from './secureSignal_shared';
import { UidSecureSignalProviderType } from './secureSignal_types';

Expand All @@ -15,14 +16,19 @@ export function __euidSSProviderScriptLoad() {
isDebugModeOn(INTEG_BASE_URL),
true
);
// For UID2 SDK integration
// For EUID SDK integration
window.__euid = window.__euid || {
callbacks: [],
};
window.__euid.callbacks?.push((eventType) => {
//@ts-ignore
if (eventType === 'SdkLoaded') {
window.__euidSecureSignalProvider!.registerSecureSignalProvider();
window.__euid.callbacks.push((eventType) => {
if (
eventType === 'InitCompleted' ||
eventType === 'SdkLoaded' ||
eventType === 'IdentityUpdated'
) {
if (window.__euid.getIdentity()) {
window.__euidSecureSignalProvider.registerSecureSignalProvider();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not do the check here to see if it is already registered instead of inside the register function?

}
});
}
Expand Down
14 changes: 10 additions & 4 deletions src/secureSignalUid2.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//@ts-nocheck
import { isDebugModeOn, UidSecureSignalProvider } from './secureSignal_shared';
import { UidSecureSignalProviderType } from './secureSignal_types';

Expand All @@ -16,10 +17,15 @@ export function __uid2SSProviderScriptLoad() {
window.__uid2 = window.__uid2 || {
callbacks: [],
};
window.__uid2.callbacks?.push((eventType) => {
//@ts-ignore
if (eventType === 'SdkLoaded') {
window.__uid2SecureSignalProvider!.registerSecureSignalProvider();
window.__uid2.callbacks.push((eventType) => {
if (
eventType === 'InitCompleted' ||
eventType === 'SdkLoaded' ||
eventType === 'IdentityUpdated'
) {
if (window.__uid2.getIdentity()) {
window.__uid2SecureSignalProvider.registerSecureSignalProvider();
}
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"display": "Node 16",

"compilerOptions": {
"lib": ["es2021"],
"lib": ["DOM", "es2022"],
"module": "commonjs",
"target": "es6",
"target": "es2022",
"allowJs": true,
"strict": true,
"esModuleInterop": true,
Expand Down
Loading