@@ -21,7 +21,8 @@ import { bucket } from '../bucketer';
21
21
import { getTestProjectConfig , getTestProjectConfigWithFeatures } from '../../tests/test_data' ;
22
22
import { createProjectConfig , ProjectConfig } from '../../project_config/project_config' ;
23
23
import { Experiment } from '../../shared_types' ;
24
- import { CONTROL_ATTRIBUTES } from '../../utils/enums' ;
24
+ import { CONTROL_ATTRIBUTES , DECISION_SOURCES } from '../../utils/enums' ;
25
+ import { getDecisionTestDatafile } from '../../tests/decision_test_datafile' ;
25
26
26
27
import {
27
28
USER_HAS_NO_FORCED_VARIATION ,
@@ -48,6 +49,7 @@ import {
48
49
} from '../decision_service/index' ;
49
50
50
51
import { BUCKETING_ID_NOT_STRING , USER_PROFILE_LOOKUP_ERROR , USER_PROFILE_SAVE_ERROR } from 'error_message' ;
52
+ import exp from 'constants' ;
51
53
52
54
type MockLogger = ReturnType < typeof getMockLogger > ;
53
55
@@ -685,7 +687,54 @@ describe('DecisionService', () => {
685
687
} ) ;
686
688
} ) ;
687
689
688
- // describe('getVariationForFeature', () => {
690
+ describe ( 'getVariationForFeature' , ( ) => {
691
+ it ( 'should return variation from the first experiment for which a variation is available' , ( ) => {
692
+ const { decisionService } = getDecisionService ( ) ;
693
+
694
+ const resolveVariationSpy = vi . spyOn ( decisionService as any , 'resolveVariation' )
695
+ . mockImplementation ( (
696
+ config ,
697
+ experiment : any ,
698
+ user ,
699
+ shouldIgnoreUPS ,
700
+ userProfileTracker
701
+ ) => {
702
+ if ( experiment . key === 'exp_2' ) {
703
+ return {
704
+ result : 'variation_2' ,
705
+ reasons : [ ] ,
706
+ } ;
707
+ }
708
+ return {
709
+ result : null ,
710
+ reasons : [ ] ,
711
+ }
712
+ } ) ;
713
+
714
+ const config = createProjectConfig ( getDecisionTestDatafile ( ) ) ;
715
+
716
+ const user = new OptimizelyUserContext ( {
717
+ optimizely : { } as any ,
718
+ userId : 'tester' ,
719
+ attributes : {
720
+ age : 40 ,
721
+ } ,
722
+ } ) ;
723
+
724
+ const feature = config . featureKeyMap [ 'flag_1' ] ;
725
+ const variation = decisionService . getVariationForFeature ( config , feature , user ) ;
726
+
727
+ expect ( variation . result ) . toEqual ( {
728
+ experiment : config . experimentKeyMap [ 'exp_2' ] ,
729
+ variation : config . variationIdMap [ '5002' ] ,
730
+ decisionSource : DECISION_SOURCES . FEATURE_TEST ,
731
+ } ) ;
689
732
690
- // });
733
+ expect ( resolveVariationSpy ) . toHaveBeenCalledTimes ( 2 ) ;
734
+ expect ( resolveVariationSpy ) . toHaveBeenNthCalledWith ( 1 ,
735
+ config , config . experimentKeyMap [ 'exp_1' ] , user , false , expect . anything ( ) ) ;
736
+ expect ( resolveVariationSpy ) . toHaveBeenNthCalledWith ( 2 ,
737
+ config , config . experimentKeyMap [ 'exp_2' ] , user , false , expect . anything ( ) ) ;
738
+ } ) ;
739
+ } ) ;
691
740
} ) ;
0 commit comments