[uiSettingsClient] support overriding local defaults (#17559)
* [uiSettingsClient] support overriding local defaults * [chrome] remove injected uiSettings value
This commit is contained in:
parent
5b94f73eb6
commit
fd37e90777
1
src/ui/public/chrome/api/angular.js
vendored
1
src/ui/public/chrome/api/angular.js
vendored
|
@ -24,7 +24,6 @@ export function initAngularApi(chrome, internals) {
|
|||
.value('buildNum', internals.buildNum)
|
||||
.value('buildSha', internals.buildSha)
|
||||
.value('serverName', internals.serverName)
|
||||
.value('uiSettings', internals.uiSettings)
|
||||
.value('sessionId', Date.now())
|
||||
.value('chrome', chrome)
|
||||
.value('esUrl', (function () {
|
||||
|
|
|
@ -20,6 +20,81 @@ You can use \`config.get(\\"throwableProperty\\", defaultValue)\`, which will ju
|
|||
\`defaultValue\` when the key is unrecognized."
|
||||
`;
|
||||
|
||||
exports[`#overrideLocalDefault key has no user value calls subscriber with new and previous value: single subscriber call 1`] = `
|
||||
Array [
|
||||
Array [
|
||||
Object {
|
||||
"key": "dateFormat",
|
||||
"newValue": "bar",
|
||||
"oldValue": "Browser",
|
||||
},
|
||||
],
|
||||
]
|
||||
`;
|
||||
|
||||
exports[`#overrideLocalDefault key has no user value synchonously modifies the default value returned by get(): get after override 1`] = `"bar"`;
|
||||
|
||||
exports[`#overrideLocalDefault key has no user value synchonously modifies the default value returned by get(): get before override 1`] = `"Browser"`;
|
||||
|
||||
exports[`#overrideLocalDefault key has no user value synchonously modifies the value returned by getAll(): getAll after override 1`] = `
|
||||
Object {
|
||||
"dateFormat": Object {
|
||||
"value": "bar",
|
||||
},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`#overrideLocalDefault key has no user value synchonously modifies the value returned by getAll(): getAll before override 1`] = `
|
||||
Object {
|
||||
"dateFormat": Object {
|
||||
"value": "Browser",
|
||||
},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`#overrideLocalDefault key with user value does not modify the return value of get: get after override 1`] = `"foo"`;
|
||||
|
||||
exports[`#overrideLocalDefault key with user value does not modify the return value of get: get before override 1`] = `"foo"`;
|
||||
|
||||
exports[`#overrideLocalDefault key with user value is included in the return value of getAll: getAll after override 1`] = `
|
||||
Object {
|
||||
"dateFormat": Object {
|
||||
"userValue": "foo",
|
||||
"value": "bar",
|
||||
},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`#overrideLocalDefault key with user value is included in the return value of getAll: getAll before override 1`] = `
|
||||
Object {
|
||||
"dateFormat": Object {
|
||||
"userValue": "foo",
|
||||
"value": "Browser",
|
||||
},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`#overrideLocalDefault key with user value returns default override when setting removed: get after override 1`] = `"bar"`;
|
||||
|
||||
exports[`#overrideLocalDefault key with user value returns default override when setting removed: get before override 1`] = `"foo"`;
|
||||
|
||||
exports[`#overrideLocalDefault key with user value returns default override when setting removed: getAll after override 1`] = `
|
||||
Object {
|
||||
"dateFormat": Object {
|
||||
"value": "bar",
|
||||
},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`#overrideLocalDefault key with user value returns default override when setting removed: getAll before override 1`] = `
|
||||
Object {
|
||||
"dateFormat": Object {
|
||||
"userValue": "foo",
|
||||
"value": "bar",
|
||||
},
|
||||
}
|
||||
`;
|
||||
|
||||
exports[`#subscribe calls handler with { key, newValue, oldValue } when config changes 1`] = `
|
||||
Array [
|
||||
Array [
|
||||
|
|
|
@ -83,6 +83,30 @@ You can use \`config.get("${key}", defaultValue)\`, which will just return
|
|||
return this.isDeclared(key) && !('value' in this._cache[key]);
|
||||
}
|
||||
|
||||
overrideLocalDefault(key, newDefault) {
|
||||
// capture the previous value
|
||||
const prevDefault = this._defaults[key]
|
||||
? this._defaults[key].value
|
||||
: undefined;
|
||||
|
||||
// update defaults map
|
||||
this._defaults[key] = {
|
||||
...this._defaults[key] || {},
|
||||
value: newDefault
|
||||
};
|
||||
|
||||
// update cached default value
|
||||
this._cache[key] = {
|
||||
...this._cache[key] || {},
|
||||
value: newDefault
|
||||
};
|
||||
|
||||
// don't broadcast change if userValue was already overriding the default
|
||||
if (this._cache[key].userValue == null) {
|
||||
this._broadcastUpdate(key, newDefault, prevDefault);
|
||||
}
|
||||
}
|
||||
|
||||
subscribe(observer) {
|
||||
this._updateObservers.add(observer);
|
||||
|
||||
|
@ -139,6 +163,10 @@ You can use \`config.get("${key}", defaultValue)\`, which will just return
|
|||
}
|
||||
}
|
||||
|
||||
this._broadcastUpdate(key, newValue, oldValue);
|
||||
}
|
||||
|
||||
_broadcastUpdate(key, newValue, oldValue) {
|
||||
this._notify.log(`config change: ${key}: ${oldValue} -> ${newValue}`);
|
||||
|
||||
for (const observer of this._updateObservers) {
|
||||
|
|
|
@ -201,3 +201,77 @@ describe('#subscribe', () => {
|
|||
expect(handler).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('#overrideLocalDefault', () => {
|
||||
describe('key has no user value', () => {
|
||||
it('synchonously modifies the default value returned by get()', () => {
|
||||
const { config } = setup();
|
||||
|
||||
expect(config.get('dateFormat')).toMatchSnapshot('get before override');
|
||||
config.overrideLocalDefault('dateFormat', 'bar');
|
||||
expect(config.get('dateFormat')).toMatchSnapshot('get after override');
|
||||
});
|
||||
|
||||
it('synchonously modifies the value returned by getAll()', () => {
|
||||
const { config } = setup();
|
||||
|
||||
expect(config.getAll()).toMatchSnapshot('getAll before override');
|
||||
config.overrideLocalDefault('dateFormat', 'bar');
|
||||
expect(config.getAll()).toMatchSnapshot('getAll after override');
|
||||
});
|
||||
|
||||
it('calls subscriber with new and previous value', () => {
|
||||
const handler = jest.fn();
|
||||
const { config } = setup();
|
||||
|
||||
config.subscribe(handler);
|
||||
config.overrideLocalDefault('dateFormat', 'bar');
|
||||
expect(handler.mock.calls).toMatchSnapshot('single subscriber call');
|
||||
});
|
||||
});
|
||||
|
||||
describe('key with user value', () => {
|
||||
it('does not modify the return value of get', () => {
|
||||
const { config } = setup();
|
||||
|
||||
config.set('dateFormat', 'foo');
|
||||
expect(config.get('dateFormat')).toMatchSnapshot('get before override');
|
||||
config.overrideLocalDefault('dateFormat', 'bar');
|
||||
expect(config.get('dateFormat')).toMatchSnapshot('get after override');
|
||||
});
|
||||
|
||||
it('is included in the return value of getAll', () => {
|
||||
const { config } = setup();
|
||||
|
||||
config.set('dateFormat', 'foo');
|
||||
expect(config.getAll()).toMatchSnapshot('getAll before override');
|
||||
config.overrideLocalDefault('dateFormat', 'bar');
|
||||
expect(config.getAll()).toMatchSnapshot('getAll after override');
|
||||
});
|
||||
|
||||
it('does not call subscriber', () => {
|
||||
const handler = jest.fn();
|
||||
const { config } = setup();
|
||||
|
||||
config.set('dateFormat', 'foo');
|
||||
config.subscribe(handler);
|
||||
config.overrideLocalDefault('dateFormat', 'bar');
|
||||
expect(handler).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns default override when setting removed', () => {
|
||||
const { config } = setup();
|
||||
|
||||
config.set('dateFormat', 'foo');
|
||||
config.overrideLocalDefault('dateFormat', 'bar');
|
||||
|
||||
expect(config.get('dateFormat')).toMatchSnapshot('get before override');
|
||||
expect(config.getAll()).toMatchSnapshot('getAll before override');
|
||||
|
||||
config.remove('dateFormat');
|
||||
|
||||
expect(config.get('dateFormat')).toMatchSnapshot('get after override');
|
||||
expect(config.getAll()).toMatchSnapshot('getAll after override');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue