mirror of
https://github.com/weaveworks/scope.git
synced 2026-03-06 03:31:00 +00:00
Support for feature flags
This commit is contained in:
30
client/app/scripts/utils/__tests__/feature-utils-test.js
Normal file
30
client/app/scripts/utils/__tests__/feature-utils-test.js
Normal 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();
|
||||
});
|
||||
});
|
||||
});
|
||||
18
client/app/scripts/utils/feature-utils.js
Normal file
18
client/app/scripts/utils/feature-utils.js
Normal 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, {});
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
],
|
||||
|
||||
17
client/test/support/localStorage.js
Normal file
17
client/test/support/localStorage.js
Normal 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 });
|
||||
Reference in New Issue
Block a user