Support for feature flags

This commit is contained in:
David Kaltschmidt
2016-11-14 17:16:46 +01:00
parent d131e99039
commit afe177cdba
5 changed files with 93 additions and 1 deletions

View File

@@ -0,0 +1,30 @@
const FU = require('../feature-utils');
describe('FeatureUtils', () => {
const FEATURE_X_KEY = 'my feature 1';
const FEATURE_Y_KEY = 'my feature 2';
beforeEach(() => {
FU.clearFeatures();
});
describe('Setting of features', () => {
it('should not have any features by default', () => {
expect(FU.featureIsEnabled(FEATURE_X_KEY)).toBeFalsy();
expect(FU.featureIsEnabled(FEATURE_Y_KEY)).toBeFalsy();
});
it('should work with enabling one feature', () => {
let success;
expect(FU.featureIsEnabled(FEATURE_X_KEY)).toBeFalsy();
success = FU.setFeature(FEATURE_X_KEY, true);
expect(success).toBeTruthy();
expect(FU.featureIsEnabled(FEATURE_X_KEY)).toBeTruthy();
expect(FU.featureIsEnabled(FEATURE_Y_KEY)).toBeFalsy();
success = FU.setFeature(FEATURE_X_KEY, false);
expect(success).toBeTruthy();
expect(FU.featureIsEnabled(FEATURE_X_KEY)).toBeFalsy();
});
});
});

View File

@@ -0,0 +1,18 @@
import { storageGetObject, storageSetObject } from './storage-utils';
const STORAGE_FEATURE_KEY = 'scopeFeatureFlags';
export function featureIsEnabled(feature) {
const features = storageGetObject(STORAGE_FEATURE_KEY, {});
return features[feature];
}
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, {});
}

View File

@@ -6,7 +6,7 @@ const log = debug('scope:storage-utils');
const storage = typeof(Storage) !== 'undefined' ? window.localStorage : null;
export function storageGet(key, defaultValue) {
if (storage && storage[key] !== undefined) {
if (storage && storage.getItem(key) !== undefined) {
return storage.getItem(key);
}
return defaultValue;
@@ -16,8 +16,31 @@ export function storageSet(key, value) {
if (storage) {
try {
storage.setItem(key, value);
return true;
} catch (e) {
log('Error storing value in storage. Maybe full? Could not store key.', key);
}
}
return false;
}
export function storageGetObject(key, defaultValue) {
const value = storageGet(key);
if (value) {
try {
return JSON.parse(value);
} catch (e) {
log('Error getting object for key.', key);
}
}
return defaultValue;
}
export function storageSetObject(key, obj) {
try {
return storageSet(key, JSON.stringify(obj));
} catch (e) {
log('Error encoding object for key', key);
}
return false;
}

View File

@@ -90,6 +90,10 @@
},
"jest": {
"transform": {".*": "<rootDir>/node_modules/babel-jest"},
"scriptPreprocessor": "<rootDir>/node_modules/babel-jest",
"setupFiles": [
"<rootDir>/test/support/localStorage.js"
],
"testPathDirs": [
"<rootDir>/app/scripts"
],

View File

@@ -0,0 +1,17 @@
const localStorageMock = (function() {
let store = {};
return {
store,
getItem: function(key) {
return store[key];
},
setItem: function(key, value) {
store[key] = value.toString();
},
clear: function() {
store = {};
}
};
})();
Object.defineProperty(window, 'Storage', { value: localStorageMock });
Object.defineProperty(window, 'localStorage', { value: localStorageMock });