Skip to content

Commit 1511373

Browse files
authored
refactor(client): In getFeatureVariables, use OptimizelyConfig instead of internal ProjectConfig (#75)
Summary: getFeatureVariables reaches inside the optimizely-sdk client to access its project config manager, to find what variables are associated with a given feature key. In recent versions of optimizely-sdk, it is no longer necessary to access project config manager to get this information. Features and variables are available via the public, documented OptimizelyConfig interface. This PR updates getFeatureVariables to use OptimizelyConfig. Test Plan: Updated unit tests Manual testing
1 parent 6724355 commit 1511373

File tree

2 files changed

+59
-71
lines changed

2 files changed

+59
-71
lines changed

src/client.spec.ts

Lines changed: 53 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -460,58 +460,69 @@ describe('ReactSDKClient', () => {
460460

461461
describe('getFeatureVariables', () => {
462462
it('returns an empty object when the inner SDK returns no variables', () => {
463-
const anyClient = mockInnerClient as any;
464-
anyClient.getFeatureVariableBoolean.mockReturnValue(null);
465-
anyClient.getFeatureVariableString.mockReturnValue(null);
466-
anyClient.getFeatureVariableInteger.mockReturnValue(null);
467-
anyClient.getFeatureVariableDouble.mockReturnValue(null);
468-
anyClient.getFeatureVariableJSON.mockReturnValue(null);
463+
(mockInnerClient.getFeatureVariable as jest.Mock).mockReturnValue(null);
469464
const instance = createInstance(config);
470465
const result = instance.getFeatureVariables('feat1');
471466
expect(result).toEqual({});
472467
});
473468

474469
it('returns an object with variables of all types returned from the inner sdk ', () => {
475-
const anyClient = mockInnerClient as any;
476-
anyClient.projectConfigManager = {
477-
getConfig() {
478-
return {
479-
featureKeyMap: {
480-
feat1: {
481-
variables: [
482-
{
483-
type: 'boolean',
484-
key: 'bvar',
485-
},
486-
{
487-
type: 'string',
488-
key: 'svar',
489-
},
490-
{
491-
type: 'integer',
492-
key: 'ivar',
493-
},
494-
{
495-
type: 'double',
496-
key: 'dvar',
497-
},
498-
{
499-
type: 'json',
500-
key: 'jvar',
501-
},
502-
],
470+
(mockInnerClient.getOptimizelyConfig as jest.Mock).mockReturnValue({
471+
featuresMap: {
472+
feat1: {
473+
variablesMap: {
474+
bvar: {
475+
id: '0',
476+
key: 'bvar',
477+
type: 'boolean',
478+
value: 'false',
479+
},
480+
svar: {
481+
id: '1',
482+
key: 'svar',
483+
type: 'string',
484+
value: '',
485+
},
486+
ivar: {
487+
id: '2',
488+
key: 'ivar',
489+
type: 'integer',
490+
value: '0',
491+
},
492+
dvar: {
493+
id: '3',
494+
key: 'dvar',
495+
type: 'double',
496+
value: '0',
497+
},
498+
jvar: {
499+
id: '4',
500+
key: 'jvar',
501+
type: 'json',
502+
value: '{}',
503503
},
504504
},
505-
};
505+
},
506506
},
507-
};
508-
anyClient.getFeatureVariableBoolean.mockReturnValue(true);
509-
anyClient.getFeatureVariableString.mockReturnValue('whatsup');
510-
anyClient.getFeatureVariableInteger.mockReturnValue(10);
511-
anyClient.getFeatureVariableDouble.mockReturnValue(-10.5);
512-
anyClient.getFeatureVariableJSON.mockReturnValue({
513-
value: 'json value',
514507
});
508+
(mockInnerClient.getFeatureVariable as jest.Mock).mockImplementation(
509+
(featureKey: string, variableKey: string) => {
510+
switch (variableKey) {
511+
case 'bvar':
512+
return true;
513+
case 'svar':
514+
return 'whatsup';
515+
case 'ivar':
516+
return 10;
517+
case 'dvar':
518+
return -10.5;
519+
case 'jvar':
520+
return { value: 'json value' };
521+
default:
522+
return null;
523+
}
524+
}
525+
);
515526
const instance = createInstance(config);
516527
instance.setUser({
517528
id: 'user1123',

src/client.ts

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -360,40 +360,17 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
360360
}
361361
const userAttributes = user.attributes;
362362
const variableObj: { [key: string]: any } = {};
363-
const config = (this._client as any).projectConfigManager.getConfig();
364-
if (!config) {
363+
const optlyConfig = this._client.getOptimizelyConfig();
364+
if (!optlyConfig) {
365365
return {};
366366
}
367-
const feature = config.featureKeyMap[featureKey];
367+
const feature = optlyConfig.featuresMap[featureKey];
368368
if (!feature) {
369369
return {};
370370
}
371-
const variables: object[] = feature.variables;
372-
variables.forEach((variableDef: any) => {
373-
const type: any = variableDef.type;
374-
const key: any = variableDef.key;
375-
376-
switch (type) {
377-
case 'string':
378-
variableObj[key] = this._client.getFeatureVariableString(featureKey, key, userId, userAttributes);
379-
break;
380-
381-
case 'boolean':
382-
variableObj[key] = this._client.getFeatureVariableBoolean(featureKey, key, userId, userAttributes);
383-
break;
384-
385-
case 'integer':
386-
variableObj[key] = this._client.getFeatureVariableInteger(featureKey, key, userId, userAttributes);
387-
break;
388-
389-
case 'double':
390-
variableObj[key] = this._client.getFeatureVariableDouble(featureKey, key, userId, userAttributes);
391-
break;
392-
393-
case 'json':
394-
variableObj[key] = this._client.getFeatureVariableJSON(featureKey, key, userId, userAttributes);
395-
break;
396-
}
371+
Object.keys(feature.variablesMap).forEach(key => {
372+
const variable = feature.variablesMap[key];
373+
variableObj[variable.key] = this._client.getFeatureVariable(featureKey, variable.key, userId, userAttributes);
397374
});
398375

399376
return variableObj;

0 commit comments

Comments
 (0)