@@ -21,6 +21,7 @@ import * as logging from '@optimizely/js-sdk-logging';
21
21
22
22
import Optimizely from './' ;
23
23
import OptimizelyUserContext from '../optimizely_user_context' ;
24
+ import { OptimizelyDecideOptions } from '../shared_types' ;
24
25
import AudienceEvaluator from '../core/audience_evaluator' ;
25
26
import bluebird from 'bluebird' ;
26
27
import bucketer from '../core/bucketer' ;
@@ -4485,7 +4486,7 @@ describe('lib/optimizely', function() {
4485
4486
4486
4487
it ( 'should make a decision for feature_test and dispatch an event' , function ( ) {
4487
4488
var flagKey = 'feature_2' ;
4488
- var variablesExpected = optlyInstance . getAllFeatureVariables ( flagKey , userId ) ;
4489
+ var expectedVariables = optlyInstance . getAllFeatureVariables ( flagKey , userId ) ;
4489
4490
var user = new OptimizelyUserContext ( {
4490
4491
optimizely : optlyInstance ,
4491
4492
userId,
@@ -4494,7 +4495,7 @@ describe('lib/optimizely', function() {
4494
4495
var expectedDecision = {
4495
4496
variationKey : 'variation_with_traffic' ,
4496
4497
enabled : true ,
4497
- variables : variablesExpected ,
4498
+ variables : expectedVariables ,
4498
4499
ruleKey : 'exp_no_audience' ,
4499
4500
flagKey : flagKey ,
4500
4501
userContext : user ,
@@ -4558,9 +4559,30 @@ describe('lib/optimizely', function() {
4558
4559
assert . deepEqual ( callArgs [ 0 ] , expectedImpressionEvent ) ;
4559
4560
} ) ;
4560
4561
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
+
4561
4583
it ( 'should make a decision for rollout and dispatch an event when sendFlagDecisions is set to true' , function ( ) {
4562
4584
var flagKey = 'feature_1' ;
4563
- var variablesExpected = optlyInstance . getAllFeatureVariables ( flagKey , userId ) ;
4585
+ var expectedVariables = optlyInstance . getAllFeatureVariables ( flagKey , userId ) ;
4564
4586
var user = new OptimizelyUserContext ( {
4565
4587
optimizely : optlyInstance ,
4566
4588
userId,
@@ -4569,7 +4591,7 @@ describe('lib/optimizely', function() {
4569
4591
var expectedDecision = {
4570
4592
variationKey : '18257766532' ,
4571
4593
enabled : true ,
4572
- variables : variablesExpected ,
4594
+ variables : expectedVariables ,
4573
4595
ruleKey : '18322080788' ,
4574
4596
flagKey : flagKey ,
4575
4597
userContext : user ,
@@ -4638,7 +4660,7 @@ describe('lib/optimizely', function() {
4638
4660
newConfig . sendFlagDecisions = false ;
4639
4661
optlyInstance . projectConfigManager . getConfig . returns ( newConfig ) ;
4640
4662
var flagKey = 'feature_1' ;
4641
- var variablesExpected = optlyInstance . getAllFeatureVariables ( flagKey , userId ) ;
4663
+ var expectedVariables = optlyInstance . getAllFeatureVariables ( flagKey , userId ) ;
4642
4664
var user = new OptimizelyUserContext ( {
4643
4665
optimizely : optlyInstance ,
4644
4666
userId,
@@ -4647,7 +4669,7 @@ describe('lib/optimizely', function() {
4647
4669
var expectedDecision = {
4648
4670
variationKey : '18257766532' ,
4649
4671
enabled : true ,
4650
- variables : variablesExpected ,
4672
+ variables : expectedVariables ,
4651
4673
ruleKey : '18322080788' ,
4652
4674
flagKey : flagKey ,
4653
4675
userContext : user ,
@@ -4659,7 +4681,7 @@ describe('lib/optimizely', function() {
4659
4681
4660
4682
it ( 'should make a decision when variation is null and dispatch an event' , function ( ) {
4661
4683
var flagKey = 'feature_3' ;
4662
- var variablesExpected = optlyInstance . getAllFeatureVariables ( flagKey , userId ) ;
4684
+ var expectedVariables = optlyInstance . getAllFeatureVariables ( flagKey , userId ) ;
4663
4685
var user = new OptimizelyUserContext ( {
4664
4686
optimizely : optlyInstance ,
4665
4687
userId,
@@ -4668,7 +4690,7 @@ describe('lib/optimizely', function() {
4668
4690
var expectedDecision = {
4669
4691
variationKey : '' ,
4670
4692
enabled : false ,
4671
- variables : variablesExpected ,
4693
+ variables : expectedVariables ,
4672
4694
ruleKey : '' ,
4673
4695
flagKey : flagKey ,
4674
4696
userContext : user ,
@@ -4732,6 +4754,153 @@ describe('lib/optimizely', function() {
4732
4754
assert . deepEqual ( callArgs [ 0 ] , expectedImpressionEvent ) ;
4733
4755
} ) ;
4734
4756
} ) ;
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
+ } ) ;
4735
4904
} ) ;
4736
4905
} ) ;
4737
4906
0 commit comments