|
| 1 | +/** |
| 2 | + * Copyright 2019, Optimizely |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +var assert = require('chai').assert; |
| 17 | + |
| 18 | +var datafile = require('../../tests/test_data').getTestProjectConfigWithFeatures(); |
| 19 | +var projectConfig = require('../project_config'); |
| 20 | +var optimizelyConfig = require('./index'); |
| 21 | + |
| 22 | +var getAllExperimentsFromDatafile = function(datafile) { |
| 23 | + var allExperiments = []; |
| 24 | + datafile.groups.forEach(function(group) { |
| 25 | + group.experiments.forEach(function(experiment) { |
| 26 | + allExperiments.push(experiment); |
| 27 | + }); |
| 28 | + }); |
| 29 | + datafile.experiments.forEach(function(experiment) { |
| 30 | + allExperiments.push(experiment); |
| 31 | + }); |
| 32 | + return allExperiments; |
| 33 | +}; |
| 34 | + |
| 35 | +describe('lib/core/optimizely_config', function() { |
| 36 | + describe('Optimizely Config', function() { |
| 37 | + var optimizelyConfigObject; |
| 38 | + var projectConfigObject; |
| 39 | + beforeEach(function() { |
| 40 | + projectConfigObject = projectConfig.createProjectConfig(datafile); |
| 41 | + optimizelyConfigObject = optimizelyConfig.getOptimizelyConfig(projectConfigObject); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should return all experiments except rollouts', function() { |
| 45 | + var experimentsMap = optimizelyConfigObject.experimentsMap; |
| 46 | + var experimentsCount = Object.keys(optimizelyConfigObject.experimentsMap).length; |
| 47 | + assert.equal(experimentsCount, 6); |
| 48 | + |
| 49 | + var allExperiments = getAllExperimentsFromDatafile(datafile); |
| 50 | + allExperiments.forEach(function(experiment) { |
| 51 | + assert.include(experimentsMap[experiment.key], { |
| 52 | + id: experiment.id, |
| 53 | + key: experiment.key, |
| 54 | + }); |
| 55 | + var variationsMap = experimentsMap[experiment.key].variationsMap; |
| 56 | + experiment.variations.forEach(function(variation) { |
| 57 | + assert.include(variationsMap[variation.key], { |
| 58 | + id: variation.id, |
| 59 | + key: variation.key, |
| 60 | + }) |
| 61 | + }); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should return all the feature flags', function() { |
| 66 | + var featureFlagsCount = Object.keys(optimizelyConfigObject.featuresMap).length; |
| 67 | + assert.equal(featureFlagsCount, 7); |
| 68 | + |
| 69 | + var featuresMap = optimizelyConfigObject.featuresMap; |
| 70 | + datafile.featureFlags.forEach(function(featureFlag) { |
| 71 | + assert.include(featuresMap[featureFlag.key], { |
| 72 | + id: featureFlag.id, |
| 73 | + key: featureFlag.key, |
| 74 | + }); |
| 75 | + featureFlag.experimentIds.forEach(function(experimentId) { |
| 76 | + var experimentKey = projectConfigObject.experimentIdMap[experimentId].key; |
| 77 | + assert.isTrue(!!featuresMap[featureFlag.key].experimentsMap[experimentKey]); |
| 78 | + }); |
| 79 | + var variablesMap = featuresMap[featureFlag.key].variablesMap; |
| 80 | + featureFlag.variables.forEach(function(variable) { |
| 81 | + assert.include(variablesMap[variable.key], { |
| 82 | + id: variable.id, |
| 83 | + key: variable.key, |
| 84 | + type: variable.type, |
| 85 | + value: variable.defaultValue, |
| 86 | + }) |
| 87 | + }); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should correctly merge all feature variables', function() { |
| 92 | + var featureFlags = datafile.featureFlags; |
| 93 | + var datafileExperimentsMap = getAllExperimentsFromDatafile(datafile) |
| 94 | + .reduce(function(experiments, experiment) { |
| 95 | + experiments[experiment.key] = experiment; |
| 96 | + return experiments; |
| 97 | + }, {}); |
| 98 | + featureFlags.forEach(function(featureFlag) { |
| 99 | + var experimentIds = featureFlag.experimentIds; |
| 100 | + experimentIds.forEach(function(experimentId) { |
| 101 | + var experimentKey = projectConfigObject.experimentIdMap[experimentId].key; |
| 102 | + var experiment = optimizelyConfigObject.experimentsMap[experimentKey]; |
| 103 | + var variations = datafileExperimentsMap[experimentKey].variations; |
| 104 | + var variationsMap = experiment.variationsMap; |
| 105 | + variations.forEach(function(variation) { |
| 106 | + featureFlag.variables.forEach(function(variable) { |
| 107 | + var variableToAssert = variationsMap[variation.key].variablesMap[variable.key]; |
| 108 | + assert.include(variable, { |
| 109 | + id: variableToAssert.id, |
| 110 | + key: variableToAssert.key, |
| 111 | + type: variableToAssert.type, |
| 112 | + }); |
| 113 | + if (!variation.featureEnabled) { |
| 114 | + assert.equal(variable.defaultValue, variableToAssert.value); |
| 115 | + } |
| 116 | + }); |
| 117 | + }) |
| 118 | + }); |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should return correct config revision', function() { |
| 123 | + assert.equal(optimizelyConfigObject.revision, datafile.revision); |
| 124 | + }); |
| 125 | + }); |
| 126 | +}); |
0 commit comments