Skip to content

Commit 215d3fe

Browse files
rafinutshaw-optimizelymikechu-optimizelyMike Chu
authored
[FSSDK-8941] [React] Enable ODP Functionality in React Context (#199)
* Update package.json to use JS beta * WIP: Update package.json to use JS beta methods * [FSSDK-8941] noop commit to link to Jira Just testing something * Add locations of new fetchQualifedSegments tests * Update unit tests new interface and async/await changes * Stub unit tests * Set react-sdk beta version * Remove fetchQualSegs in setUser & update tests * Update yarn.lock * Remove fetchQS tests w/i setUser * WIP: Update fetchQualifiedSegments tests * Rollback premature version bump * Update tests * Fix format and test * Update copyright --------- Co-authored-by: Mike Chu <[email protected]> Co-authored-by: Mike Chu <[email protected]>
1 parent e91f8e3 commit 215d3fe

File tree

4 files changed

+99
-37
lines changed

4 files changed

+99
-37
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"access": "public"
3434
},
3535
"dependencies": {
36-
"@optimizely/optimizely-sdk": "^4.9.2",
36+
"@optimizely/optimizely-sdk": "^5.0.0-beta",
3737
"hoist-non-react-statics": "^3.3.0",
3838
"prop-types": "^15.6.2",
3939
"utility-types": "^2.1.0 || ^3.0.0"

src/client.spec.ts

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2019-2022, Optimizely
2+
* Copyright 2019-2023, Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,21 +15,24 @@
1515
*/
1616
jest.mock('@optimizely/optimizely-sdk');
1717
jest.mock('./logger', () => {
18-
return { logger: {
19-
warn : jest.fn(() => ()=>{}),
20-
info : jest.fn(() => ()=>{}),
21-
error : jest.fn(() => ()=>{}),
22-
debug : jest.fn(() => ()=>{})
23-
} };
24-
})
25-
18+
return {
19+
logger: {
20+
warn: jest.fn(() => () => {}),
21+
info: jest.fn(() => () => {}),
22+
error: jest.fn(() => () => {}),
23+
debug: jest.fn(() => () => {}),
24+
},
25+
};
26+
});
27+
2628
import * as optimizely from '@optimizely/optimizely-sdk';
2729

2830
import { createInstance, OnReadyResult, ReactSDKClient } from './client';
31+
import { logger } from './logger';
32+
2933
interface MockedReactSDKClient extends ReactSDKClient {
3034
client: optimizely.Client;
3135
initialConfig: optimizely.Config;
32-
3336
}
3437

3538
describe('ReactSDKClient', () => {
@@ -46,6 +49,7 @@ describe('ReactSDKClient', () => {
4649
decide: jest.fn(),
4750
decideAll: jest.fn(),
4851
decideForKeys: jest.fn(),
52+
fetchQualifiedSegments: jest.fn(),
4953
setForcedDecision: jest.fn(),
5054
removeForcedDecision: jest.fn(),
5155
removeAllForcedDecisions: jest.fn(),
@@ -72,6 +76,8 @@ describe('ReactSDKClient', () => {
7276
getOptimizelyConfig: jest.fn(() => null),
7377
onReady: jest.fn(() => Promise.resolve({ success: false })),
7478
close: jest.fn(),
79+
getVuid: jest.fn(),
80+
sendOdpEvent: jest.fn(),
7581
notificationCenter: {
7682
addNotificationListener: jest.fn(() => 0),
7783
removeNotificationListener: jest.fn(() => false),
@@ -1177,6 +1183,45 @@ describe('ReactSDKClient', () => {
11771183
});
11781184
});
11791185

1186+
describe('fetchQualifedSegments', () => {
1187+
let instance: ReactSDKClient;
1188+
beforeEach(() => {
1189+
instance = createInstance(config);
1190+
});
1191+
1192+
it('should never call fetchQualifiedSegments if Optimizely user context is falsy', async () => {
1193+
const result = await instance.fetchQualifiedSegments();
1194+
1195+
expect(result).toEqual(false);
1196+
expect(logger.warn).toHaveBeenCalledTimes(1);
1197+
expect(logger.warn).toBeCalledWith(
1198+
'Unable to fetch qualified segments for user because Optimizely client failed to initialize.'
1199+
);
1200+
});
1201+
1202+
it('should return false if fetch fails', async () => {
1203+
instance.setUser({
1204+
id: 'user1',
1205+
});
1206+
1207+
jest.spyOn(instance, 'fetchQualifiedSegments').mockImplementation(async () => false);
1208+
const result = await instance.fetchQualifiedSegments();
1209+
1210+
expect(result).toEqual(false);
1211+
});
1212+
1213+
it('should return true if fetch successful', async () => {
1214+
instance.setUser({
1215+
id: 'user1',
1216+
});
1217+
1218+
jest.spyOn(instance, 'fetchQualifiedSegments').mockImplementation(async () => true);
1219+
const result = await instance.fetchQualifiedSegments();
1220+
1221+
expect(result).toEqual(true);
1222+
});
1223+
});
1224+
11801225
describe('onForcedVariationsUpdate', () => {
11811226
let instance: ReactSDKClient;
11821227
beforeEach(() => {

src/client.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2019-2022, Optimizely
2+
* Copyright 2019-2023, Optimizely
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,7 +22,7 @@ import { logger } from './logger';
2222
export type VariableValuesObject = {
2323
[key: string]: any;
2424
};
25-
25+
2626
type DisposeFn = () => void;
2727

2828
type OnUserUpdateHandler = (userInfo: UserInfo) => void;
@@ -170,6 +170,8 @@ export interface ReactSDKClient extends Omit<optimizely.Client, 'createUserConte
170170
removeForcedDecision(decisionContext: optimizely.OptimizelyDecisionContext): boolean;
171171

172172
getForcedDecision(decisionContext: optimizely.OptimizelyDecisionContext): optimizely.OptimizelyForcedDecision | null;
173+
174+
fetchQualifiedSegments(): Promise<boolean>;
173175
}
174176

175177
export const DEFAULT_ON_READY_TIMEOUT = 5000;
@@ -211,7 +213,7 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
211213
*/
212214
constructor(config: optimizely.Config) {
213215
this.initialConfig = config;
214-
this.userPromiseResolver = () => { };
216+
this.userPromiseResolver = () => {};
215217

216218
const configWithClientInfo = {
217219
...config,
@@ -321,6 +323,15 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
321323
return null;
322324
}
323325

326+
async fetchQualifiedSegments(): Promise<boolean> {
327+
if (!this.userContext) {
328+
logger.warn('Unable to fetch qualified segments for user because Optimizely client failed to initialize.');
329+
return false;
330+
}
331+
332+
return await this.userContext.fetchQualifiedSegments();
333+
}
334+
324335
setUser(userInfo: UserInfo): void {
325336
// TODO add check for valid user
326337
if (userInfo.id) {
@@ -1123,7 +1134,7 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
11231134
return new Promise<{ success: boolean; reason: string }>((resolve, reject) =>
11241135
resolve({
11251136
success: true,
1126-
reason: 'Optimizely client is not initialized.'
1137+
reason: 'Optimizely client is not initialized.',
11271138
})
11281139
);
11291140
}
@@ -1176,6 +1187,21 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
11761187
attributes: finalUserAttributes,
11771188
};
11781189
}
1190+
1191+
// TODO: discuss if we want to expose these method and provide implementation
1192+
getVuid(): string | undefined {
1193+
return undefined;
1194+
}
1195+
1196+
// TODO: discuss if we want to expose these method and provide implementation
1197+
sendOdpEvent(
1198+
action: string,
1199+
type: string | undefined,
1200+
identifiers: Map<string, string> | undefined,
1201+
data: Map<string, unknown> | undefined
1202+
): void {
1203+
// no-op
1204+
}
11791205
}
11801206

11811207
export function createInstance(config: optimizely.Config): ReactSDKClient {

yarn.lock

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -544,14 +544,6 @@
544544
"@optimizely/js-sdk-utils" "^0.4.0"
545545
decompress-response "^4.2.1"
546546

547-
"@optimizely/js-sdk-event-processor@^0.9.2":
548-
version "0.9.5"
549-
resolved "https://registry.npmjs.org/@optimizely/js-sdk-event-processor/-/js-sdk-event-processor-0.9.5.tgz"
550-
integrity sha512-g5zqAjJuexxgbNvn7dacFkQXQxH3+OtjELfmSswvhxP9EHkyNR0ZdQF/kBxFxr335F2/RRPvAJ9tQBPkwaBg8g==
551-
dependencies:
552-
"@optimizely/js-sdk-logging" "^0.3.1"
553-
"@optimizely/js-sdk-utils" "^0.4.0"
554-
555547
"@optimizely/js-sdk-logging@^0.3.1":
556548
version "0.3.1"
557549
resolved "https://registry.npmjs.org/@optimizely/js-sdk-logging/-/js-sdk-logging-0.3.1.tgz"
@@ -566,17 +558,16 @@
566558
dependencies:
567559
uuid "^3.3.2"
568560

569-
"@optimizely/optimizely-sdk@^4.9.2":
570-
version "4.9.2"
571-
resolved "https://registry.yarnpkg.com/@optimizely/optimizely-sdk/-/optimizely-sdk-4.9.2.tgz#d63edda82e957927a3b9d909aa9f67a34013eb77"
572-
integrity sha512-aPUU2KGDgo0VmbYNXeUCttXQgbBr59leUhyNPidy1J3YixEhzo0Q9UXCzhf4SgCTCtHCwahE7g0Lf36U0IVPgQ==
561+
"@optimizely/optimizely-sdk@^5.0.0-beta":
562+
version "5.0.0-beta"
563+
resolved "https://registry.yarnpkg.com/@optimizely/optimizely-sdk/-/optimizely-sdk-5.0.0-beta.tgz#3290713524707d6328ec2c00a4ec1490410d3bcd"
564+
integrity sha512-WgiZDAJcdRyRPbJlZ96cuVVAF+6IuwK4hzGT6XBPdfRrBm6jLb9oQ3qUhGJMxNBoUFHlaNVnvm/T4lKS/gcy9Q==
573565
dependencies:
574566
"@optimizely/js-sdk-datafile-manager" "^0.9.5"
575-
"@optimizely/js-sdk-event-processor" "^0.9.2"
576-
"@optimizely/js-sdk-logging" "^0.3.1"
567+
decompress-response "^4.2.1"
577568
json-schema "^0.4.0"
578-
murmurhash "0.0.2"
579-
uuid "^3.3.2"
569+
murmurhash "^2.0.1"
570+
uuid "^8.3.2"
580571

581572
"@rollup/plugin-commonjs@^16.0.0":
582573
version "16.0.0"
@@ -3462,10 +3453,10 @@ [email protected]:
34623453
resolved "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz"
34633454
integrity sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==
34643455

3465-
murmurhash@0.0.2:
3466-
version "0.0.2"
3467-
resolved "https://registry.npmjs.org/murmurhash/-/murmurhash-0.0.2.tgz"
3468-
integrity sha512-LKlwdZKWzvCQpMszb2HO5leJ7P9T4m5XuDKku8bM0uElrzqK9cn0+iozwQS8jO4SNjrp4w7olalgd8WgsIjhWA==
3456+
murmurhash@^2.0.1:
3457+
version "2.0.1"
3458+
resolved "https://registry.yarnpkg.com/murmurhash/-/murmurhash-2.0.1.tgz#4097720e08cf978872194ad84ea5be2dec9b610f"
3459+
integrity sha512-5vQEh3y+DG/lMPM0mCGPDnyV8chYg/g7rl6v3Gd8WMF9S429ox3Xk8qrk174kWhG767KQMqqxLD1WnGd77hiew==
34693460

34703461
34713462
version "0.0.8"
@@ -4757,9 +4748,9 @@ uuid@^3.3.2:
47574748
resolved "https://registry.npmjs.org/uuid/-/uuid-3.4.0.tgz"
47584749
integrity sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==
47594750

4760-
uuid@^8.3.0:
4751+
uuid@^8.3.0, uuid@^8.3.2:
47614752
version "8.3.2"
4762-
resolved "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz"
4753+
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2"
47634754
integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==
47644755

47654756
v8-compile-cache@^2.0.3:

0 commit comments

Comments
 (0)