Skip to content

Commit f8b39dd

Browse files
no identity available new event
1 parent 59856db commit f8b39dd

File tree

3 files changed

+87
-40
lines changed

3 files changed

+87
-40
lines changed

src/callbackManager.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ export type PayloadWithIdentity = {
1919
};
2020

2121
export class CallbackManager {
22-
private _getIdentity: () => Identity | null | undefined;
22+
private _getIdentity: (isForCallback: boolean) => Identity | null | undefined;
2323
private _logger: Logger;
2424
private _sdk: SdkBase;
2525
private _productName: string;
2626
constructor(
2727
sdk: SdkBase,
2828
productName: string,
29-
getIdentity: () => Identity | null | undefined,
29+
getIdentity: (isForCallback: boolean) => Identity | null | undefined,
3030
logger: Logger
3131
) {
3232
this._productName = productName;
@@ -44,7 +44,7 @@ export class CallbackManager {
4444
this.safeRunCallback(c, EventType.SdkLoaded, {});
4545
if (this._sentInit)
4646
this.safeRunCallback(c, EventType.InitCompleted, {
47-
identity: this._getIdentity() ?? null,
47+
identity: this._getIdentity(true) ?? null,
4848
});
4949
}
5050
return Array.prototype.push.apply(this._sdk.callbacks, args);
@@ -57,7 +57,7 @@ export class CallbackManager {
5757

5858
const enrichedPayload = {
5959
...payload,
60-
identity: this._getIdentity() ?? null,
60+
identity: this._getIdentity(true) ?? null,
6161
};
6262
for (const callback of this._sdk.callbacks) {
6363
this.safeRunCallback(callback, event, enrichedPayload);

src/integrationTests/options.test.ts

+69-27
Original file line numberDiff line numberDiff line change
@@ -556,15 +556,23 @@ describe('Store config UID2', () => {
556556
});
557557
});
558558

559-
describe('when no identity is sent in init', () => {
559+
describe('calls the NoIdentityAvailable event', () => {
560560
let handler: ReturnType<typeof jest.fn>;
561561
beforeEach(() => {
562562
handler = jest.fn();
563563
uid2.callbacks.push(handler);
564564
});
565-
test('runs NoIdentityAvailable event', () => {
565+
566+
test('when init is called for the first time with no identity', () => {
567+
uid2.init({});
568+
expect(handler).toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, { identity: null });
569+
});
570+
test('when init is already complete and called again with no identity', () => {
571+
uid2.init({});
566572
uid2.init({});
567573
expect(handler).toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, { identity: null });
574+
});
575+
test('when init is already complete and called again with an expired identity', () => {
568576
uid2.init({});
569577
expect(handler).toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, { identity: null });
570578
let expiredIdentity = makeIdentity({
@@ -578,55 +586,89 @@ describe('Store config UID2', () => {
578586
identity: expiredIdentity,
579587
});
580588
});
581-
test('runs NoIdentityAvailable event', () => {
589+
test('when init is already complete but the existing identity is expired', () => {
582590
let expiredIdentity = makeIdentity({
583591
identity_expires: Date.now() - 100000,
584592
refresh_expires: Date.now() - 100000,
585593
});
586594
uid2.init({
587595
identity: expiredIdentity,
588596
});
597+
expect(handler).toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, {
598+
identity: expiredIdentity,
599+
});
589600
uid2.init({});
590601
expect(handler).toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, {
591602
identity: expiredIdentity,
592603
});
593604
});
594-
test('runs NoIdentityAvailable event', () => {
595-
let expiredIdentity1 = makeIdentity({
596-
identity_expires: Date.now() - 5000,
597-
refresh_expires: Date.now() - 5000,
598-
});
599-
let expiredIdentity2 = makeIdentity({
600-
identity_expires: Date.now() - 100000,
601-
refresh_expires: Date.now() - 100000,
605+
test('when identity is expired but refreshable', () => {
606+
let expiredRefreshableIdentity = makeIdentity({
607+
identity_expires: Date.now() - 10000,
608+
refresh_expires: Date.now() + 10000,
602609
});
603-
uid2.init({
604-
identity: expiredIdentity1,
610+
uid2.setIdentity(expiredRefreshableIdentity);
611+
612+
// in this case, identity is temporarily unavailable but still unavailable
613+
expect(handler).toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, {
614+
identity: null,
605615
});
606-
uid2.init({ identity: expiredIdentity2 });
616+
});
617+
test('when login is required', () => {
618+
uid2.isLoginRequired();
619+
607620
expect(handler).toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, {
608-
identity: expiredIdentity2,
621+
identity: null,
609622
});
610623
});
611-
test('runs NoIdentityAvailable event', () => {
612-
uid2.init({});
613-
let expiredIdentity = makeIdentity({
614-
identity_expires: Date.now() - 5000,
615-
refresh_expires: Date.now() - 5000,
624+
test('when get identity returns null or get advertising token returns undefined', () => {
625+
const nullIdentity = uid2.getIdentity();
626+
627+
expect(nullIdentity).toBeNull();
628+
expect(handler).toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, {
629+
identity: null,
616630
});
617-
uid2.setIdentity(expiredIdentity);
631+
});
632+
test('when there is no advertising token', () => {
633+
const token = uid2.getAdvertisingToken();
634+
635+
expect(token).toBeUndefined();
618636
expect(handler).toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, {
619637
identity: null,
620638
});
621639
});
622-
test('runs NoIdentityAvailable event', () => {
640+
});
641+
642+
describe('does not call NoIdentityAvailable event', () => {
643+
let handler: ReturnType<typeof jest.fn>;
644+
beforeEach(() => {
645+
handler = jest.fn();
646+
uid2.callbacks.push(handler);
647+
});
648+
649+
test('when setIdentity is run with a valid identity, should not call NoIdentityAvailable on set or get', () => {
623650
uid2.init({});
624-
let expiredIdentity = makeIdentity({
625-
identity_expires: Date.now() - 5000,
626-
refresh_expires: Date.now() - 5000,
651+
let validIdentity = makeIdentity();
652+
uid2.setIdentity(validIdentity);
653+
expect(handler).not.toHaveBeenCalledWith(EventType.NoIdentityAvailable, {
654+
identity: null,
627655
});
628-
uid2.setIdentity(expiredIdentity);
629-
expect(handler).toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, {
656+
657+
uid2.getIdentity();
658+
expect(handler).not.toHaveBeenCalledWith(EventType.NoIdentityAvailable, {
659+
identity: null,
660+
});
661+
662+
uid2.getAdvertisingToken();
663+
expect(handler).not.toHaveBeenCalledWith(EventType.NoIdentityAvailable, {
664+
identity: null,
665+
});
666+
});
667+
test('when identity is set with opted out identity', () => {
668+
uid2.init({});
669+
let optedOutIdentity = makeIdentity({ status: 'optout' });
670+
uid2.setIdentity(optedOutIdentity);
671+
expect(handler).not.toHaveBeenLastCalledWith(EventType.NoIdentityAvailable, {
630672
identity: null,
631673
});
632674
});

src/sdkBase.ts

+14-9
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export abstract class SdkBase {
8484
this._callbackManager = new CallbackManager(
8585
this,
8686
this._product.name,
87-
() => this.getIdentity(),
87+
() => this.getIdentity(true),
8888
this._logger
8989
);
9090
}
@@ -157,20 +157,25 @@ export abstract class SdkBase {
157157
} else {
158158
this.triggerRefreshOrSetTimer(validatedIdentity);
159159
}
160-
this._callbackManager.runCallbacks(EventType.IdentityUpdated, {});
161-
} else {
162-
this._callbackManager.runCallbacks(EventType.NoIdentityAvailable, {});
163160
}
161+
this.isIdentityAvailable();
164162
}
165163

166-
public getIdentity(): Identity | null {
164+
public getIdentity(isForCallback?: boolean): Identity | null {
167165
const identity = this._identity ?? this.getIdentityNoInit();
168-
return identity && !this.temporarilyUnavailable(identity) && !isOptoutIdentity(identity)
169-
? identity
170-
: null;
166+
const isValid =
167+
identity && !this.temporarilyUnavailable(identity) && !isOptoutIdentity(identity);
168+
if (!isValid) {
169+
if (!isForCallback) {
170+
this._callbackManager.runCallbacks(EventType.NoIdentityAvailable, {});
171+
}
172+
return null;
173+
} else {
174+
return identity;
175+
}
171176
}
172177

173-
// When the SDK has been initialized, this function should return the token
178+
// When the SDK has been initialized, this vfunction should return the token
174179
// from the most recent refresh request, if there is a request, wait for the
175180
// new token. Otherwise, returns a promise which will be resolved after init.
176181
public getAdvertisingTokenAsync() {

0 commit comments

Comments
 (0)