Skip to content

feat(ForcedDecisions): Add forced-decisions APIs to ReactSDKClient and update useDecision hook to reflect changes #133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"dependencies": {
"@optimizely/js-sdk-logging": "^0.1.0",
"@optimizely/optimizely-sdk": "^4.7.0",
"@optimizely/optimizely-sdk": "^4.9.1",
"hoist-non-react-statics": "^3.3.0",
"prop-types": "^15.6.2",
"utility-types": "^2.1.0 || ^3.0.0"
Expand Down
259 changes: 240 additions & 19 deletions src/client.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2019-2020, Optimizely
* Copyright 2019-2022, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,6 +33,9 @@ describe('ReactSDKClient', () => {
decide: jest.fn(),
decideAll: jest.fn(),
decideForKeys: jest.fn(),
setForcedDecision: jest.fn(),
removeForcedDecision: jest.fn(),
removeAllForcedDecisions: jest.fn(),
} as any;

mockInnerClient = {
Expand Down Expand Up @@ -526,19 +529,19 @@ describe('ReactSDKClient', () => {
const mockFn = mockOptimizelyUserContext.decideAll as jest.Mock;
const mockCreateUserContext = mockInnerClient.createUserContext as jest.Mock;
mockFn.mockReturnValue({
'theFlag1': {
theFlag1: {
enabled: true,
flagKey: 'theFlag1',
reasons: [],
ruleKey: '',
userContext: mockOptimizelyUserContext,
variables: {},
variationKey: 'varition1',
}
},
});
let result = instance.decideAll();
expect(result).toEqual({
'theFlag1': {
theFlag1: {
enabled: true,
flagKey: 'theFlag1',
reasons: [],
Expand All @@ -549,26 +552,26 @@ describe('ReactSDKClient', () => {
},
variables: {},
variationKey: 'varition1',
}
},
});
expect(mockFn).toBeCalledTimes(1);
expect(mockFn).toBeCalledWith([]);
expect(mockCreateUserContext).toBeCalledWith('user1', { foo: 'bar' });
expect(mockCreateUserContext).toBeCalledWith('user1', { foo: 'bar' });
mockFn.mockReset();
mockFn.mockReturnValue({
'theFlag2': {
theFlag2: {
enabled: true,
flagKey: 'theFlag2',
reasons: [],
ruleKey: '',
userContext: mockOptimizelyUserContext,
variables: {},
variationKey: 'varition2',
}
},
});
result = instance.decideAll([optimizely.OptimizelyDecideOption.INCLUDE_REASONS], 'user2', { bar: 'baz' });
expect(result).toEqual({
'theFlag2': {
theFlag2: {
enabled: true,
flagKey: 'theFlag2',
reasons: [],
Expand All @@ -579,7 +582,7 @@ describe('ReactSDKClient', () => {
},
variables: {},
variationKey: 'varition2',
}
},
});
expect(mockFn).toBeCalledTimes(1);
expect(mockFn).toBeCalledWith([optimizely.OptimizelyDecideOption.INCLUDE_REASONS]);
Expand All @@ -590,19 +593,19 @@ describe('ReactSDKClient', () => {
const mockFn = mockOptimizelyUserContext.decideForKeys as jest.Mock;
const mockCreateUserContext = mockInnerClient.createUserContext as jest.Mock;
mockFn.mockReturnValue({
'theFlag1': {
theFlag1: {
enabled: true,
flagKey: 'theFlag1',
reasons: [],
ruleKey: '',
userContext: mockOptimizelyUserContext,
variables: {},
variationKey: 'varition1',
}
},
});
let result = instance.decideForKeys(['theFlag1']);
expect(result).toEqual({
'theFlag1': {
theFlag1: {
enabled: true,
flagKey: 'theFlag1',
reasons: [],
Expand All @@ -613,26 +616,28 @@ describe('ReactSDKClient', () => {
},
variables: {},
variationKey: 'varition1',
}
},
});
expect(mockFn).toBeCalledTimes(1);
expect(mockFn).toBeCalledWith(['theFlag1'], []);
expect(mockCreateUserContext).toBeCalledWith('user1', { foo: 'bar' });
mockFn.mockReset();
mockFn.mockReturnValue({
'theFlag2': {
theFlag2: {
enabled: true,
flagKey: 'theFlag2',
reasons: [],
ruleKey: '',
userContext: mockOptimizelyUserContext,
variables: {},
variationKey: 'varition2',
}
},
});
result = instance.decideForKeys(['theFlag1'], [optimizely.OptimizelyDecideOption.INCLUDE_REASONS], 'user2', {
bar: 'baz',
});
result = instance.decideForKeys(['theFlag1'], [optimizely.OptimizelyDecideOption.INCLUDE_REASONS], 'user2', { bar: 'baz' });
expect(result).toEqual({
'theFlag2': {
theFlag2: {
enabled: true,
flagKey: 'theFlag2',
reasons: [],
Expand All @@ -643,7 +648,7 @@ describe('ReactSDKClient', () => {
},
variables: {},
variationKey: 'varition2',
}
},
});
expect(mockFn).toBeCalledTimes(1);
expect(mockFn).toBeCalledWith(['theFlag1'], [optimizely.OptimizelyDecideOption.INCLUDE_REASONS]);
Expand Down Expand Up @@ -856,4 +861,220 @@ describe('ReactSDKClient', () => {
expect(handler).not.toBeCalled();
});
});

describe('removeAllForcedDecisions', () => {
let instance: ReactSDKClient;
beforeEach(() => {
instance = createInstance(config);
});

it('should return false if no user context has been set ', () => {
const mockFn = mockOptimizelyUserContext.removeAllForcedDecisions as jest.Mock;

mockFn.mockReturnValue(false);

const result = instance.removeAllForcedDecisions();
expect(result).toBeDefined();
expect(result).toEqual(false);
});

it('should return true if user context has been set ', () => {
instance.setUser({
id: 'user1',
});
const mockFn = mockOptimizelyUserContext.removeAllForcedDecisions as jest.Mock;

mockFn.mockReturnValue(true);

const result = instance.removeAllForcedDecisions();
expect(mockFn).toBeCalledTimes(1);
expect(result).toBeDefined();
expect(result).toEqual(true);
});
});

describe('setForcedDecision', () => {
let instance: ReactSDKClient;
beforeEach(() => {
instance = createInstance(config);
instance.setUser({
id: 'user1',
attributes: {
foo: 'bar',
},
});
});

it('should overwrite decide when forcedDecision is envoked', () => {
const mockFn = mockOptimizelyUserContext.decide as jest.Mock;
mockFn.mockReturnValue({
enabled: true,
flagKey: 'theFlag1',
reasons: [],
ruleKey: '',
userContext: mockOptimizelyUserContext,
variables: {},
variationKey: 'varition1',
});
const result = instance.decide('theFlag1');
expect(result).toEqual({
enabled: true,
flagKey: 'theFlag1',
reasons: [],
ruleKey: '',
userContext: {
id: 'user1',
attributes: { foo: 'bar' },
},
variables: {},
variationKey: 'varition1',
});
expect(mockFn).toBeCalledTimes(1);
expect(mockFn).toBeCalledWith('theFlag1', []);

const mockFnForcedDecision = mockOptimizelyUserContext.setForcedDecision as jest.Mock;
mockFnForcedDecision.mockReturnValue(true);
instance.setForcedDecision(
{
flagKey: 'theFlag1',
ruleKey: 'experiment',
},
{ variationKey: 'varition2' }
);

expect(mockFnForcedDecision).toBeCalledTimes(1);

mockFn.mockReset();
mockFn.mockReturnValue({
enabled: true,
flagKey: 'theFlag1',
reasons: [],
ruleKey: '',
userContext: mockOptimizelyUserContext,
variables: {},
variationKey: 'varition2',
});
const result2 = instance.decide('theFlag1', []);

expect(mockFn).toBeCalledTimes(1);
expect(mockFn).toBeCalledWith('theFlag1', []);
expect(result2).toEqual({
enabled: true,
flagKey: 'theFlag1',
reasons: [],
ruleKey: '',
userContext: { id: 'user1', attributes: { foo: 'bar' } },
variables: {},
variationKey: 'varition2',
});
});
});

describe('removeForcedDecision', () => {
let instance: ReactSDKClient;
beforeEach(() => {
instance = createInstance(config);
instance.setUser({
id: 'user1',
attributes: {
foo: 'bar',
},
});
});

it('should revert back to the decide default value when removeForcedDecision is envoked after settingup the forced decision', () => {
const mockFn = mockOptimizelyUserContext.decide as jest.Mock;
mockFn.mockReturnValue({
enabled: true,
flagKey: 'theFlag1',
reasons: [],
ruleKey: '',
userContext: mockOptimizelyUserContext,
variables: {},
variationKey: 'varition1',
});
const result = instance.decide('theFlag1');
expect(result).toEqual({
enabled: true,
flagKey: 'theFlag1',
reasons: [],
ruleKey: '',
userContext: {
id: 'user1',
attributes: { foo: 'bar' },
},
variables: {},
variationKey: 'varition1',
});
expect(mockFn).toBeCalledTimes(1);
expect(mockFn).toBeCalledWith('theFlag1', []);

const mockFnForcedDecision = mockOptimizelyUserContext.setForcedDecision as jest.Mock;
mockFnForcedDecision.mockReturnValue(true);
instance.setForcedDecision(
{
flagKey: 'theFlag1',
ruleKey: 'experiment',
},
{ variationKey: 'varition2' }
);

expect(mockFnForcedDecision).toBeCalledTimes(1);

mockFn.mockReset();
mockFn.mockReturnValue({
enabled: true,
flagKey: 'theFlag1',
reasons: [],
ruleKey: '',
userContext: mockOptimizelyUserContext,
variables: {},
variationKey: 'varition2',
});
const result2 = instance.decide('theFlag1', []);

expect(mockFn).toBeCalledTimes(1);
expect(mockFn).toBeCalledWith('theFlag1', []);
expect(result2).toEqual({
enabled: true,
flagKey: 'theFlag1',
reasons: [],
ruleKey: '',
userContext: { id: 'user1', attributes: { foo: 'bar' } },
variables: {},
variationKey: 'varition2',
});

const mockFnRemoveForcedDecision = mockOptimizelyUserContext.removeForcedDecision as jest.Mock;
mockFnRemoveForcedDecision.mockReturnValue(true);
instance.removeForcedDecision({
flagKey: 'theFlag1',
ruleKey: 'experiment',
});

mockFn.mockReset();
mockFn.mockReturnValue({
enabled: true,
flagKey: 'theFlag1',
reasons: [],
ruleKey: '',
userContext: mockOptimizelyUserContext,
variables: {},
variationKey: 'varition1',
});
const result3 = instance.decide('theFlag1', []);

expect(mockFn).toBeCalledTimes(1);
expect(mockFn).toBeCalledWith('theFlag1', []);
expect(result3).toEqual({
enabled: true,
flagKey: 'theFlag1',
reasons: [],
ruleKey: '',
userContext: { id: 'user1', attributes: { foo: 'bar' } },
variables: {},
variationKey: 'varition1',
});
});
});
});
Loading