Skip to content
Merged
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
136 changes: 70 additions & 66 deletions src/integrationTests/basic.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { afterEach, beforeEach, describe, expect, jest, test } from '@jest/globals';

import { ApiClient } from '../apiClient';
import * as mocks from '../mocks';
import { __uid2InternalHandleScriptLoad, sdkWindow, UID2 } from '../uid2Sdk';
import { CallbackHandler, EventType } from '../callbackManager';
Expand Down Expand Up @@ -127,7 +128,7 @@ testCookieAndLocalStorage(() => {
expect(() =>
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
uid2.init({ callback: () => {}, refreshRetryPeriod: 'abc' })
uid2.init({ callback: () => {}, refreshRetryPeriod: 'abc' }),
).toThrow(TypeError);
});
test('should fail on refreshRetryPeriod being less than 1 second', () => {
Expand All @@ -153,7 +154,7 @@ testCookieAndLocalStorage(() => {
advertisingToken: undefined,
advertising_token: undefined,
status: UID2.IdentityStatus.NO_IDENTITY,
})
}),
);
});
test('should not set value', () => {
Expand Down Expand Up @@ -181,7 +182,7 @@ testCookieAndLocalStorage(() => {
advertisingToken: undefined,
advertising_token: undefined,
status: UID2.IdentityStatus.NO_IDENTITY,
})
}),
);
});
test('should clear value', () => {
Expand Down Expand Up @@ -211,7 +212,7 @@ testCookieAndLocalStorage(() => {
advertisingToken: identity.advertising_token,
advertising_token: identity.advertising_token,
status: UID2.IdentityStatus.ESTABLISHED,
})
}),
);
});
test('should set value', () => {
Expand Down Expand Up @@ -243,7 +244,7 @@ testCookieAndLocalStorage(() => {
advertisingToken: undefined,
advertising_token: undefined,
status: UID2.IdentityStatus.REFRESH_EXPIRED,
})
}),
);
});
test('should clear value', () => {
Expand Down Expand Up @@ -305,7 +306,7 @@ testCookieAndLocalStorage(() => {
expect(xhrMock.send).toHaveBeenCalledTimes(1);
expect(xhrMock.open).toHaveBeenLastCalledWith('POST', uid2RefreshUrl, true);
expect(xhrMock.send).toHaveBeenLastCalledWith(identity.refresh_token);
xhrMock.onreadystatechange();
xhrMock.onreadystatechange(new Event(''));
expect(cryptoMock.subtle.importKey).toHaveBeenCalled();
});

Expand Down Expand Up @@ -333,7 +334,7 @@ testCookieAndLocalStorage(() => {
expect(xhrMock.send).toHaveBeenCalledTimes(1);
expect(xhrMock.open).toHaveBeenLastCalledWith('POST', uid2RefreshUrl, true);
expect(xhrMock.send).toHaveBeenLastCalledWith(identity.refresh_token);
xhrMock.onreadystatechange();
xhrMock.onreadystatechange(new Event(''));
expect(cryptoMock.subtle.importKey).toHaveBeenCalledTimes(0);
mocks.resetCrypto(sdkWindow);
});
Expand Down Expand Up @@ -375,7 +376,7 @@ testCookieAndLocalStorage(() => {
advertisingToken: undefined,
advertising_token: undefined,
status: UID2.IdentityStatus.INVALID,
})
}),
);
});
test('should clear value', () => {
Expand Down Expand Up @@ -408,7 +409,7 @@ testCookieAndLocalStorage(() => {
advertisingToken: identity.advertising_token,
advertising_token: identity.advertising_token,
status: UID2.IdentityStatus.ESTABLISHED,
})
}),
);
});
test('should set value', () => {
Expand Down Expand Up @@ -447,7 +448,7 @@ testCookieAndLocalStorage(() => {
advertisingToken: initIdentity.advertising_token,
advertising_token: initIdentity.advertising_token,
status: UID2.IdentityStatus.ESTABLISHED,
})
}),
);
});
test('should set value', () => {
Expand Down Expand Up @@ -478,7 +479,7 @@ testCookieAndLocalStorage(() => {
identity: originalIdentity,
useCookie: useCookie,
});
let cryptoMock = new mocks.CryptoMock(sdkWindow);
new mocks.CryptoMock(sdkWindow);
});
afterEach(() => {
mocks.resetCrypto(sdkWindow);
Expand All @@ -496,7 +497,7 @@ testCookieAndLocalStorage(() => {
advertisingToken: updatedIdentity.advertising_token,
advertising_token: updatedIdentity.advertising_token,
status: UID2.IdentityStatus.REFRESHED,
})
}),
);
});
test('should set value', () => {
Expand Down Expand Up @@ -524,7 +525,7 @@ testCookieAndLocalStorage(() => {
advertisingToken: originalIdentity.advertising_token,
advertising_token: originalIdentity.advertising_token,
status: UID2.IdentityStatus.ESTABLISHED,
})
}),
);
});
test('should set value', () => {
Expand All @@ -551,7 +552,7 @@ testCookieAndLocalStorage(() => {
advertisingToken: undefined,
advertising_token: undefined,
status: UID2.IdentityStatus.OPTOUT,
})
}),
);
});
test('should set cookie to optout', () => {
Expand Down Expand Up @@ -579,7 +580,7 @@ testCookieAndLocalStorage(() => {
advertisingToken: undefined,
advertising_token: undefined,
status: UID2.IdentityStatus.REFRESH_EXPIRED,
})
}),
);
});
test('should not set cookie', () => {
Expand Down Expand Up @@ -610,7 +611,7 @@ testCookieAndLocalStorage(() => {
advertisingToken: originalIdentity.advertising_token,
advertising_token: originalIdentity.advertising_token,
status: UID2.IdentityStatus.ESTABLISHED,
})
}),
);
});
test('should set value', () => {
Expand Down Expand Up @@ -638,7 +639,7 @@ testCookieAndLocalStorage(() => {
advertisingToken: originalIdentity.advertising_token,
advertising_token: originalIdentity.advertising_token,
status: UID2.IdentityStatus.ESTABLISHED,
})
}),
);
});
test('should set value', () => {
Expand Down Expand Up @@ -666,7 +667,7 @@ testCookieAndLocalStorage(() => {
advertisingToken: originalIdentity.advertising_token,
advertising_token: originalIdentity.advertising_token,
status: UID2.IdentityStatus.ESTABLISHED,
})
}),
);
});
test('should set value', () => {
Expand Down Expand Up @@ -694,7 +695,7 @@ testCookieAndLocalStorage(() => {
advertisingToken: originalIdentity.advertising_token,
advertising_token: originalIdentity.advertising_token,
status: UID2.IdentityStatus.ESTABLISHED,
})
}),
);
});
test('should set value', () => {
Expand Down Expand Up @@ -722,7 +723,7 @@ testCookieAndLocalStorage(() => {
advertisingToken: undefined,
advertising_token: undefined,
status: UID2.IdentityStatus.REFRESH_EXPIRED,
})
}),
);
});
test('should not set cookie', () => {
Expand Down Expand Up @@ -754,7 +755,7 @@ testCookieAndLocalStorage(() => {
identity: originalIdentity,
useCookie: useCookie,
});
let cryptoMock = new mocks.CryptoMock(sdkWindow);
new mocks.CryptoMock(sdkWindow);
});
afterEach(() => {
mocks.resetCrypto(sdkWindow);
Expand All @@ -773,7 +774,7 @@ testCookieAndLocalStorage(() => {
advertisingToken: updatedIdentity.advertising_token,
advertising_token: updatedIdentity.advertising_token,
status: UID2.IdentityStatus.REFRESHED,
})
}),
);
});
test('should set value', () => {
Expand All @@ -796,7 +797,7 @@ testCookieAndLocalStorage(() => {
xhrMock.responseText = btoa(
JSON.stringify({
status: 'optout',
})
}),
);
xhrMock.onreadystatechange(new Event(''));
});
Expand All @@ -807,7 +808,7 @@ testCookieAndLocalStorage(() => {
advertisingToken: undefined,
advertising_token: undefined,
status: UID2.IdentityStatus.OPTOUT,
})
}),
);
});
test('should invoke the callback with no identity', () => {
Expand Down Expand Up @@ -843,7 +844,7 @@ testCookieAndLocalStorage(() => {
advertisingToken: undefined,
advertising_token: undefined,
status: UID2.IdentityStatus.REFRESH_EXPIRED,
})
}),
);
});
test('should not set cookie', () => {
Expand Down Expand Up @@ -874,7 +875,7 @@ testCookieAndLocalStorage(() => {
advertisingToken: undefined,
advertising_token: undefined,
status: UID2.IdentityStatus.EXPIRED,
})
}),
);
});
test('should set value', () => {
Expand Down Expand Up @@ -903,7 +904,7 @@ testCookieAndLocalStorage(() => {
advertisingToken: undefined,
advertising_token: undefined,
status: UID2.IdentityStatus.REFRESH_EXPIRED,
})
}),
);
});
test('should not set cookie', () => {
Expand Down Expand Up @@ -1015,13 +1016,23 @@ describe('SDK bootstraps itself if init has already been completed', () => {
const phone = '+12345678901';
const phoneHash = 'EObwtHBUqDNZR33LNSMdtt5cafsYFuGmuY4ZLenlue4=';

let callCstgApiSpy: jest.SpiedFunction<ApiClient['callCstgApi']>;

beforeEach(() => {
sdkWindow.__uid2 = new UID2();
});

test.skip('should bootstrap therefore public functions should return the correct values without calling init again', async () => {
afterEach(() => {
callCstgApiSpy?.mockRestore();
});

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

callCstgApiSpy = jest
.spyOn(ApiClient.prototype, 'callCstgApi')
.mockResolvedValue({ status: 'success', identity });

uid2.init({ identity });

// mimicking closing the page, but not re-calling the UID2 constructor
Expand All @@ -1031,36 +1042,32 @@ describe('SDK bootstraps itself if init has already been completed', () => {

expect(uid2.getAdvertisingToken()).toBe(identity.advertising_token);
expect(uid2.getIdentity()).toStrictEqual(identity);
expect(async () => {
await uid2.setIdentityFromEmail(email, mocks.makeUid2CstgOption());
}).not.toThrow();
expect(async () => {
await uid2.setIdentityFromEmailHash(emailHash, mocks.makeUid2CstgOption());
}).not.toThrow();
expect(async () => {
await uid2.setIdentityFromPhone(phone, mocks.makeUid2CstgOption());
}).not.toThrow();
expect(async () => {
await uid2.setIdentityFromPhoneHash(phoneHash, mocks.makeUid2CstgOption());
}).not.toThrow();
await expect(
uid2.setIdentityFromEmail(email, mocks.makeUid2CstgOption()),
).resolves.toBeUndefined();
await expect(
uid2.setIdentityFromEmailHash(emailHash, mocks.makeUid2CstgOption()),
).resolves.toBeUndefined();
await expect(
uid2.setIdentityFromPhone(phone, mocks.makeUid2CstgOption()),
).resolves.toBeUndefined();
await expect(
uid2.setIdentityFromPhoneHash(phoneHash, mocks.makeUid2CstgOption()),
).resolves.toBeUndefined();
});

test('should not bootstrap therefore public functions error or return undefined/null', async () => {
__uid2InternalHandleScriptLoad();
expect(uid2.getAdvertisingToken()).toBe(undefined);
expect(uid2.getIdentity()).toStrictEqual(null);
expect(async () => {
await uid2.setIdentityFromEmail(email, mocks.makeUid2CstgOption());
}).rejects.toThrow();
expect(async () => {
await uid2.setIdentityFromEmailHash(emailHash, mocks.makeUid2CstgOption());
}).rejects.toThrow();
expect(async () => {
await uid2.setIdentityFromPhone(phone, mocks.makeUid2CstgOption());
}).rejects.toThrow();
expect(async () => {
await uid2.setIdentityFromPhoneHash(phoneHash, mocks.makeUid2CstgOption());
}).rejects.toThrow();
await expect(uid2.setIdentityFromEmail(email, mocks.makeUid2CstgOption())).rejects.toThrow();
await expect(
uid2.setIdentityFromEmailHash(emailHash, mocks.makeUid2CstgOption()),
).rejects.toThrow();
await expect(uid2.setIdentityFromPhone(phone, mocks.makeUid2CstgOption())).rejects.toThrow();
await expect(
uid2.setIdentityFromPhoneHash(phoneHash, mocks.makeUid2CstgOption()),
).rejects.toThrow();
});
});

Expand All @@ -1084,24 +1091,21 @@ describe('Token retrieval and related public functions working without init', ()
},
];

identityStorageFunctions.forEach((functions) => {
for (const functions of identityStorageFunctions) {
functions.setIdentity();
expect(uid2.getAdvertisingToken()).toBe(identity.advertising_token);
expect(uid2.getIdentity()).toStrictEqual(identity);
// Without init, getAdvertisingTokenAsync() returns a deferred promise that only resolves after init; do not await to avoid timeout.
expect(uid2.getAdvertisingTokenAsync()).resolves.toBe(identity.advertising_token);
expect(async () => {
await uid2.setIdentityFromEmail(email, mocks.makeUid2CstgOption());
}).rejects.toThrow();
expect(async () => {
await uid2.setIdentityFromEmailHash(emailHash, mocks.makeUid2CstgOption());
}).rejects.toThrow();
expect(async () => {
await uid2.setIdentityFromPhone(phone, mocks.makeUid2CstgOption());
}).rejects.toThrow();
expect(async () => {
await uid2.setIdentityFromPhoneHash(phoneHash, mocks.makeUid2CstgOption());
}).rejects.toThrow();
await expect(uid2.setIdentityFromEmail(email, mocks.makeUid2CstgOption())).rejects.toThrow();
await expect(
uid2.setIdentityFromEmailHash(emailHash, mocks.makeUid2CstgOption()),
).rejects.toThrow();
await expect(uid2.setIdentityFromPhone(phone, mocks.makeUid2CstgOption())).rejects.toThrow();
await expect(
uid2.setIdentityFromPhoneHash(phoneHash, mocks.makeUid2CstgOption()),
).rejects.toThrow();
functions.removeIdentity();
});
}
});
});
Loading