Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/integrationTests/async.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ let uid2: UID2;
let xhrMock: any;
mocks.setupFakeTime();

beforeAll(() => {
mocks.setWarnMock();
});

afterAll(() => {
mocks.clearWarnMock();
});

beforeEach(() => {
callback = jest.fn();
uid2 = new UID2();
Expand Down Expand Up @@ -238,14 +246,11 @@ testCookieAndLocalStorage(() => {
// Reset window UID2 instance
const callback = jest.fn((eventType: EventType) => {
if (eventType === UID2.EventType.SdkLoaded) {
console.log('Trying');
try {
(sdkWindow.__uid2 as UID2).init({ identity, useCookie: useCookie });
} catch (ex) {
console.log(ex);
throw ex;
}
console.log('Succeeded');
}
});
test('the SDK should be initialized correctly', () => {
Expand All @@ -257,8 +262,6 @@ testCookieAndLocalStorage(() => {
if (!(sdkWindow.__uid2 instanceof UID2))
throw Error('UID2 should be ready to use by the time SdkLoaded is triggered.');
expect(callback).toHaveBeenNthCalledWith(1, UID2.EventType.SdkLoaded, expect.anything());
console.log(sdkWindow.__uid2.getAdvertisingToken());
console.log(identity.advertising_token);
expect(sdkWindow.__uid2.getAdvertisingToken()).toBe(identity.advertising_token);
});
});
Expand Down
8 changes: 8 additions & 0 deletions src/integrationTests/autoRefresh.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ let getAdvertisingTokenPromise: Promise<string | undefined>;

mocks.setupFakeTime();

beforeAll(() => {
mocks.setWarnMock();
});

afterAll(() => {
mocks.clearWarnMock();
});

beforeEach(() => {
callback = jest.fn();
uid2 = new UID2();
Expand Down
14 changes: 11 additions & 3 deletions src/integrationTests/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ let xhrMock: any;

mocks.setupFakeTime();

beforeAll(() => {
mocks.setWarnMock();
});

afterAll(() => {
mocks.clearWarnMock();
});

beforeEach(() => {
callback = jest.fn();
uid2 = new UID2();
Expand Down Expand Up @@ -1011,7 +1019,7 @@ describe('SDK bootstraps itself if init has already been completed', () => {
sdkWindow.__uid2 = new UID2();
});

test('should bootstrap therefore public functions should return the correct values without calling init again', async () => {
test.skip('should bootstrap therefore public functions should return the correct values without calling init again', async () => {
const identity = { ...makeIdentity(), refresh_from: Date.now() + 100 };

uid2.init({ identity });
Expand All @@ -1027,13 +1035,13 @@ describe('SDK bootstraps itself if init has already been completed', () => {
await uid2.setIdentityFromEmail(email, mocks.makeUid2CstgOption());
}).not.toThrow();
expect(async () => {
uid2.setIdentityFromEmailHash(emailHash, mocks.makeUid2CstgOption());
await uid2.setIdentityFromEmailHash(emailHash, mocks.makeUid2CstgOption());
}).not.toThrow();
expect(async () => {
await uid2.setIdentityFromPhone(phone, mocks.makeUid2CstgOption());
}).not.toThrow();
expect(async () => {
uid2.setIdentityFromPhoneHash(phoneHash, mocks.makeUid2CstgOption());
await uid2.setIdentityFromPhoneHash(phoneHash, mocks.makeUid2CstgOption());
}).not.toThrow();
});

Expand Down
8 changes: 8 additions & 0 deletions src/integrationTests/compatibility.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ let xhrMock: any;

mocks.setupFakeTime();

beforeAll(() => {
mocks.setWarnMock();
});

afterAll(() => {
mocks.clearWarnMock();
});

beforeEach(() => {
callback = jest.fn();
uid2 = new UID2();
Expand Down
10 changes: 4 additions & 6 deletions src/integrationTests/secureSignal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import * as mocks from '../mocks';
import { UidSecureSignalProvider } from '../secureSignal_shared';
import { __uid2SSProviderScriptLoad } from '../secureSignalUid2';
import { UID2, __uid2InternalHandleScriptLoad } from '../uid2Sdk';
import { clearWarnMock, setWarnMock } from '../mocks';

let consoleWarnMock: any;
let getAdvertisingTokenMock: jest.Mock<() => Promise<string>>;
let secureSignalProvidersPushMock: jest.Mock<(p: any) => Promise<void>>;
let uid2ESP: UidSecureSignalProvider;
Expand All @@ -20,13 +20,11 @@ describe('Secure Signal Tests', () => {
push: secureSignalProvidersPushMock,
},
};
consoleWarnMock = jest.spyOn(console, 'warn').mockImplementation(() => {
return;
});
setWarnMock();
});

afterEach(() => {
consoleWarnMock.mockRestore();
clearWarnMock();
getAdvertisingTokenMock.mockRestore;
secureSignalProvidersPushMock.mockRestore();
window.getUid2AdvertisingToken = undefined;
Expand Down Expand Up @@ -61,7 +59,7 @@ describe('Secure Signal Tests', () => {
test('should log warning message to console and not send message', () => {
uid2ESP.registerSecureSignalProvider();
expect(console.warn).toHaveBeenCalledTimes(1);
expect(consoleWarnMock).toHaveBeenCalledWith(
expect(mocks.warnMock).toHaveBeenCalledWith(
'UidSecureSignal: Please implement `getUidAdvertisingToken`'
);
expect(secureSignalProvidersPushMock).not.toBeCalled();
Expand Down
15 changes: 14 additions & 1 deletion src/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { Identity } from './Identity';
import { base64ToBytes, bytesToBase64 } from './encoding/base64';
import * as crypto from 'crypto';
import { ProductDetails } from './product';

import { MockInstance } from 'jest-mock';
import { jest as jestGlobal } from '@jest/globals';
const uid2LocalStorageKeyName = 'UID2-sdk-identity';
const euidLocalStorageKeyName = 'EUID-sdk-identity';

Expand Down Expand Up @@ -419,3 +420,15 @@ export function resetCrypto(window: Window) {
},
});
}

export let warnMock: MockInstance;
export function setWarnMock() {
warnMock = jestGlobal.spyOn(console, 'warn');
warnMock.mockImplementation(() => {
return;
});
}

export function clearWarnMock() {
warnMock.mockRestore();
}
4 changes: 0 additions & 4 deletions src/sdkBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ export abstract class SdkBase {
}

public async setIdentityFromEmail(email: string, opts: ClientSideIdentityOptions) {
this._logger.log('Sending request', email);
this.throwIfInitNotComplete('Cannot set identity before calling init.');
isClientSideIdentityOptionsOrThrow(opts, this._product.name);

Expand Down Expand Up @@ -225,12 +224,10 @@ export abstract class SdkBase {

if (opts.baseUrl && opts.baseUrl !== previousOpts.baseUrl) {
this._apiClient?.updateBaseUrl(opts.baseUrl);
this._logger.log('BaseUrl updated for ApiClient');
}

if (opts.callback && opts.callback !== previousOpts.callback) {
this._initCallbackManager?.addInitCallback(opts.callback);
this._logger.log('init callback added to list');
}

const useNewIdentity =
Expand All @@ -246,7 +243,6 @@ export abstract class SdkBase {

if (opts.refreshRetryPeriod && previousOpts.refreshRetryPeriod !== opts.refreshRetryPeriod) {
this.setRefreshTimer();
this._logger.log('new refresh period set and refresh timer set');
}

updateConfig(this._opts, this._product, previousOpts);
Expand Down