diff --git a/src/ui/public/config/__tests__/config.js b/src/ui/public/config/__tests__/config.js index f072ece7b8f3d7..116075ed2b9e39 100644 --- a/src/ui/public/config/__tests__/config.js +++ b/src/ui/public/config/__tests__/config.js @@ -57,13 +57,6 @@ describe('config component', function () { expect(config.set).withArgs('unrecognizedProperty', 'somevalue').to.not.throwException(); expect(config.get('unrecognizedProperty')).to.be('somevalue'); }); - - // there is currently no way to make it return false without significant changes to the code - it('returns true for success', async () => { - expect(await config.set('random', 'value')).to.be(true); - // setting to the same should set it to true as well - expect(await config.set('random', 'value')).to.be(true); - }); }); describe('#$bind', function () { @@ -74,7 +67,7 @@ describe('config component', function () { expect($scope).to.have.property('dateFormat', dateFormat); }); - it('alows overriding the property name', function () { + it('allows overriding the property name', function () { const dateFormat = config.get('dateFormat'); config.bindToScope($scope, 'dateFormat', 'defaultDateFormat'); expect($scope).to.not.have.property('dateFormat'); @@ -95,4 +88,24 @@ describe('config component', function () { }); + describe('#_change', () => { + + it('returns true for success', async () => { + // immediately resolve to avoid timing issues + const delayedUpdate = Promise.resolve(); + + expect(await config._change('random', 'value', { _delayedUpdate: delayedUpdate })).to.be(true); + // setting to the same should set it to true as well + expect(await config._change('random', 'value')).to.be(true); + }); + + it('returns false for failure', async () => { + // immediately resolve to avoid timing issues + const delayedUpdate = Promise.reject(); + + expect(await config._change('random', 'value', { _delayedUpdate: delayedUpdate })).to.be(false); + }); + + }); + }); diff --git a/src/ui/public/config/config.js b/src/ui/public/config/config.js index 1e55143d2488b1..7a10a14b7ae296 100644 --- a/src/ui/public/config/config.js +++ b/src/ui/public/config/config.js @@ -15,8 +15,8 @@ module.service(`config`, function (Private, $rootScope, chrome, uiSettings) { config.getAll = () => cloneDeep(settings); config.get = (key, defaultValue) => getCurrentValue(key, defaultValue); - config.set = (key, val) => change(key, isPlainObject(val) ? angular.toJson(val) : val); - config.remove = key => change(key, null); + config.set = (key, val) => config._change(key, isPlainObject(val) ? angular.toJson(val) : val); + config.remove = key => config._change(key, null); config.isDeclared = key => key in settings; config.isDefault = key => !config.isDeclared(key) || nullOrEmpty(settings[key].userValue); config.isCustom = key => config.isDeclared(key) && !('value' in settings[key]); @@ -57,7 +57,7 @@ any custom setting configuration watchers for "${key}" may fix this issue.`); return scope.$on(`change:config`, update); } - function change(key, value) { + config._change = (key, value, { _delayedUpdate = delayedUpdate }) => { const declared = config.isDeclared(key); const oldVal = declared ? settings[key].userValue : undefined; const newVal = key in defaults && defaults[key].defaultValue === value ? null : value; @@ -68,7 +68,7 @@ any custom setting configuration watchers for "${key}" may fix this issue.`); const initialVal = declared ? config.get(key) : undefined; localUpdate(key, newVal, initialVal); - return delayedUpdate(key, newVal) + return _delayedUpdate(key, newVal) .then(updatedSettings => { settings = mergeSettings(defaults, updatedSettings); return true; @@ -78,7 +78,7 @@ any custom setting configuration watchers for "${key}" may fix this issue.`); notify.error(reason); return false; }); - } + }; function localUpdate(key, newVal, oldVal) { patch(key, newVal);