From 8375101b7bdef1dc63822c1499737e253aa1ecf4 Mon Sep 17 00:00:00 2001 From: Md Junaed Hossain <169046794+junaed-optimizely@users.noreply.github.com> Date: Tue, 24 Dec 2024 21:49:42 +0600 Subject: [PATCH 1/3] [FSSDK-10980] set user logic adjustment --- src/client.spec.ts | 19 +++++++++++++++++++ src/client.ts | 8 +++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/client.spec.ts b/src/client.spec.ts index 5f92f58..6aa2b82 100644 --- a/src/client.spec.ts +++ b/src/client.spec.ts @@ -315,6 +315,25 @@ describe('ReactSDKClient', () => { expect(instance.getUserContext()).toBe(null); }); + it('if user id is not present, and ODP is explicitly off, user promise will be pending', async () => { + jest.spyOn(mockInnerClient, 'onReady').mockResolvedValue({ success: true }); + + instance = createInstance({ + ...config, + odpOptions: { + disabled: true, + }, + }); + + await instance.setUser(DefaultUser); + expect(instance.isReady()).toBe(false); + + await instance.setUser({ id: 'user123' }); + await instance.onReady(); + + expect(instance.isReady()).toBe(true); + }); + it('can be called with no/default user set', async () => { jest.spyOn(mockOptimizelyUserContext, 'getUserId').mockReturnValue(validVuid); diff --git a/src/client.ts b/src/client.ts index b743021..39c838f 100644 --- a/src/client.ts +++ b/src/client.ts @@ -242,7 +242,7 @@ class OptimizelyReactSDKClient implements ReactSDKClient { this._client = optimizely.createInstance(configWithClientInfo); this.isClientReady = !!this.getOptimizelyConfig(); this.isUsingSdkKey = !!configWithClientInfo.sdkKey; - + console.log('I am in the constructor', this.isClientReady, this.isUsingSdkKey); if (this._client) { const clientReadyPromise = this._client.onReady(); @@ -250,6 +250,8 @@ class OptimizelyReactSDKClient implements ReactSDKClient { ([userResult, clientResult]) => { this.isClientReady = clientResult.success; this.isUserReady = userResult.success; + console.log('isClientReady', this.isClientReady); + console.log('isUserReady', this.isUserReady); const clientAndUserReady = this.isReady(); this.clientAndUserReadyPromiseFulfilled = true; @@ -382,6 +384,10 @@ class OptimizelyReactSDKClient implements ReactSDKClient { } public async setUser(userInfo: UserInfo): Promise { + // If user id is not present and ODP is explicitly off, user promise will be pending + if (userInfo?.id === null && this.odpExplicitlyOff) { + return; + } this.user = { id: userInfo.id || DefaultUser.id, attributes: userInfo.attributes || DefaultUser.attributes, From 7713f4d2666a2fba607005cce53a88b53194469a Mon Sep 17 00:00:00 2001 From: Md Junaed Hossain <169046794+junaed-optimizely@users.noreply.github.com> Date: Tue, 24 Dec 2024 21:52:18 +0600 Subject: [PATCH 2/3] [FSSDK-10980] log removal + comment improvement --- src/client.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/client.ts b/src/client.ts index 39c838f..d910a15 100644 --- a/src/client.ts +++ b/src/client.ts @@ -242,7 +242,7 @@ class OptimizelyReactSDKClient implements ReactSDKClient { this._client = optimizely.createInstance(configWithClientInfo); this.isClientReady = !!this.getOptimizelyConfig(); this.isUsingSdkKey = !!configWithClientInfo.sdkKey; - console.log('I am in the constructor', this.isClientReady, this.isUsingSdkKey); + if (this._client) { const clientReadyPromise = this._client.onReady(); @@ -250,8 +250,6 @@ class OptimizelyReactSDKClient implements ReactSDKClient { ([userResult, clientResult]) => { this.isClientReady = clientResult.success; this.isUserReady = userResult.success; - console.log('isClientReady', this.isClientReady); - console.log('isUserReady', this.isUserReady); const clientAndUserReady = this.isReady(); this.clientAndUserReadyPromiseFulfilled = true; @@ -384,7 +382,7 @@ class OptimizelyReactSDKClient implements ReactSDKClient { } public async setUser(userInfo: UserInfo): Promise { - // If user id is not present and ODP is explicitly off, user promise will be pending + // If user id is not present and ODP is explicitly off, user promise will be pending until setUser is called again with proper user id if (userInfo?.id === null && this.odpExplicitlyOff) { return; } From 200da3a208f10d779dd34280a89462485638df17 Mon Sep 17 00:00:00 2001 From: Md Junaed Hossain <169046794+junaed-optimizely@users.noreply.github.com> Date: Tue, 24 Dec 2024 21:58:13 +0600 Subject: [PATCH 3/3] [FSSDK-10980] optional chain removed --- src/client.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/client.ts b/src/client.ts index d910a15..468f4f4 100644 --- a/src/client.ts +++ b/src/client.ts @@ -383,7 +383,7 @@ class OptimizelyReactSDKClient implements ReactSDKClient { public async setUser(userInfo: UserInfo): Promise { // If user id is not present and ODP is explicitly off, user promise will be pending until setUser is called again with proper user id - if (userInfo?.id === null && this.odpExplicitlyOff) { + if (userInfo.id === null && this.odpExplicitlyOff) { return; } this.user = {