Skip to content

Commit 7e96368

Browse files
committed
feature test 1
1 parent a15a0a0 commit 7e96368

File tree

3 files changed

+461
-4
lines changed

3 files changed

+461
-4
lines changed

lib/core/decision_service/index.spec.ts

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import { bucket } from '../bucketer';
2121
import { getTestProjectConfig, getTestProjectConfigWithFeatures } from '../../tests/test_data';
2222
import { createProjectConfig, ProjectConfig } from '../../project_config/project_config';
2323
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';
2526

2627
import {
2728
USER_HAS_NO_FORCED_VARIATION,
@@ -48,6 +49,7 @@ import {
4849
} from '../decision_service/index';
4950

5051
import { BUCKETING_ID_NOT_STRING, USER_PROFILE_LOOKUP_ERROR, USER_PROFILE_SAVE_ERROR } from 'error_message';
52+
import exp from 'constants';
5153

5254
type MockLogger = ReturnType<typeof getMockLogger>;
5355

@@ -685,7 +687,54 @@ describe('DecisionService', () => {
685687
});
686688
});
687689

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+
});
689732

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+
});
691740
});

lib/core/decision_service/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ export class DecisionService {
615615
isProfileUpdated: false,
616616
userProfile: null,
617617
}
618-
const shouldIgnoreUPS = options[OptimizelyDecideOption.IGNORE_USER_PROFILE_SERVICE];
618+
const shouldIgnoreUPS = !!options[OptimizelyDecideOption.IGNORE_USER_PROFILE_SERVICE];
619619

620620
if(!shouldIgnoreUPS) {
621621
userProfileTracker.userProfile = this.resolveExperimentBucketMap(userId, attributes);

0 commit comments

Comments
 (0)