Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 20 additions & 18 deletions src/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ describe('ReactSDKClient', () => {
getFeatureVariableBoolean: jest.fn(() => null),
getFeatureVariableDouble: jest.fn(() => null),
getFeatureVariableJSON: jest.fn(() => null),
getAllFeatureVariables: jest.fn(() => { return {} }),
getAllFeatureVariables: jest.fn(() => {
return {};
}),
getFeatureVariable: jest.fn(() => null),
getFeatureVariableInteger: jest.fn(() => null),
getFeatureVariableString: jest.fn(() => null),
Expand Down Expand Up @@ -508,7 +510,7 @@ describe('ReactSDKClient', () => {
anyClient.getFeatureVariableInteger.mockReturnValue(10);
anyClient.getFeatureVariableDouble.mockReturnValue(-10.5);
anyClient.getFeatureVariableJSON.mockReturnValue({
value: 'json value'
value: 'json value',
});
const instance = createInstance(config);
instance.setUser({
Expand All @@ -521,31 +523,31 @@ describe('ReactSDKClient', () => {
ivar: 10,
dvar: -10.5,
jvar: {
value: 'json value'
}
value: 'json value',
},
});
});
});

describe('getAllFeatureVariables', () => {
it('returns an empty object when the inner SDK returns no variables', () => {
const anyClient = mockInnerClient.getAllFeatureVariables as jest.Mock;
const anyClient = mockInnerClient.getAllFeatureVariables as jest.Mock;
anyClient.mockReturnValue({});
const instance = createInstance(config);
const result = instance.getAllFeatureVariables('feat1', 'user1');
expect(result).toEqual({});
});

it('returns an object with variables of all types returned from the inner sdk ', () => {
const anyClient = mockInnerClient.getAllFeatureVariables as jest.Mock;
const anyClient = mockInnerClient.getAllFeatureVariables as jest.Mock;
anyClient.mockReturnValue({
bvar: true,
svar: 'whatsup',
ivar: 10,
dvar: -10.5,
jvar: {
value: 'json value'
}
value: 'json value',
},
});
const instance = createInstance(config);
instance.setUser({
Expand All @@ -558,8 +560,8 @@ describe('ReactSDKClient', () => {
ivar: 10,
dvar: -10.5,
jvar: {
value: 'json value'
}
value: 'json value',
},
});
});

Expand All @@ -571,8 +573,8 @@ describe('ReactSDKClient', () => {
ivar: 10,
dvar: -10.5,
jvar: {
value: 'json value'
}
value: 'json value',
},
});
const instance = createInstance(config);
instance.setUser({
Expand All @@ -588,8 +590,8 @@ describe('ReactSDKClient', () => {
ivar: 10,
dvar: -10.5,
jvar: {
value: 'json value'
}
value: 'json value',
},
});
expect(mockFn).toBeCalledTimes(1);
expect(mockFn).toBeCalledWith('feat1', 'user1', {
Expand All @@ -602,8 +604,8 @@ describe('ReactSDKClient', () => {
ivar: 11,
dvar: -11.5,
jvar: {
value: 'json another value'
}
value: 'json another value',
},
});
result = instance.getAllFeatureVariables('feat1', 'user2', {
bar: 'baz',
Expand All @@ -614,8 +616,8 @@ describe('ReactSDKClient', () => {
ivar: 11,
dvar: -11.5,
jvar: {
value: 'json another value'
}
value: 'json another value',
},
});
expect(mockInnerClient.getAllFeatureVariables).toBeCalledTimes(1);
expect(mockInnerClient.getAllFeatureVariables).toBeCalledWith('feat1', 'user2', { bar: 'baz' });
Expand Down
44 changes: 15 additions & 29 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import * as optimizely from '@optimizely/optimizely-sdk';
import * as logging from '@optimizely/js-sdk-logging';
import { UserAttributes } from "@optimizely/optimizely-sdk";
import { UserAttributes } from '@optimizely/optimizely-sdk';

const logger = logging.getLogger('ReactSDK');

Expand Down Expand Up @@ -95,21 +95,21 @@ export interface ReactSDKClient extends optimizely.Client {
featureKey: string,
variableKey: string,
overrideUserId?: string,
overrideAttributes?: optimizely.UserAttributes,
): unknown
overrideAttributes?: optimizely.UserAttributes
): unknown;

getFeatureVariable(
featureKey: string,
variableKey: string,
overrideUserId: string,
overrideAttributes?: optimizely.UserAttributes
): unknown
): unknown;

getAllFeatureVariables(
featureKey: string,
overrideUserId: string,
overrideAttributes?: optimizely.UserAttributes
): { [variableKey: string]: unknown }
): { [variableKey: string]: unknown };

isFeatureEnabled(
featureKey: string,
Expand Down Expand Up @@ -296,7 +296,7 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
overrideUserId?: string | optimizely.EventTags,
overrideAttributes?: optimizely.UserAttributes,
eventTags?: optimizely.EventTags
) {
): void {
if (typeof overrideUserId !== 'undefined' && typeof overrideUserId !== 'string') {
eventTags = overrideUserId;
overrideUserId = undefined;
Expand Down Expand Up @@ -509,14 +509,9 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
): unknown {
const user = this.getUserContextWithOverrides(overrideUserId, overrideAttributes);
if (user.id === null) {
return null
return null;
}
return this._client.getFeatureVariableJSON(
feature,
variable,
user.id,
user.attributes,
);
return this._client.getFeatureVariableJSON(feature, variable, user.id, user.attributes);
}

/**
Expand All @@ -530,21 +525,16 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
* @memberof OptimizelyReactSDKClient
*/
getFeatureVariable(
featureKey: string,
variableKey: string,
overrideUserId: string,
overrideAttributes?: optimizely.UserAttributes
featureKey: string,
variableKey: string,
overrideUserId: string,
overrideAttributes?: optimizely.UserAttributes
): unknown {
const user = this.getUserContextWithOverrides(overrideUserId, overrideAttributes);
if (user.id === null) {
return null
return null;
}
return this._client.getFeatureVariable(
featureKey,
variableKey,
user.id,
user.attributes,
);
return this._client.getFeatureVariable(featureKey, variableKey, user.id, user.attributes);
}

/**
Expand All @@ -564,11 +554,7 @@ class OptimizelyReactSDKClient implements ReactSDKClient {
if (user.id === null) {
return {};
}
return this._client.getAllFeatureVariables(
featureKey,
user.id,
user.attributes,
);
return this._client.getAllFeatureVariables(featureKey, user.id, user.attributes);
}

/**
Expand Down