From faa8ee098cd7886ded27af584b98ba94bf85a39c Mon Sep 17 00:00:00 2001 From: David Kaltschmidt Date: Tue, 15 Nov 2016 11:35:35 +0100 Subject: [PATCH] Adapted feature check to allow only some features --- .../utils/__tests__/feature-utils-test.js | 15 ++++++- client/app/scripts/utils/feature-utils.js | 43 ++++++++++++++----- client/test/support/localStorage.js | 2 +- 3 files changed, 47 insertions(+), 13 deletions(-) diff --git a/client/app/scripts/utils/__tests__/feature-utils-test.js b/client/app/scripts/utils/__tests__/feature-utils-test.js index 2395236e4..f0ee39296 100644 --- a/client/app/scripts/utils/__tests__/feature-utils-test.js +++ b/client/app/scripts/utils/__tests__/feature-utils-test.js @@ -6,7 +6,8 @@ describe('FeatureUtils', () => { const FEATURE_Y_KEY = 'my feature 2'; beforeEach(() => { - FU.clearFeatures(); + FU.setFeature(FEATURE_X_KEY, false); + FU.setFeature(FEATURE_Y_KEY, false); }); describe('Setting of features', () => { @@ -26,5 +27,17 @@ describe('FeatureUtils', () => { expect(success).toBeTruthy(); expect(FU.featureIsEnabled(FEATURE_X_KEY)).toBeFalsy(); }); + + it('should allow for either feature', () => { + let success; + expect(FU.featureIsEnabledAny(FEATURE_X_KEY, FEATURE_Y_KEY)).toBeFalsy(); + success = FU.setFeature(FEATURE_X_KEY, true); + expect(success).toBeTruthy(); + expect(FU.featureIsEnabledAny(FEATURE_X_KEY, FEATURE_Y_KEY)).toBeTruthy(); + success = FU.setFeature(FEATURE_X_KEY, false); + success = FU.setFeature(FEATURE_Y_KEY, true); + expect(success).toBeTruthy(); + expect(FU.featureIsEnabledAny(FEATURE_X_KEY, FEATURE_Y_KEY)).toBeTruthy(); + }); }); }); diff --git a/client/app/scripts/utils/feature-utils.js b/client/app/scripts/utils/feature-utils.js index abd4ae3b5..91c2fccfe 100644 --- a/client/app/scripts/utils/feature-utils.js +++ b/client/app/scripts/utils/feature-utils.js @@ -1,18 +1,39 @@ -import { storageGetObject, storageSetObject } from './storage-utils'; +import { storageGet, storageSet } from './storage-utils'; -const STORAGE_FEATURE_KEY = 'scopeFeatureFlags'; +// prefix for all feature flags +const STORAGE_KEY_PREFIX = 'scope-experimental-'; +const getKey = key => `${STORAGE_KEY_PREFIX}${key}`; + +/** + * Returns true if `feature` is enabled + * + * Features can be enabled either via calling `setFeature()` or by setting + * `localStorage.scope-experimental-featureName = true` in the console. + * @param {String} feature Feature name, ideally one word or hyphenated + * @return {Boolean} True if feature is enabled + */ export function featureIsEnabled(feature) { - const features = storageGetObject(STORAGE_FEATURE_KEY, {}); - return features[feature]; + return storageGet(getKey(feature)); } +/** + * Returns true if any of the features given as arguments are enabled. + * + * Useful if features are hierarchical, e.g.: + * `featureIsEnabledAny('superFeature', 'subFeature')` + * @param {String} args Feature names + * @return {Boolean} True if any of the features are enabled + */ +export function featureIsEnabledAny(...args) { + return Array.prototype.some.call(args, feature => featureIsEnabled(feature)); +} + +/** + * Set true/false if a feature is enabled. + * @param {String} feature Feature name + * @param {Boolean} isEnabled true/false + */ export function setFeature(feature, isEnabled) { - const features = storageGetObject(STORAGE_FEATURE_KEY, {}); - features[feature] = isEnabled; - return storageSetObject(STORAGE_FEATURE_KEY, features); -} - -export function clearFeatures() { - return storageSetObject(STORAGE_FEATURE_KEY, {}); + return storageSet(getKey(feature), isEnabled); } diff --git a/client/test/support/localStorage.js b/client/test/support/localStorage.js index d153bdf8a..50cf4b680 100644 --- a/client/test/support/localStorage.js +++ b/client/test/support/localStorage.js @@ -6,7 +6,7 @@ const localStorageMock = (function() { return store[key]; }, setItem: function(key, value) { - store[key] = value.toString(); + store[key] = value; }, clear: function() { store = {};