Skip to content

Commit b09f217

Browse files
committed
Update user context unit tests
1 parent a0cb796 commit b09f217

File tree

2 files changed

+210
-186
lines changed

2 files changed

+210
-186
lines changed

packages/optimizely-sdk/lib/optimizely/index.tests.js

Lines changed: 177 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import * as logging from '@optimizely/js-sdk-logging';
2121

2222
import Optimizely from './';
2323
import OptimizelyUserContext from '../optimizely_user_context';
24+
import { OptimizelyDecideOptions } from '../shared_types';
2425
import AudienceEvaluator from '../core/audience_evaluator';
2526
import bluebird from 'bluebird';
2627
import bucketer from '../core/bucketer';
@@ -4485,7 +4486,7 @@ describe('lib/optimizely', function() {
44854486

44864487
it('should make a decision for feature_test and dispatch an event', function() {
44874488
var flagKey = 'feature_2';
4488-
var variablesExpected = optlyInstance.getAllFeatureVariables(flagKey, userId);
4489+
var expectedVariables = optlyInstance.getAllFeatureVariables(flagKey, userId);
44894490
var user = new OptimizelyUserContext({
44904491
optimizely: optlyInstance,
44914492
userId,
@@ -4494,7 +4495,7 @@ describe('lib/optimizely', function() {
44944495
var expectedDecision = {
44954496
variationKey: 'variation_with_traffic',
44964497
enabled: true,
4497-
variables: variablesExpected,
4498+
variables: expectedVariables,
44984499
ruleKey: 'exp_no_audience',
44994500
flagKey: flagKey,
45004501
userContext: user,
@@ -4558,9 +4559,30 @@ describe('lib/optimizely', function() {
45584559
assert.deepEqual(callArgs[0], expectedImpressionEvent);
45594560
});
45604561

4562+
it('should make a decision for feature_test and do not dispatch an event with DISABLE_DECISION_EVENT passed in decide options ', function() {
4563+
var flagKey = 'feature_2';
4564+
var expectedVariables = optlyInstance.getAllFeatureVariables(flagKey, userId);
4565+
var user = new OptimizelyUserContext({
4566+
optimizely: optlyInstance,
4567+
userId,
4568+
});
4569+
var decision = optlyInstance.decide(user, flagKey, [ OptimizelyDecideOptions.DISABLE_DECISION_EVENT ]);
4570+
var expectedDecision = {
4571+
variationKey: 'variation_with_traffic',
4572+
enabled: true,
4573+
variables: expectedVariables,
4574+
ruleKey: 'exp_no_audience',
4575+
flagKey: flagKey,
4576+
userContext: user,
4577+
reasons: [],
4578+
}
4579+
assert.deepEqual(decision, expectedDecision);
4580+
sinon.assert.notCalled(optlyInstance.eventDispatcher.dispatchEvent);
4581+
});
4582+
45614583
it('should make a decision for rollout and dispatch an event when sendFlagDecisions is set to true', function() {
45624584
var flagKey = 'feature_1';
4563-
var variablesExpected = optlyInstance.getAllFeatureVariables(flagKey, userId);
4585+
var expectedVariables = optlyInstance.getAllFeatureVariables(flagKey, userId);
45644586
var user = new OptimizelyUserContext({
45654587
optimizely: optlyInstance,
45664588
userId,
@@ -4569,7 +4591,7 @@ describe('lib/optimizely', function() {
45694591
var expectedDecision = {
45704592
variationKey: '18257766532',
45714593
enabled: true,
4572-
variables: variablesExpected,
4594+
variables: expectedVariables,
45734595
ruleKey: '18322080788',
45744596
flagKey: flagKey,
45754597
userContext: user,
@@ -4638,7 +4660,7 @@ describe('lib/optimizely', function() {
46384660
newConfig.sendFlagDecisions = false;
46394661
optlyInstance.projectConfigManager.getConfig.returns(newConfig);
46404662
var flagKey = 'feature_1';
4641-
var variablesExpected = optlyInstance.getAllFeatureVariables(flagKey, userId);
4663+
var expectedVariables = optlyInstance.getAllFeatureVariables(flagKey, userId);
46424664
var user = new OptimizelyUserContext({
46434665
optimizely: optlyInstance,
46444666
userId,
@@ -4647,7 +4669,7 @@ describe('lib/optimizely', function() {
46474669
var expectedDecision = {
46484670
variationKey: '18257766532',
46494671
enabled: true,
4650-
variables: variablesExpected,
4672+
variables: expectedVariables,
46514673
ruleKey: '18322080788',
46524674
flagKey: flagKey,
46534675
userContext: user,
@@ -4659,7 +4681,7 @@ describe('lib/optimizely', function() {
46594681

46604682
it('should make a decision when variation is null and dispatch an event', function() {
46614683
var flagKey = 'feature_3';
4662-
var variablesExpected = optlyInstance.getAllFeatureVariables(flagKey, userId);
4684+
var expectedVariables = optlyInstance.getAllFeatureVariables(flagKey, userId);
46634685
var user = new OptimizelyUserContext({
46644686
optimizely: optlyInstance,
46654687
userId,
@@ -4668,7 +4690,7 @@ describe('lib/optimizely', function() {
46684690
var expectedDecision = {
46694691
variationKey: '',
46704692
enabled: false,
4671-
variables: variablesExpected,
4693+
variables: expectedVariables,
46724694
ruleKey: '',
46734695
flagKey: flagKey,
46744696
userContext: user,
@@ -4732,6 +4754,153 @@ describe('lib/optimizely', function() {
47324754
assert.deepEqual(callArgs[0], expectedImpressionEvent);
47334755
});
47344756
});
4757+
4758+
describe('with EXCLUDE_VARIABLES flag in default decide options', function() {
4759+
beforeEach(function() {
4760+
optlyInstance = new Optimizely({
4761+
clientEngine: 'node-sdk',
4762+
datafile: testData.getTestDecideProjectConfig(),
4763+
errorHandler: errorHandler,
4764+
eventDispatcher: eventDispatcher,
4765+
jsonSchemaValidator: jsonSchemaValidator,
4766+
logger: createdLogger,
4767+
isValidInstance: true,
4768+
eventBatchSize: 1,
4769+
defaultDecideOptions: [ OptimizelyDecideOptions.EXCLUDE_VARIABLES ],
4770+
});
4771+
4772+
sinon.stub(errorHandler, 'handleError');
4773+
sinon.stub(createdLogger, 'log');
4774+
sinon.stub(fns, 'uuid').returns('a68cf1ad-0393-4e18-af87-efe8f01a7c9c');
4775+
});
4776+
4777+
afterEach(function() {
4778+
errorHandler.handleError.restore();
4779+
createdLogger.log.restore();
4780+
fns.uuid.restore();
4781+
});
4782+
4783+
it('should exclude variables in decision object and dispatch an event', function() {
4784+
var flagKey = 'feature_2';
4785+
var user = new OptimizelyUserContext({
4786+
optimizely: optlyInstance,
4787+
userId
4788+
});
4789+
var decision = optlyInstance.decide(user, flagKey);
4790+
var expectedDecisionObj = {
4791+
variationKey: 'variation_with_traffic',
4792+
enabled: true,
4793+
variables: {},
4794+
ruleKey: 'exp_no_audience',
4795+
flagKey: flagKey,
4796+
userContext: user,
4797+
reasons: [],
4798+
}
4799+
assert.deepEqual(decision, expectedDecisionObj);
4800+
sinon.assert.calledOnce(optlyInstance.eventDispatcher.dispatchEvent);
4801+
var expectedImpressionEvent = {
4802+
httpVerb: 'POST',
4803+
url: 'https://logx.optimizely.com/v1/events',
4804+
params: {
4805+
account_id: '10367498574',
4806+
project_id: '10431130345',
4807+
visitors: [
4808+
{
4809+
snapshots: [
4810+
{
4811+
decisions: [
4812+
{
4813+
campaign_id: '10417730432',
4814+
experiment_id: '10420810910',
4815+
variation_id: '10418551353',
4816+
metadata: {
4817+
flag_key: 'feature_2',
4818+
rule_key: 'exp_no_audience',
4819+
rule_type: 'feature-test',
4820+
variation_key: 'variation_with_traffic',
4821+
enabled: true,
4822+
},
4823+
},
4824+
],
4825+
events: [
4826+
{
4827+
entity_id: '10417730432',
4828+
timestamp: Math.round(new Date().getTime()),
4829+
key: 'campaign_activated',
4830+
uuid: 'a68cf1ad-0393-4e18-af87-efe8f01a7c9c',
4831+
},
4832+
],
4833+
},
4834+
],
4835+
visitor_id: 'tester',
4836+
attributes: [
4837+
{
4838+
entity_id: '$opt_bot_filtering',
4839+
key: '$opt_bot_filtering',
4840+
type: 'custom',
4841+
value: true,
4842+
},
4843+
],
4844+
},
4845+
],
4846+
revision: '241',
4847+
client_name: 'node-sdk',
4848+
client_version: enums.NODE_CLIENT_VERSION,
4849+
anonymize_ip: true,
4850+
enrich_decisions: true,
4851+
},
4852+
};
4853+
var callArgs = eventDispatcher.dispatchEvent.getCalls()[0].args;
4854+
assert.deepEqual(callArgs[0], expectedImpressionEvent);
4855+
});
4856+
});
4857+
4858+
describe('with DISABLE_DECISION_EVENT flag in default decide options', function() {
4859+
beforeEach(function() {
4860+
optlyInstance = new Optimizely({
4861+
clientEngine: 'node-sdk',
4862+
datafile: testData.getTestDecideProjectConfig(),
4863+
errorHandler: errorHandler,
4864+
eventDispatcher: eventDispatcher,
4865+
jsonSchemaValidator: jsonSchemaValidator,
4866+
logger: createdLogger,
4867+
isValidInstance: true,
4868+
eventBatchSize: 1,
4869+
defaultDecideOptions: [ OptimizelyDecideOptions.DISABLE_DECISION_EVENT ],
4870+
});
4871+
4872+
sinon.stub(errorHandler, 'handleError');
4873+
sinon.stub(createdLogger, 'log');
4874+
sinon.stub(fns, 'uuid').returns('a68cf1ad-0393-4e18-af87-efe8f01a7c9c');
4875+
});
4876+
4877+
afterEach(function() {
4878+
errorHandler.handleError.restore();
4879+
createdLogger.log.restore();
4880+
fns.uuid.restore();
4881+
});
4882+
4883+
it('should make a decision and do not dispatch an event', function() {
4884+
var flagKey = 'feature_2';
4885+
var expectedVariables= optlyInstance.getAllFeatureVariables(flagKey, userId);
4886+
var user = new OptimizelyUserContext({
4887+
optimizely: optlyInstance,
4888+
userId
4889+
});
4890+
var decision = optlyInstance.decide(user, flagKey);
4891+
var expectedDecisionObj = {
4892+
variationKey: expectedVariables,
4893+
enabled: true,
4894+
variables: {},
4895+
ruleKey: 'exp_no_audience',
4896+
flagKey: flagKey,
4897+
userContext: user,
4898+
reasons: [],
4899+
}
4900+
assert.deepEqual(decision, expectedDecisionObj);
4901+
sinon.assert.notCalled(optlyInstance.eventDispatcher.dispatchEvent);
4902+
});
4903+
});
47354904
});
47364905
});
47374906

0 commit comments

Comments
 (0)