Skip to content

Commit 48c615d

Browse files
add unit tests for getFeatureVariable
1 parent dbb1f22 commit 48c615d

File tree

2 files changed

+867
-140
lines changed

2 files changed

+867
-140
lines changed

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

Lines changed: 104 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,99 @@ Optimizely.prototype.getEnabledFeatures = function(userId, attributes) {
647647
}
648648
};
649649

650+
/**
651+
* Returns value of the variable attached to the given feature flag.
652+
* Returns null if the feature key or variable key is invalid.
653+
*
654+
* @param {string} featureKey Key of the feature whose variable's value is
655+
* being accessed
656+
* @param {string} variableKey Key of the variable whose value is being
657+
* accessed
658+
* @param {string} userId ID for the user
659+
* @param {Object} attributes Optional user attributes
660+
* @return {*} Value of the variable cast to the appropriate
661+
* type, or null if the feature key is invalid or
662+
* the variable key is invalid.
663+
*/
664+
665+
Optimizely.prototype.getFeatureVariable = function(featureKey, variableKey, userId, attributes) {
666+
if (!this.__isValidInstance()) {
667+
var apiName = 'getFeatureVariable';
668+
this.logger.log(LOG_LEVEL.ERROR, sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, apiName));
669+
return null;
670+
}
671+
672+
if (!this.__validateInputs({ feature_key: featureKey, variable_key: variableKey, user_id: userId }, attributes)) {
673+
return null;
674+
}
675+
676+
var configObj = this.projectConfigManager.getConfig();
677+
if (!configObj) {
678+
return null;
679+
}
680+
681+
var featureFlag = projectConfig.getFeatureFromKey(configObj, featureKey, this.logger);
682+
if (!featureFlag) {
683+
return null;
684+
}
685+
686+
var variable = projectConfig.getVariableForFeature(configObj, featureKey, variableKey, this.logger);
687+
if (!variable) {
688+
return null;
689+
}
690+
691+
var featureEnabled = false;
692+
var variableValue = variable.defaultValue;
693+
var decision = this.decisionService.getVariationForFeature(configObj, featureFlag, userId, attributes);
694+
695+
if (decision.variation !== null) {
696+
featureEnabled = decision.variation.featureEnabled;
697+
var value = projectConfig.getVariableValueForVariation(configObj, variable, decision.variation, this.logger);
698+
if (value !== null) {
699+
if (featureEnabled === true) {
700+
variableValue = value;
701+
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.USER_RECEIVED_VARIABLE_VALUE, MODULE_NAME, variableKey, featureFlag.key, variableValue, userId));
702+
} else {
703+
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.FEATURE_NOT_ENABLED_RETURN_DEFAULT_VARIABLE_VALUE, MODULE_NAME,
704+
featureFlag.key, userId, variableKey));
705+
}
706+
} else {
707+
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.VARIABLE_NOT_USED_RETURN_DEFAULT_VARIABLE_VALUE, MODULE_NAME, variableKey, decision.variation.key));
708+
}
709+
} else {
710+
this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.USER_RECEIVED_DEFAULT_VARIABLE_VALUE, MODULE_NAME, userId,
711+
variableKey, featureFlag.key));
712+
}
713+
714+
var sourceInfo = {};
715+
if (decision.decisionSource === DECISION_SOURCES.FEATURE_TEST) {
716+
sourceInfo = {
717+
experimentKey: decision.experiment.key,
718+
variationKey: decision.variation.key,
719+
}
720+
}
721+
722+
var typeCastedValue = projectConfig.getTypeCastValue(variableValue, variable.type, this.logger);
723+
this.notificationCenter.sendNotifications(
724+
NOTIFICATION_TYPES.DECISION,
725+
{
726+
type: DECISION_NOTIFICATION_TYPES.FEATURE_VARIABLE,
727+
userId: userId,
728+
attributes: attributes || {},
729+
decisionInfo: {
730+
featureKey: featureKey,
731+
featureEnabled: featureEnabled,
732+
source: decision.decisionSource,
733+
variableKey: variableKey,
734+
variableValue: typeCastedValue,
735+
variableType: variable.type,
736+
sourceInfo: sourceInfo,
737+
}
738+
}
739+
);
740+
return typeCastedValue;
741+
};
742+
650743
/**
651744
* Helper method to get the value for a variable of a certain type attached to a
652745
* feature flag. Returns null if the feature key is invalid, the variable key is
@@ -667,11 +760,9 @@ Optimizely.prototype.getEnabledFeatures = function(userId, attributes) {
667760
* variable key is invalid, or there is a mismatch
668761
* with the type of the variable
669762
*/
670-
671-
Optimizely.prototype.getFeatureVariable = function(featureKey, variableKey, userId, attributes) {
763+
Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableKey, variableType, userId, attributes) {
672764
if (!this.__isValidInstance()) {
673-
// var apiName = 'getFeatureVariable' + variableType.charAt(0).toUpperCase() + variableType.slice(1);
674-
var apiName = 'getFeatureVariable';
765+
var apiName = 'getFeatureVariable' + variableType.charAt(0).toUpperCase() + variableType.slice(1);
675766
this.logger.log(LOG_LEVEL.ERROR, sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, apiName));
676767
return null;
677768
}
@@ -695,13 +786,13 @@ Optimizely.prototype.getFeatureVariable = function(featureKey, variableKey, user
695786
return null;
696787
}
697788

698-
// if (variable.type !== variableType) {
699-
// this.logger.log(
700-
// LOG_LEVEL.WARNING,
701-
// sprintf(LOG_MESSAGES.VARIABLE_REQUESTED_WITH_WRONG_TYPE, MODULE_NAME, variableType, variable.type)
702-
// );
703-
// return null;
704-
// }
789+
if (variable.type !== variableType) {
790+
this.logger.log(
791+
LOG_LEVEL.WARNING,
792+
sprintf(LOG_MESSAGES.VARIABLE_REQUESTED_WITH_WRONG_TYPE, MODULE_NAME, variableType, variable.type)
793+
);
794+
return null;
795+
}
705796

706797
var featureEnabled = false;
707798
var variableValue = variable.defaultValue;
@@ -734,7 +825,7 @@ Optimizely.prototype.getFeatureVariable = function(featureKey, variableKey, user
734825
}
735826
}
736827

737-
var typeCastedValue = projectConfig.getTypeCastValue(variableValue, variable.type, this.logger);
828+
var typeCastedValue = projectConfig.getTypeCastValue(variableValue, variableType, this.logger);
738829
this.notificationCenter.sendNotifications(
739830
NOTIFICATION_TYPES.DECISION,
740831
{
@@ -747,101 +838,14 @@ Optimizely.prototype.getFeatureVariable = function(featureKey, variableKey, user
747838
source: decision.decisionSource,
748839
variableKey: variableKey,
749840
variableValue: typeCastedValue,
750-
variableType: variable.type,
841+
variableType: variableType,
751842
sourceInfo: sourceInfo,
752843
}
753844
}
754845
);
755-
// return variableValue;
756846
return typeCastedValue;
757847
};
758848

759-
// Optimizely.prototype._getFeatureVariableForType = function(featureKey, variableKey, variableType, userId, attributes) {
760-
// if (!this.__isValidInstance()) {
761-
// var apiName = 'getFeatureVariable' + variableType.charAt(0).toUpperCase() + variableType.slice(1);
762-
// this.logger.log(LOG_LEVEL.ERROR, sprintf(LOG_MESSAGES.INVALID_OBJECT, MODULE_NAME, apiName));
763-
// return null;
764-
// }
765-
766-
// if (!this.__validateInputs({ feature_key: featureKey, variable_key: variableKey, user_id: userId }, attributes)) {
767-
// return null;
768-
// }
769-
770-
// var configObj = this.projectConfigManager.getConfig();
771-
// if (!configObj) {
772-
// return null;
773-
// }
774-
775-
// var featureFlag = projectConfig.getFeatureFromKey(configObj, featureKey, this.logger);
776-
// if (!featureFlag) {
777-
// return null;
778-
// }
779-
780-
// var variable = projectConfig.getVariableForFeature(configObj, featureKey, variableKey, this.logger);
781-
// if (!variable) {
782-
// return null;
783-
// }
784-
785-
// if (variable.type !== variableType) {
786-
// this.logger.log(
787-
// LOG_LEVEL.WARNING,
788-
// sprintf(LOG_MESSAGES.VARIABLE_REQUESTED_WITH_WRONG_TYPE, MODULE_NAME, variableType, variable.type)
789-
// );
790-
// return null;
791-
// }
792-
793-
// var featureEnabled = false;
794-
// var variableValue = variable.defaultValue;
795-
// var decision = this.decisionService.getVariationForFeature(configObj, featureFlag, userId, attributes);
796-
797-
// if (decision.variation !== null) {
798-
// featureEnabled = decision.variation.featureEnabled;
799-
// var value = projectConfig.getVariableValueForVariation(configObj, variable, decision.variation, this.logger);
800-
// if (value !== null) {
801-
// if (featureEnabled === true) {
802-
// variableValue = value;
803-
// this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.USER_RECEIVED_VARIABLE_VALUE, MODULE_NAME, variableKey, featureFlag.key, variableValue, userId));
804-
// } else {
805-
// this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.FEATURE_NOT_ENABLED_RETURN_DEFAULT_VARIABLE_VALUE, MODULE_NAME,
806-
// featureFlag.key, userId, variableKey));
807-
// }
808-
// } else {
809-
// this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.VARIABLE_NOT_USED_RETURN_DEFAULT_VARIABLE_VALUE, MODULE_NAME, variableKey, decision.variation.key));
810-
// }
811-
// } else {
812-
// this.logger.log(LOG_LEVEL.INFO, sprintf(LOG_MESSAGES.USER_RECEIVED_DEFAULT_VARIABLE_VALUE, MODULE_NAME, userId,
813-
// variableKey, featureFlag.key));
814-
// }
815-
816-
// var sourceInfo = {};
817-
// if (decision.decisionSource === DECISION_SOURCES.FEATURE_TEST) {
818-
// sourceInfo = {
819-
// experimentKey: decision.experiment.key,
820-
// variationKey: decision.variation.key,
821-
// }
822-
// }
823-
824-
// var typeCastedValue = projectConfig.getTypeCastValue(variableValue, variableType, this.logger);
825-
// this.notificationCenter.sendNotifications(
826-
// NOTIFICATION_TYPES.DECISION,
827-
// {
828-
// type: DECISION_NOTIFICATION_TYPES.FEATURE_VARIABLE,
829-
// userId: userId,
830-
// attributes: attributes || {},
831-
// decisionInfo: {
832-
// featureKey: featureKey,
833-
// featureEnabled: featureEnabled,
834-
// source: decision.decisionSource,
835-
// variableKey: variableKey,
836-
// variableValue: typeCastedValue,
837-
// variableType: variableType,
838-
// sourceInfo: sourceInfo,
839-
// }
840-
// }
841-
// );
842-
// return typeCastedValue;
843-
// };
844-
845849
/**
846850
* Returns value for the given boolean variable attached to the given feature
847851
* flag.

0 commit comments

Comments
 (0)