From a1be97efcd4275517de5670d7ed56b91dff8a4fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Bevacqua?= Date: Fri, 3 Jun 2016 17:37:19 -0300 Subject: [PATCH 01/12] [settings] Added .get, renamed .getAll as .getRaw, added new .getAll method. --- src/ui/settings/index.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/ui/settings/index.js b/src/ui/settings/index.js index fbc17c2075af3..0f14673370c58 100644 --- a/src/ui/settings/index.js +++ b/src/ui/settings/index.js @@ -4,7 +4,9 @@ import defaultsProvider from './defaults'; export default function setupSettings(kbnServer, server, config) { const status = kbnServer.status.create('ui settings'); const uiSettings = { + get, getAll, + getRaw, getDefaults, getUserProvided, set, @@ -15,7 +17,23 @@ export default function setupSettings(kbnServer, server, config) { server.decorate('server', 'uiSettings', () => uiSettings); kbnServer.ready().then(mirrorEsStatus); + function get(key) { + return getAll().then(all => all[key]); + } + function getAll() { + return getRaw() + .then(raw => Object.keys(raw) + .reduce((all, key) => { + const item = raw[key]; + const hasUserValue = 'userValue' in item; + all[key] = hasUserValue ? item.userValue : item.value; + return all; + }, {}) + ); + } + + function getRaw() { return Promise .all([getDefaults(), getUserProvided()]) .then(([defaults, user]) => defaultsDeep(user, defaults)); From 7e191b82dd2a5ad8f51dfed069e9151571938380 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Bevacqua?= Date: Mon, 27 Jun 2016 14:48:12 -0300 Subject: [PATCH 02/12] [fix] @spalger identified as NSA mole. Run server-side tests in src/ui directory. --- tasks/config/simplemocha.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tasks/config/simplemocha.js b/tasks/config/simplemocha.js index 2dddcfbe1bd41..95213d40e2f97 100644 --- a/tasks/config/simplemocha.js +++ b/tasks/config/simplemocha.js @@ -13,8 +13,7 @@ module.exports = { 'test/**/__tests__/**/*.js', 'src/**/__tests__/**/*.js', 'test/fixtures/__tests__/*.js', - '!src/**/public/**', - '!src/ui/**' + '!src/**/public/**' ] } }; From 415e6635f1399bc2f6883efce787e8fbae64faa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Bevacqua?= Date: Mon, 27 Jun 2016 14:49:00 -0300 Subject: [PATCH 03/12] [settings] Added .removeMany method for convenience single-roundtrip. --- src/ui/settings/index.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/ui/settings/index.js b/src/ui/settings/index.js index 0f14673370c58..0cf2aac900443 100644 --- a/src/ui/settings/index.js +++ b/src/ui/settings/index.js @@ -11,7 +11,8 @@ export default function setupSettings(kbnServer, server, config) { getUserProvided, set, setMany, - remove + remove, + removeMany }; server.decorate('server', 'uiSettings', () => uiSettings); @@ -77,6 +78,14 @@ export default function setupSettings(kbnServer, server, config) { return set(key, null); } + function removeMany(keys) { + const changes = {}; + keys.forEach(key => { + changes[key] = null; + }); + return setMany(changes); + } + function mirrorEsStatus() { const esStatus = kbnServer.status.getForPluginId('elasticsearch'); From 1657c65e9da529e916c5c9e80fbbda59e94c1bd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Bevacqua?= Date: Mon, 27 Jun 2016 14:49:17 -0300 Subject: [PATCH 04/12] [test] Introduced server-side tests for UI Settings feature. --- src/ui/settings/__tests__/index.js | 345 +++++++++++++++++++++++++++++ 1 file changed, 345 insertions(+) create mode 100644 src/ui/settings/__tests__/index.js diff --git a/src/ui/settings/__tests__/index.js b/src/ui/settings/__tests__/index.js new file mode 100644 index 0000000000000..d1534aa4d0b16 --- /dev/null +++ b/src/ui/settings/__tests__/index.js @@ -0,0 +1,345 @@ +import sinon from 'sinon'; +import expect from 'expect.js'; +import init from '..'; +import defaultsProvider from '../defaults'; + +describe('ui settings', function () { + describe('overview', function () { + it('has expected api surface', function () { + const { uiSettings } = instantiate(); + expect(typeof uiSettings.get).to.be('function'); + expect(typeof uiSettings.getAll).to.be('function'); + expect(typeof uiSettings.getDefaults).to.be('function'); + expect(typeof uiSettings.getRaw).to.be('function'); + expect(typeof uiSettings.getUserProvided).to.be('function'); + expect(typeof uiSettings.remove).to.be('function'); + expect(typeof uiSettings.set).to.be('function'); + expect(typeof uiSettings.setMany).to.be('function'); + }); + }); + + describe('#setMany()', function () { + it('updates a single value in one operation', function () { + const { server, uiSettings, configGet } = instantiate(); + const result = uiSettings.setMany({ one: 'value' }); + expect(typeof result.then).to.be('function'); + expect(server.plugins.elasticsearch.client.update.callCount).to.eql(1); + expect(server.plugins.elasticsearch.client.update.firstCall.args).to.eql([{ + index: configGet('kibana.index'), + id: configGet('pkg.version'), + type: 'config', + body: { + doc: { one: 'value' } + } + }]); + }); + + it('updates several values in one operation', function () { + const { server, uiSettings, configGet } = instantiate(); + const result = uiSettings.setMany({ one: 'value', another: 'val' }); + expect(typeof result.then).to.be('function'); + expect(server.plugins.elasticsearch.client.update.callCount).to.eql(1); + expect(server.plugins.elasticsearch.client.update.firstCall.args).to.eql([{ + index: configGet('kibana.index'), + id: configGet('pkg.version'), + type: 'config', + body: { + doc: { one: 'value', another: 'val' } + } + }]); + }); + }); + + describe('#set()', function () { + it('updates single values by (key, value)', function () { + const { server, uiSettings, configGet } = instantiate(); + const result = uiSettings.set('one', 'value'); + expect(typeof result.then).to.be('function'); + expect(server.plugins.elasticsearch.client.update.callCount).to.eql(1); + expect(server.plugins.elasticsearch.client.update.firstCall.args).to.eql([{ + index: configGet('kibana.index'), + id: configGet('pkg.version'), + type: 'config', + body: { + doc: { one: 'value' } + } + }]); + }); + }); + + describe('#remove()', function () { + it('removes single values by key', function () { + const { server, uiSettings, configGet } = instantiate(); + const result = uiSettings.remove('one'); + expect(typeof result.then).to.be('function'); + expect(server.plugins.elasticsearch.client.update.callCount).to.eql(1); + expect(server.plugins.elasticsearch.client.update.firstCall.args).to.eql([{ + index: configGet('kibana.index'), + id: configGet('pkg.version'), + type: 'config', + body: { + doc: { one: null } + } + }]); + }); + }); + + describe('#removeMany()', function () { + it('removes a single value', function () { + const { server, uiSettings, configGet } = instantiate(); + const result = uiSettings.removeMany(['one']); + expect(typeof result.then).to.be('function'); + expect(server.plugins.elasticsearch.client.update.callCount).to.eql(1); + expect(server.plugins.elasticsearch.client.update.firstCall.args).to.eql([{ + index: configGet('kibana.index'), + id: configGet('pkg.version'), + type: 'config', + body: { + doc: { one: null } + } + }]); + }); + + it('updates several values in one operation', function () { + const { server, uiSettings, configGet } = instantiate(); + const result = uiSettings.removeMany(['one', 'two', 'three']); + expect(typeof result.then).to.be('function'); + expect(server.plugins.elasticsearch.client.update.callCount).to.eql(1); + expect(server.plugins.elasticsearch.client.update.firstCall.args).to.eql([{ + index: configGet('kibana.index'), + id: configGet('pkg.version'), + type: 'config', + body: { + doc: { one: null, two: null, three: null } + } + }]); + }); + }); + + describe('#getDefaults()', function () { + it('is promised the default values', async function () { + const { server, uiSettings, configGet } = instantiate(); + const defaults = await uiSettings.getDefaults(); + expect(defaults).eql(defaultsProvider()); + }); + }); + + describe('#getUserProvided()', function () { + it('pulls user configuration from ES', async function () { + const getResult = { user: 'customized' }; + const { server, uiSettings, configGet } = instantiate({ getResult }); + const result = await uiSettings.getUserProvided(); + expect(server.plugins.elasticsearch.client.get.callCount).to.eql(1); + expect(server.plugins.elasticsearch.client.get.firstCall.args).to.eql([{ + index: configGet('kibana.index'), + id: configGet('pkg.version'), + type: 'config' + }]); + expect(result).to.eql({ + user: { userValue: 'customized' } + }); + }); + + it('ignores null user configuration (because default values)', async function () { + const getResult = { user: 'customized', usingDefault: null, something: 'else' }; + const { server, uiSettings, configGet } = instantiate({ getResult }); + const result = await uiSettings.getUserProvided(); + expect(server.plugins.elasticsearch.client.get.callCount).to.eql(1); + expect(server.plugins.elasticsearch.client.get.firstCall.args).to.eql([{ + index: configGet('kibana.index'), + id: configGet('pkg.version'), + type: 'config' + }]); + expect(result).to.eql({ + user: { userValue: 'customized' }, something: { userValue: 'else' } + }); + }); + }); + + describe('#getRaw()', function () { + it(`without user configuration it's equal to the defaults`, async function () { + const getResult = {}; + const { server, uiSettings, configGet } = instantiate({ getResult }); + const result = await uiSettings.getRaw(); + expect(server.plugins.elasticsearch.client.get.callCount).to.eql(1); + expect(server.plugins.elasticsearch.client.get.firstCall.args).to.eql([{ + index: configGet('kibana.index'), + id: configGet('pkg.version'), + type: 'config' + }]); + expect(result).to.eql(defaultsProvider()); + }); + + it(`user configuration gets merged with defaults`, async function () { + const getResult = { foo: 'bar' }; + const { server, uiSettings, configGet } = instantiate({ getResult }); + const result = await uiSettings.getRaw(); + expect(server.plugins.elasticsearch.client.get.callCount).to.eql(1); + expect(server.plugins.elasticsearch.client.get.firstCall.args).to.eql([{ + index: configGet('kibana.index'), + id: configGet('pkg.version'), + type: 'config' + }]); + const merged = defaultsProvider(); + merged.foo = { userValue: 'bar' }; + expect(result).to.eql(merged); + }); + + it(`user configuration gets merged into defaults`, async function () { + const getResult = { dateFormat: 'YYYY-MM-DD' }; + const { server, uiSettings, configGet } = instantiate({ getResult }); + const result = await uiSettings.getRaw(); + expect(server.plugins.elasticsearch.client.get.callCount).to.eql(1); + expect(server.plugins.elasticsearch.client.get.firstCall.args).to.eql([{ + index: configGet('kibana.index'), + id: configGet('pkg.version'), + type: 'config' + }]); + const merged = defaultsProvider(); + merged.dateFormat.userValue = 'YYYY-MM-DD'; + expect(result).to.eql(merged); + }); + }); + + describe('#getAll()', function () { + it(`returns key value pairs`, async function () { + const getResult = {}; + const { server, uiSettings, configGet } = instantiate({ getResult }); + const result = await uiSettings.getAll(); + expect(server.plugins.elasticsearch.client.get.callCount).to.eql(1); + expect(server.plugins.elasticsearch.client.get.firstCall.args).to.eql([{ + index: configGet('kibana.index'), + id: configGet('pkg.version'), + type: 'config' + }]); + const defaults = defaultsProvider(); + const expectation = {}; + Object.keys(defaults).forEach(key => { + expectation[key] = defaults[key].value; + }); + expect(result).to.eql(expectation); + }); + + it(`returns key value pairs including user configuration`, async function () { + const getResult = { something: 'user-provided' }; + const { server, uiSettings, configGet } = instantiate({ getResult }); + const result = await uiSettings.getAll(); + expect(server.plugins.elasticsearch.client.get.callCount).to.eql(1); + expect(server.plugins.elasticsearch.client.get.firstCall.args).to.eql([{ + index: configGet('kibana.index'), + id: configGet('pkg.version'), + type: 'config' + }]); + const defaults = defaultsProvider(); + const expectation = {}; + Object.keys(defaults).forEach(key => { + expectation[key] = defaults[key].value; + }); + expectation.something = 'user-provided'; + expect(result).to.eql(expectation); + }); + + it(`returns key value pairs including user configuration for existing settings`, async function () { + const getResult = { dateFormat: 'YYYY-MM-DD' }; + const { server, uiSettings, configGet } = instantiate({ getResult }); + const result = await uiSettings.getAll(); + expect(server.plugins.elasticsearch.client.get.callCount).to.eql(1); + expect(server.plugins.elasticsearch.client.get.firstCall.args).to.eql([{ + index: configGet('kibana.index'), + id: configGet('pkg.version'), + type: 'config' + }]); + const defaults = defaultsProvider(); + const expectation = {}; + Object.keys(defaults).forEach(key => { + expectation[key] = defaults[key].value; + }); + expectation.dateFormat = 'YYYY-MM-DD'; + expect(result).to.eql(expectation); + }); + }); + + describe('#get()', function () { + it(`returns the promised value for a key`, async function () { + const getResult = {}; + const { server, uiSettings, configGet } = instantiate({ getResult }); + const result = await uiSettings.get('dateFormat'); + expect(server.plugins.elasticsearch.client.get.callCount).to.eql(1); + expect(server.plugins.elasticsearch.client.get.firstCall.args).to.eql([{ + index: configGet('kibana.index'), + id: configGet('pkg.version'), + type: 'config' + }]); + const defaults = defaultsProvider(); + expect(result).to.eql(defaults.dateFormat.value); + }); + + it(`returns the user-configured value for a custom key`, async function () { + const getResult = { custom: 'value' }; + const { server, uiSettings, configGet } = instantiate({ getResult }); + const result = await uiSettings.get('custom'); + expect(server.plugins.elasticsearch.client.get.callCount).to.eql(1); + expect(server.plugins.elasticsearch.client.get.firstCall.args).to.eql([{ + index: configGet('kibana.index'), + id: configGet('pkg.version'), + type: 'config' + }]); + const defaults = defaultsProvider(); + expect(result).to.eql('value'); + }); + + it(`returns the user-configured value for a modified key`, async function () { + const getResult = { dateFormat: 'YYYY-MM-DD' }; + const { server, uiSettings, configGet } = instantiate({ getResult }); + const result = await uiSettings.get('dateFormat'); + expect(server.plugins.elasticsearch.client.get.callCount).to.eql(1); + expect(server.plugins.elasticsearch.client.get.firstCall.args).to.eql([{ + index: configGet('kibana.index'), + id: configGet('pkg.version'), + type: 'config' + }]); + const defaults = defaultsProvider(); + expect(result).to.eql('YYYY-MM-DD'); + }); + }); +}); + +function instantiate({ getResult } = {}) { + const esStatus = { + state: 'green', + on: sinon.spy() + }; + const settingsStatus = { + state: 'green', + red: sinon.spy(), + yellow: sinon.spy(), + green: sinon.spy() + }; + const kbnServer = { + status: { + create: sinon.stub().withArgs('ui settings').returns(settingsStatus), + getForPluginId: sinon.stub().withArgs('elasticsearch').returns(esStatus) + }, + ready: sinon.stub().returns(Promise.resolve()) + }; + const server = { + decorate: (_, key, value) => server[key] = value, + plugins: { + elasticsearch: { + client: { + get: sinon.stub().returns(Promise.resolve({ _source: getResult })), + update: sinon.stub().returns(Promise.resolve()) + } + } + } + }; + const configGet = sinon.stub(); + configGet.withArgs('kibana.index').returns('.kibana'); + configGet.withArgs('pkg.version').returns('1.2.3-test'); + const config = { + get: configGet + }; + const setupSettings = init(kbnServer, server, config); + const uiSettings = server.uiSettings(); + return { kbnServer, server, config, uiSettings, esStatus, settingsStatus, configGet }; +} From 8ace579ed41a2efabc2b0719dca370df7dbdcedd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Bevacqua?= Date: Mon, 27 Jun 2016 14:59:57 -0300 Subject: [PATCH 05/12] [refactor] Update usage of uiSettings when pulling a single value --- .../kibana/server/routes/api/ingest/register_post.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/core_plugins/kibana/server/routes/api/ingest/register_post.js b/src/core_plugins/kibana/server/routes/api/ingest/register_post.js index a1b0c5976b54f..caf4d23f05795 100644 --- a/src/core_plugins/kibana/server/routes/api/ingest/register_post.js +++ b/src/core_plugins/kibana/server/routes/api/ingest/register_post.js @@ -64,7 +64,8 @@ export function registerPost(server) { } }, handler: async function (req, reply) { - const config = await server.uiSettings().getAll(); + const uiSettings = server.uiSettings(); + const metaFields = await uiSettings.get('metaFields'); const boundCallWithRequest = _.partial(server.plugins.elasticsearch.callWithRequest, req); const requestDocument = _.cloneDeep(req.payload); const indexPattern = keysToCamelCaseShallow(requestDocument.index_pattern); @@ -74,8 +75,6 @@ export function registerPost(server) { delete indexPattern.id; const mappings = createMappingsFromPatternFields(indexPattern.fields); - - const metaFields = _.get(config, 'metaFields.userValue', config.metaFields.value); const indexPatternMetaFields = _.map(metaFields, name => ({name})); indexPattern.fields = initDefaultFieldProps(indexPattern.fields.concat(indexPatternMetaFields)); From 0d31bb8228a0bb589743c814fdd8286b2fe6d3d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Bevacqua?= Date: Mon, 27 Jun 2016 15:16:40 -0300 Subject: [PATCH 06/12] [test] Simplified test cases. Single assertion. --- src/ui/settings/__tests__/index.js | 35 ++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/src/ui/settings/__tests__/index.js b/src/ui/settings/__tests__/index.js index d1534aa4d0b16..bc621826cff76 100644 --- a/src/ui/settings/__tests__/index.js +++ b/src/ui/settings/__tests__/index.js @@ -13,16 +13,22 @@ describe('ui settings', function () { expect(typeof uiSettings.getRaw).to.be('function'); expect(typeof uiSettings.getUserProvided).to.be('function'); expect(typeof uiSettings.remove).to.be('function'); + expect(typeof uiSettings.removeMany).to.be('function'); expect(typeof uiSettings.set).to.be('function'); expect(typeof uiSettings.setMany).to.be('function'); }); }); describe('#setMany()', function () { + it('returns a promise', () => { + const { uiSettings } = instantiate(); + const result = uiSettings.setMany({ a: 'b' }); + expect(typeof result.then).to.be('function'); + }); + it('updates a single value in one operation', function () { const { server, uiSettings, configGet } = instantiate(); const result = uiSettings.setMany({ one: 'value' }); - expect(typeof result.then).to.be('function'); expect(server.plugins.elasticsearch.client.update.callCount).to.eql(1); expect(server.plugins.elasticsearch.client.update.firstCall.args).to.eql([{ index: configGet('kibana.index'), @@ -37,7 +43,6 @@ describe('ui settings', function () { it('updates several values in one operation', function () { const { server, uiSettings, configGet } = instantiate(); const result = uiSettings.setMany({ one: 'value', another: 'val' }); - expect(typeof result.then).to.be('function'); expect(server.plugins.elasticsearch.client.update.callCount).to.eql(1); expect(server.plugins.elasticsearch.client.update.firstCall.args).to.eql([{ index: configGet('kibana.index'), @@ -51,10 +56,15 @@ describe('ui settings', function () { }); describe('#set()', function () { + it('returns a promise', () => { + const { uiSettings } = instantiate(); + const result = uiSettings.set('a', 'b'); + expect(typeof result.then).to.be('function'); + }); + it('updates single values by (key, value)', function () { const { server, uiSettings, configGet } = instantiate(); const result = uiSettings.set('one', 'value'); - expect(typeof result.then).to.be('function'); expect(server.plugins.elasticsearch.client.update.callCount).to.eql(1); expect(server.plugins.elasticsearch.client.update.firstCall.args).to.eql([{ index: configGet('kibana.index'), @@ -68,10 +78,15 @@ describe('ui settings', function () { }); describe('#remove()', function () { + it('returns a promise', () => { + const { uiSettings } = instantiate(); + const result = uiSettings.remove('one'); + expect(typeof result.then).to.be('function'); + }); + it('removes single values by key', function () { const { server, uiSettings, configGet } = instantiate(); const result = uiSettings.remove('one'); - expect(typeof result.then).to.be('function'); expect(server.plugins.elasticsearch.client.update.callCount).to.eql(1); expect(server.plugins.elasticsearch.client.update.firstCall.args).to.eql([{ index: configGet('kibana.index'), @@ -85,10 +100,15 @@ describe('ui settings', function () { }); describe('#removeMany()', function () { + it('returns a promise', () => { + const { uiSettings } = instantiate(); + const result = uiSettings.removeMany(['one']); + expect(typeof result.then).to.be('function'); + }); + it('removes a single value', function () { const { server, uiSettings, configGet } = instantiate(); const result = uiSettings.removeMany(['one']); - expect(typeof result.then).to.be('function'); expect(server.plugins.elasticsearch.client.update.callCount).to.eql(1); expect(server.plugins.elasticsearch.client.update.firstCall.args).to.eql([{ index: configGet('kibana.index'), @@ -103,7 +123,6 @@ describe('ui settings', function () { it('updates several values in one operation', function () { const { server, uiSettings, configGet } = instantiate(); const result = uiSettings.removeMany(['one', 'two', 'three']); - expect(typeof result.then).to.be('function'); expect(server.plugins.elasticsearch.client.update.callCount).to.eql(1); expect(server.plugins.elasticsearch.client.update.firstCall.args).to.eql([{ index: configGet('kibana.index'), @@ -284,7 +303,6 @@ describe('ui settings', function () { id: configGet('pkg.version'), type: 'config' }]); - const defaults = defaultsProvider(); expect(result).to.eql('value'); }); @@ -298,7 +316,6 @@ describe('ui settings', function () { id: configGet('pkg.version'), type: 'config' }]); - const defaults = defaultsProvider(); expect(result).to.eql('YYYY-MM-DD'); }); }); @@ -341,5 +358,5 @@ function instantiate({ getResult } = {}) { }; const setupSettings = init(kbnServer, server, config); const uiSettings = server.uiSettings(); - return { kbnServer, server, config, uiSettings, esStatus, settingsStatus, configGet }; + return { server, uiSettings, configGet }; } From 11c3bf503e90d39baf6e5259d606ff387557349a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Bevacqua?= Date: Mon, 27 Jun 2016 15:23:04 -0300 Subject: [PATCH 07/12] [test] Refactor to avoid code duplication. --- src/ui/settings/__tests__/index.js | 185 ++++++++++------------------- 1 file changed, 65 insertions(+), 120 deletions(-) diff --git a/src/ui/settings/__tests__/index.js b/src/ui/settings/__tests__/index.js index bc621826cff76..f8dc0be4fd06e 100644 --- a/src/ui/settings/__tests__/index.js +++ b/src/ui/settings/__tests__/index.js @@ -29,29 +29,17 @@ describe('ui settings', function () { it('updates a single value in one operation', function () { const { server, uiSettings, configGet } = instantiate(); const result = uiSettings.setMany({ one: 'value' }); - expect(server.plugins.elasticsearch.client.update.callCount).to.eql(1); - expect(server.plugins.elasticsearch.client.update.firstCall.args).to.eql([{ - index: configGet('kibana.index'), - id: configGet('pkg.version'), - type: 'config', - body: { - doc: { one: 'value' } - } - }]); + expectElasticsearchUpdateQuery(server, configGet, { + one: 'value' + }); }); it('updates several values in one operation', function () { const { server, uiSettings, configGet } = instantiate(); const result = uiSettings.setMany({ one: 'value', another: 'val' }); - expect(server.plugins.elasticsearch.client.update.callCount).to.eql(1); - expect(server.plugins.elasticsearch.client.update.firstCall.args).to.eql([{ - index: configGet('kibana.index'), - id: configGet('pkg.version'), - type: 'config', - body: { - doc: { one: 'value', another: 'val' } - } - }]); + expectElasticsearchUpdateQuery(server, configGet, { + one: 'value', another: 'val' + }); }); }); @@ -65,15 +53,9 @@ describe('ui settings', function () { it('updates single values by (key, value)', function () { const { server, uiSettings, configGet } = instantiate(); const result = uiSettings.set('one', 'value'); - expect(server.plugins.elasticsearch.client.update.callCount).to.eql(1); - expect(server.plugins.elasticsearch.client.update.firstCall.args).to.eql([{ - index: configGet('kibana.index'), - id: configGet('pkg.version'), - type: 'config', - body: { - doc: { one: 'value' } - } - }]); + expectElasticsearchUpdateQuery(server, configGet, { + one: 'value' + }); }); }); @@ -87,15 +69,9 @@ describe('ui settings', function () { it('removes single values by key', function () { const { server, uiSettings, configGet } = instantiate(); const result = uiSettings.remove('one'); - expect(server.plugins.elasticsearch.client.update.callCount).to.eql(1); - expect(server.plugins.elasticsearch.client.update.firstCall.args).to.eql([{ - index: configGet('kibana.index'), - id: configGet('pkg.version'), - type: 'config', - body: { - doc: { one: null } - } - }]); + expectElasticsearchUpdateQuery(server, configGet, { + one: null + }); }); }); @@ -109,29 +85,17 @@ describe('ui settings', function () { it('removes a single value', function () { const { server, uiSettings, configGet } = instantiate(); const result = uiSettings.removeMany(['one']); - expect(server.plugins.elasticsearch.client.update.callCount).to.eql(1); - expect(server.plugins.elasticsearch.client.update.firstCall.args).to.eql([{ - index: configGet('kibana.index'), - id: configGet('pkg.version'), - type: 'config', - body: { - doc: { one: null } - } - }]); + expectElasticsearchUpdateQuery(server, configGet, { + one: null + }); }); it('updates several values in one operation', function () { const { server, uiSettings, configGet } = instantiate(); const result = uiSettings.removeMany(['one', 'two', 'three']); - expect(server.plugins.elasticsearch.client.update.callCount).to.eql(1); - expect(server.plugins.elasticsearch.client.update.firstCall.args).to.eql([{ - index: configGet('kibana.index'), - id: configGet('pkg.version'), - type: 'config', - body: { - doc: { one: null, two: null, three: null } - } - }]); + expectElasticsearchUpdateQuery(server, configGet, { + one: null, two: null, three: null + }); }); }); @@ -148,12 +112,13 @@ describe('ui settings', function () { const getResult = { user: 'customized' }; const { server, uiSettings, configGet } = instantiate({ getResult }); const result = await uiSettings.getUserProvided(); - expect(server.plugins.elasticsearch.client.get.callCount).to.eql(1); - expect(server.plugins.elasticsearch.client.get.firstCall.args).to.eql([{ - index: configGet('kibana.index'), - id: configGet('pkg.version'), - type: 'config' - }]); + expectElasticsearchGetQuery(server, configGet); + }); + + it('returns user configuration', async function () { + const getResult = { user: 'customized' }; + const { server, uiSettings, configGet } = instantiate({ getResult }); + const result = await uiSettings.getUserProvided(); expect(result).to.eql({ user: { userValue: 'customized' } }); @@ -163,12 +128,6 @@ describe('ui settings', function () { const getResult = { user: 'customized', usingDefault: null, something: 'else' }; const { server, uiSettings, configGet } = instantiate({ getResult }); const result = await uiSettings.getUserProvided(); - expect(server.plugins.elasticsearch.client.get.callCount).to.eql(1); - expect(server.plugins.elasticsearch.client.get.firstCall.args).to.eql([{ - index: configGet('kibana.index'), - id: configGet('pkg.version'), - type: 'config' - }]); expect(result).to.eql({ user: { userValue: 'customized' }, something: { userValue: 'else' } }); @@ -176,16 +135,17 @@ describe('ui settings', function () { }); describe('#getRaw()', function () { + it('pulls user configuration from ES', async function () { + const getResult = {}; + const { server, uiSettings, configGet } = instantiate({ getResult }); + const result = await uiSettings.getRaw(); + expectElasticsearchGetQuery(server, configGet); + }); + it(`without user configuration it's equal to the defaults`, async function () { const getResult = {}; const { server, uiSettings, configGet } = instantiate({ getResult }); const result = await uiSettings.getRaw(); - expect(server.plugins.elasticsearch.client.get.callCount).to.eql(1); - expect(server.plugins.elasticsearch.client.get.firstCall.args).to.eql([{ - index: configGet('kibana.index'), - id: configGet('pkg.version'), - type: 'config' - }]); expect(result).to.eql(defaultsProvider()); }); @@ -193,12 +153,6 @@ describe('ui settings', function () { const getResult = { foo: 'bar' }; const { server, uiSettings, configGet } = instantiate({ getResult }); const result = await uiSettings.getRaw(); - expect(server.plugins.elasticsearch.client.get.callCount).to.eql(1); - expect(server.plugins.elasticsearch.client.get.firstCall.args).to.eql([{ - index: configGet('kibana.index'), - id: configGet('pkg.version'), - type: 'config' - }]); const merged = defaultsProvider(); merged.foo = { userValue: 'bar' }; expect(result).to.eql(merged); @@ -208,12 +162,6 @@ describe('ui settings', function () { const getResult = { dateFormat: 'YYYY-MM-DD' }; const { server, uiSettings, configGet } = instantiate({ getResult }); const result = await uiSettings.getRaw(); - expect(server.plugins.elasticsearch.client.get.callCount).to.eql(1); - expect(server.plugins.elasticsearch.client.get.firstCall.args).to.eql([{ - index: configGet('kibana.index'), - id: configGet('pkg.version'), - type: 'config' - }]); const merged = defaultsProvider(); merged.dateFormat.userValue = 'YYYY-MM-DD'; expect(result).to.eql(merged); @@ -221,16 +169,17 @@ describe('ui settings', function () { }); describe('#getAll()', function () { + it('pulls user configuration from ES', async function () { + const getResult = {}; + const { server, uiSettings, configGet } = instantiate({ getResult }); + const result = await uiSettings.getAll(); + expectElasticsearchGetQuery(server, configGet); + }); + it(`returns key value pairs`, async function () { const getResult = {}; const { server, uiSettings, configGet } = instantiate({ getResult }); const result = await uiSettings.getAll(); - expect(server.plugins.elasticsearch.client.get.callCount).to.eql(1); - expect(server.plugins.elasticsearch.client.get.firstCall.args).to.eql([{ - index: configGet('kibana.index'), - id: configGet('pkg.version'), - type: 'config' - }]); const defaults = defaultsProvider(); const expectation = {}; Object.keys(defaults).forEach(key => { @@ -243,12 +192,6 @@ describe('ui settings', function () { const getResult = { something: 'user-provided' }; const { server, uiSettings, configGet } = instantiate({ getResult }); const result = await uiSettings.getAll(); - expect(server.plugins.elasticsearch.client.get.callCount).to.eql(1); - expect(server.plugins.elasticsearch.client.get.firstCall.args).to.eql([{ - index: configGet('kibana.index'), - id: configGet('pkg.version'), - type: 'config' - }]); const defaults = defaultsProvider(); const expectation = {}; Object.keys(defaults).forEach(key => { @@ -262,12 +205,6 @@ describe('ui settings', function () { const getResult = { dateFormat: 'YYYY-MM-DD' }; const { server, uiSettings, configGet } = instantiate({ getResult }); const result = await uiSettings.getAll(); - expect(server.plugins.elasticsearch.client.get.callCount).to.eql(1); - expect(server.plugins.elasticsearch.client.get.firstCall.args).to.eql([{ - index: configGet('kibana.index'), - id: configGet('pkg.version'), - type: 'config' - }]); const defaults = defaultsProvider(); const expectation = {}; Object.keys(defaults).forEach(key => { @@ -279,16 +216,17 @@ describe('ui settings', function () { }); describe('#get()', function () { + it('pulls user configuration from ES', async function () { + const getResult = {}; + const { server, uiSettings, configGet } = instantiate({ getResult }); + const result = await uiSettings.get(); + expectElasticsearchGetQuery(server, configGet); + }); + it(`returns the promised value for a key`, async function () { const getResult = {}; const { server, uiSettings, configGet } = instantiate({ getResult }); const result = await uiSettings.get('dateFormat'); - expect(server.plugins.elasticsearch.client.get.callCount).to.eql(1); - expect(server.plugins.elasticsearch.client.get.firstCall.args).to.eql([{ - index: configGet('kibana.index'), - id: configGet('pkg.version'), - type: 'config' - }]); const defaults = defaultsProvider(); expect(result).to.eql(defaults.dateFormat.value); }); @@ -297,12 +235,6 @@ describe('ui settings', function () { const getResult = { custom: 'value' }; const { server, uiSettings, configGet } = instantiate({ getResult }); const result = await uiSettings.get('custom'); - expect(server.plugins.elasticsearch.client.get.callCount).to.eql(1); - expect(server.plugins.elasticsearch.client.get.firstCall.args).to.eql([{ - index: configGet('kibana.index'), - id: configGet('pkg.version'), - type: 'config' - }]); expect(result).to.eql('value'); }); @@ -310,17 +242,30 @@ describe('ui settings', function () { const getResult = { dateFormat: 'YYYY-MM-DD' }; const { server, uiSettings, configGet } = instantiate({ getResult }); const result = await uiSettings.get('dateFormat'); - expect(server.plugins.elasticsearch.client.get.callCount).to.eql(1); - expect(server.plugins.elasticsearch.client.get.firstCall.args).to.eql([{ - index: configGet('kibana.index'), - id: configGet('pkg.version'), - type: 'config' - }]); expect(result).to.eql('YYYY-MM-DD'); }); }); }); +function expectElasticsearchGetQuery(server, configGet) { + expect(server.plugins.elasticsearch.client.get.callCount).to.eql(1); + expect(server.plugins.elasticsearch.client.get.firstCall.args).to.eql([{ + index: configGet('kibana.index'), + id: configGet('pkg.version'), + type: 'config' + }]); +} + +function expectElasticsearchUpdateQuery(server, configGet, doc) { + expect(server.plugins.elasticsearch.client.update.callCount).to.eql(1); + expect(server.plugins.elasticsearch.client.update.firstCall.args).to.eql([{ + index: configGet('kibana.index'), + id: configGet('pkg.version'), + type: 'config', + body: { doc } + }]); +} + function instantiate({ getResult } = {}) { const esStatus = { state: 'green', From 308ee58c261fc78d4eb998f51a0e5722bd57c02e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Bevacqua?= Date: Mon, 4 Jul 2016 16:36:19 -0300 Subject: [PATCH 08/12] [test] Semantic fix to compare against Promise. --- src/ui/settings/__tests__/index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ui/settings/__tests__/index.js b/src/ui/settings/__tests__/index.js index f8dc0be4fd06e..b8e59ddb7eca6 100644 --- a/src/ui/settings/__tests__/index.js +++ b/src/ui/settings/__tests__/index.js @@ -23,7 +23,7 @@ describe('ui settings', function () { it('returns a promise', () => { const { uiSettings } = instantiate(); const result = uiSettings.setMany({ a: 'b' }); - expect(typeof result.then).to.be('function'); + expect(result).to.be.a(Promise); }); it('updates a single value in one operation', function () { @@ -47,7 +47,7 @@ describe('ui settings', function () { it('returns a promise', () => { const { uiSettings } = instantiate(); const result = uiSettings.set('a', 'b'); - expect(typeof result.then).to.be('function'); + expect(result).to.be.a(Promise); }); it('updates single values by (key, value)', function () { @@ -63,7 +63,7 @@ describe('ui settings', function () { it('returns a promise', () => { const { uiSettings } = instantiate(); const result = uiSettings.remove('one'); - expect(typeof result.then).to.be('function'); + expect(result).to.be.a(Promise); }); it('removes single values by key', function () { @@ -79,7 +79,7 @@ describe('ui settings', function () { it('returns a promise', () => { const { uiSettings } = instantiate(); const result = uiSettings.removeMany(['one']); - expect(typeof result.then).to.be('function'); + expect(result).to.be.a(Promise); }); it('removes a single value', function () { From af0d592817a75051f2551ae11db1c782bd11f824 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Bevacqua?= Date: Tue, 5 Jul 2016 11:38:09 -0300 Subject: [PATCH 09/12] [chore] Use isEqual and add comments about the methods in uiSettings. --- src/ui/settings/__tests__/index.js | 41 +++++++++++++++--------------- src/ui/settings/index.js | 18 ++++++++++--- 2 files changed, 35 insertions(+), 24 deletions(-) diff --git a/src/ui/settings/__tests__/index.js b/src/ui/settings/__tests__/index.js index b8e59ddb7eca6..e1f687babe733 100644 --- a/src/ui/settings/__tests__/index.js +++ b/src/ui/settings/__tests__/index.js @@ -1,3 +1,4 @@ +import { isEqual } from 'lodash'; import sinon from 'sinon'; import expect from 'expect.js'; import init from '..'; @@ -103,7 +104,7 @@ describe('ui settings', function () { it('is promised the default values', async function () { const { server, uiSettings, configGet } = instantiate(); const defaults = await uiSettings.getDefaults(); - expect(defaults).eql(defaultsProvider()); + expect(isEqual(defaults, defaultsProvider())); }); }); @@ -119,18 +120,18 @@ describe('ui settings', function () { const getResult = { user: 'customized' }; const { server, uiSettings, configGet } = instantiate({ getResult }); const result = await uiSettings.getUserProvided(); - expect(result).to.eql({ + expect(isEqual(result, { user: { userValue: 'customized' } - }); + })); }); it('ignores null user configuration (because default values)', async function () { const getResult = { user: 'customized', usingDefault: null, something: 'else' }; const { server, uiSettings, configGet } = instantiate({ getResult }); const result = await uiSettings.getUserProvided(); - expect(result).to.eql({ + expect(isEqual(result, { user: { userValue: 'customized' }, something: { userValue: 'else' } - }); + })); }); }); @@ -146,7 +147,7 @@ describe('ui settings', function () { const getResult = {}; const { server, uiSettings, configGet } = instantiate({ getResult }); const result = await uiSettings.getRaw(); - expect(result).to.eql(defaultsProvider()); + expect(isEqual(result, defaultsProvider())); }); it(`user configuration gets merged with defaults`, async function () { @@ -155,7 +156,7 @@ describe('ui settings', function () { const result = await uiSettings.getRaw(); const merged = defaultsProvider(); merged.foo = { userValue: 'bar' }; - expect(result).to.eql(merged); + expect(isEqual(result, merged)); }); it(`user configuration gets merged into defaults`, async function () { @@ -164,7 +165,7 @@ describe('ui settings', function () { const result = await uiSettings.getRaw(); const merged = defaultsProvider(); merged.dateFormat.userValue = 'YYYY-MM-DD'; - expect(result).to.eql(merged); + expect(isEqual(result, merged)); }); }); @@ -185,7 +186,7 @@ describe('ui settings', function () { Object.keys(defaults).forEach(key => { expectation[key] = defaults[key].value; }); - expect(result).to.eql(expectation); + expect(isEqual(result, expectation)); }); it(`returns key value pairs including user configuration`, async function () { @@ -198,7 +199,7 @@ describe('ui settings', function () { expectation[key] = defaults[key].value; }); expectation.something = 'user-provided'; - expect(result).to.eql(expectation); + expect(isEqual(result, expectation)); }); it(`returns key value pairs including user configuration for existing settings`, async function () { @@ -211,7 +212,7 @@ describe('ui settings', function () { expectation[key] = defaults[key].value; }); expectation.dateFormat = 'YYYY-MM-DD'; - expect(result).to.eql(expectation); + expect(isEqual(result, expectation)); }); }); @@ -228,42 +229,42 @@ describe('ui settings', function () { const { server, uiSettings, configGet } = instantiate({ getResult }); const result = await uiSettings.get('dateFormat'); const defaults = defaultsProvider(); - expect(result).to.eql(defaults.dateFormat.value); + expect(isEqual(result, defaults.dateFormat.value)); }); it(`returns the user-configured value for a custom key`, async function () { const getResult = { custom: 'value' }; const { server, uiSettings, configGet } = instantiate({ getResult }); const result = await uiSettings.get('custom'); - expect(result).to.eql('value'); + expect(isEqual(result, 'value')); }); it(`returns the user-configured value for a modified key`, async function () { const getResult = { dateFormat: 'YYYY-MM-DD' }; const { server, uiSettings, configGet } = instantiate({ getResult }); const result = await uiSettings.get('dateFormat'); - expect(result).to.eql('YYYY-MM-DD'); + expect(isEqual(result, 'YYYY-MM-DD')); }); }); }); function expectElasticsearchGetQuery(server, configGet) { - expect(server.plugins.elasticsearch.client.get.callCount).to.eql(1); - expect(server.plugins.elasticsearch.client.get.firstCall.args).to.eql([{ + expect(isEqual(server.plugins.elasticsearch.client.get.callCount, 1)); + expect(isEqual(server.plugins.elasticsearch.client.get.firstCall.args, [{ index: configGet('kibana.index'), id: configGet('pkg.version'), type: 'config' - }]); + }])); } function expectElasticsearchUpdateQuery(server, configGet, doc) { - expect(server.plugins.elasticsearch.client.update.callCount).to.eql(1); - expect(server.plugins.elasticsearch.client.update.firstCall.args).to.eql([{ + expect(isEqual(server.plugins.elasticsearch.client.update.callCount, 1)); + expect(isEqual(server.plugins.elasticsearch.client.update.firstCall.args, [{ index: configGet('kibana.index'), id: configGet('pkg.version'), type: 'config', body: { doc } - }]); + }])); } function instantiate({ getResult } = {}) { diff --git a/src/ui/settings/index.js b/src/ui/settings/index.js index 0cf2aac900443..3709e48c8124b 100644 --- a/src/ui/settings/index.js +++ b/src/ui/settings/index.js @@ -4,15 +4,25 @@ import defaultsProvider from './defaults'; export default function setupSettings(kbnServer, server, config) { const status = kbnServer.status.create('ui settings'); const uiSettings = { + // returns a Promise for the value of the requested setting get, + // returns a Promise for a hash of setting key/value pairs getAll, - getRaw, - getDefaults, - getUserProvided, + // .set(key, value), returns a Promise for persisting the new value to ES set, + // takes a key/value hash, returns a Promise for persisting the new values to ES setMany, + // returns a Promise for removing the provided key from user-specific settings remove, - removeMany + // takes an array, returns a Promise for removing every provided key from user-specific settings + removeMany, + + // returns a Promise for the default settings, follows metadata format (see ./defaults) + getDefaults, + // returns a Promise for user-specific settings stored in ES, follows metadata format + getUserProvided, + // returns a Promise merging results of getDefaults & getUserProvided, follows metadata format + getRaw }; server.decorate('server', 'uiSettings', () => uiSettings); From 1673b31e490fb1f30a101f4212bce99c7631341c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Bevacqua?= Date: Tue, 5 Jul 2016 11:48:53 -0300 Subject: [PATCH 10/12] [test] .to.be.ok(), ugh. --- src/ui/settings/__tests__/index.js | 32 +++++++++++++++--------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/ui/settings/__tests__/index.js b/src/ui/settings/__tests__/index.js index e1f687babe733..19fad9002fcb0 100644 --- a/src/ui/settings/__tests__/index.js +++ b/src/ui/settings/__tests__/index.js @@ -104,7 +104,7 @@ describe('ui settings', function () { it('is promised the default values', async function () { const { server, uiSettings, configGet } = instantiate(); const defaults = await uiSettings.getDefaults(); - expect(isEqual(defaults, defaultsProvider())); + expect(isEqual(defaults, defaultsProvider())).to.be.ok(); }); }); @@ -122,7 +122,7 @@ describe('ui settings', function () { const result = await uiSettings.getUserProvided(); expect(isEqual(result, { user: { userValue: 'customized' } - })); + })).to.be.ok(); }); it('ignores null user configuration (because default values)', async function () { @@ -131,7 +131,7 @@ describe('ui settings', function () { const result = await uiSettings.getUserProvided(); expect(isEqual(result, { user: { userValue: 'customized' }, something: { userValue: 'else' } - })); + })).to.be.ok(); }); }); @@ -147,7 +147,7 @@ describe('ui settings', function () { const getResult = {}; const { server, uiSettings, configGet } = instantiate({ getResult }); const result = await uiSettings.getRaw(); - expect(isEqual(result, defaultsProvider())); + expect(isEqual(result, defaultsProvider())).to.be.ok(); }); it(`user configuration gets merged with defaults`, async function () { @@ -156,7 +156,7 @@ describe('ui settings', function () { const result = await uiSettings.getRaw(); const merged = defaultsProvider(); merged.foo = { userValue: 'bar' }; - expect(isEqual(result, merged)); + expect(isEqual(result, merged)).to.be.ok(); }); it(`user configuration gets merged into defaults`, async function () { @@ -165,7 +165,7 @@ describe('ui settings', function () { const result = await uiSettings.getRaw(); const merged = defaultsProvider(); merged.dateFormat.userValue = 'YYYY-MM-DD'; - expect(isEqual(result, merged)); + expect(isEqual(result, merged)).to.be.ok(); }); }); @@ -186,7 +186,7 @@ describe('ui settings', function () { Object.keys(defaults).forEach(key => { expectation[key] = defaults[key].value; }); - expect(isEqual(result, expectation)); + expect(isEqual(result, expectation)).to.be.ok(); }); it(`returns key value pairs including user configuration`, async function () { @@ -199,7 +199,7 @@ describe('ui settings', function () { expectation[key] = defaults[key].value; }); expectation.something = 'user-provided'; - expect(isEqual(result, expectation)); + expect(isEqual(result, expectation)).to.be.ok(); }); it(`returns key value pairs including user configuration for existing settings`, async function () { @@ -212,7 +212,7 @@ describe('ui settings', function () { expectation[key] = defaults[key].value; }); expectation.dateFormat = 'YYYY-MM-DD'; - expect(isEqual(result, expectation)); + expect(isEqual(result, expectation)).to.be.ok(); }); }); @@ -229,42 +229,42 @@ describe('ui settings', function () { const { server, uiSettings, configGet } = instantiate({ getResult }); const result = await uiSettings.get('dateFormat'); const defaults = defaultsProvider(); - expect(isEqual(result, defaults.dateFormat.value)); + expect(isEqual(result, defaults.dateFormat.value)).to.be.ok(); }); it(`returns the user-configured value for a custom key`, async function () { const getResult = { custom: 'value' }; const { server, uiSettings, configGet } = instantiate({ getResult }); const result = await uiSettings.get('custom'); - expect(isEqual(result, 'value')); + expect(isEqual(result, 'value')).to.be.ok(); }); it(`returns the user-configured value for a modified key`, async function () { const getResult = { dateFormat: 'YYYY-MM-DD' }; const { server, uiSettings, configGet } = instantiate({ getResult }); const result = await uiSettings.get('dateFormat'); - expect(isEqual(result, 'YYYY-MM-DD')); + expect(isEqual(result, 'YYYY-MM-DD')).to.be.ok(); }); }); }); function expectElasticsearchGetQuery(server, configGet) { - expect(isEqual(server.plugins.elasticsearch.client.get.callCount, 1)); + expect(isEqual(server.plugins.elasticsearch.client.get.callCount, 1)).to.be.ok(); expect(isEqual(server.plugins.elasticsearch.client.get.firstCall.args, [{ index: configGet('kibana.index'), id: configGet('pkg.version'), type: 'config' - }])); + }])).to.be.ok(); } function expectElasticsearchUpdateQuery(server, configGet, doc) { - expect(isEqual(server.plugins.elasticsearch.client.update.callCount, 1)); + expect(isEqual(server.plugins.elasticsearch.client.update.callCount, 1)).to.be.ok(); expect(isEqual(server.plugins.elasticsearch.client.update.firstCall.args, [{ index: configGet('kibana.index'), id: configGet('pkg.version'), type: 'config', body: { doc } - }])); + }])).to.be.ok(); } function instantiate({ getResult } = {}) { From a2d34b3a9dc5dccbe69003eeed3afe38cb3d4527 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Bevacqua?= Date: Thu, 7 Jul 2016 14:52:47 -0300 Subject: [PATCH 11/12] [test] Refactor according to @cjcenizal's suggestions. --- src/ui/settings/__tests__/index.js | 50 +++++++++++++++--------------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/src/ui/settings/__tests__/index.js b/src/ui/settings/__tests__/index.js index 19fad9002fcb0..7ec95e416744a 100644 --- a/src/ui/settings/__tests__/index.js +++ b/src/ui/settings/__tests__/index.js @@ -8,15 +8,15 @@ describe('ui settings', function () { describe('overview', function () { it('has expected api surface', function () { const { uiSettings } = instantiate(); - expect(typeof uiSettings.get).to.be('function'); - expect(typeof uiSettings.getAll).to.be('function'); - expect(typeof uiSettings.getDefaults).to.be('function'); - expect(typeof uiSettings.getRaw).to.be('function'); - expect(typeof uiSettings.getUserProvided).to.be('function'); - expect(typeof uiSettings.remove).to.be('function'); - expect(typeof uiSettings.removeMany).to.be('function'); - expect(typeof uiSettings.set).to.be('function'); - expect(typeof uiSettings.setMany).to.be('function'); + expect(typeof uiSettings.get).to.equal('function'); + expect(typeof uiSettings.getAll).to.equal('function'); + expect(typeof uiSettings.getDefaults).to.equal('function'); + expect(typeof uiSettings.getRaw).to.equal('function'); + expect(typeof uiSettings.getUserProvided).to.equal('function'); + expect(typeof uiSettings.remove).to.equal('function'); + expect(typeof uiSettings.removeMany).to.equal('function'); + expect(typeof uiSettings.set).to.equal('function'); + expect(typeof uiSettings.setMany).to.equal('function'); }); }); @@ -104,7 +104,7 @@ describe('ui settings', function () { it('is promised the default values', async function () { const { server, uiSettings, configGet } = instantiate(); const defaults = await uiSettings.getDefaults(); - expect(isEqual(defaults, defaultsProvider())).to.be.ok(); + expect(isEqual(defaults, defaultsProvider())).to.be.true(); }); }); @@ -122,7 +122,7 @@ describe('ui settings', function () { const result = await uiSettings.getUserProvided(); expect(isEqual(result, { user: { userValue: 'customized' } - })).to.be.ok(); + })).to.be.true(); }); it('ignores null user configuration (because default values)', async function () { @@ -131,7 +131,7 @@ describe('ui settings', function () { const result = await uiSettings.getUserProvided(); expect(isEqual(result, { user: { userValue: 'customized' }, something: { userValue: 'else' } - })).to.be.ok(); + })).to.be.true(); }); }); @@ -147,7 +147,7 @@ describe('ui settings', function () { const getResult = {}; const { server, uiSettings, configGet } = instantiate({ getResult }); const result = await uiSettings.getRaw(); - expect(isEqual(result, defaultsProvider())).to.be.ok(); + expect(isEqual(result, defaultsProvider())).to.be.true(); }); it(`user configuration gets merged with defaults`, async function () { @@ -156,7 +156,7 @@ describe('ui settings', function () { const result = await uiSettings.getRaw(); const merged = defaultsProvider(); merged.foo = { userValue: 'bar' }; - expect(isEqual(result, merged)).to.be.ok(); + expect(isEqual(result, merged)).to.be.true(); }); it(`user configuration gets merged into defaults`, async function () { @@ -165,7 +165,7 @@ describe('ui settings', function () { const result = await uiSettings.getRaw(); const merged = defaultsProvider(); merged.dateFormat.userValue = 'YYYY-MM-DD'; - expect(isEqual(result, merged)).to.be.ok(); + expect(isEqual(result, merged)).to.be.true(); }); }); @@ -186,7 +186,7 @@ describe('ui settings', function () { Object.keys(defaults).forEach(key => { expectation[key] = defaults[key].value; }); - expect(isEqual(result, expectation)).to.be.ok(); + expect(isEqual(result, expectation)).to.be.true(); }); it(`returns key value pairs including user configuration`, async function () { @@ -199,7 +199,7 @@ describe('ui settings', function () { expectation[key] = defaults[key].value; }); expectation.something = 'user-provided'; - expect(isEqual(result, expectation)).to.be.ok(); + expect(isEqual(result, expectation)).to.be.true(); }); it(`returns key value pairs including user configuration for existing settings`, async function () { @@ -212,7 +212,7 @@ describe('ui settings', function () { expectation[key] = defaults[key].value; }); expectation.dateFormat = 'YYYY-MM-DD'; - expect(isEqual(result, expectation)).to.be.ok(); + expect(isEqual(result, expectation)).to.be.true(); }); }); @@ -229,42 +229,42 @@ describe('ui settings', function () { const { server, uiSettings, configGet } = instantiate({ getResult }); const result = await uiSettings.get('dateFormat'); const defaults = defaultsProvider(); - expect(isEqual(result, defaults.dateFormat.value)).to.be.ok(); + expect(result).to.equal(defaults.dateFormat.value); }); it(`returns the user-configured value for a custom key`, async function () { const getResult = { custom: 'value' }; const { server, uiSettings, configGet } = instantiate({ getResult }); const result = await uiSettings.get('custom'); - expect(isEqual(result, 'value')).to.be.ok(); + expect(result).to.equal('value'); }); it(`returns the user-configured value for a modified key`, async function () { const getResult = { dateFormat: 'YYYY-MM-DD' }; const { server, uiSettings, configGet } = instantiate({ getResult }); const result = await uiSettings.get('dateFormat'); - expect(isEqual(result, 'YYYY-MM-DD')).to.be.ok(); + expect(result).to.equal('YYYY-MM-DD'); }); }); }); function expectElasticsearchGetQuery(server, configGet) { - expect(isEqual(server.plugins.elasticsearch.client.get.callCount, 1)).to.be.ok(); + expect(server.plugins.elasticsearch.client.get.callCount).to.equal(1); expect(isEqual(server.plugins.elasticsearch.client.get.firstCall.args, [{ index: configGet('kibana.index'), id: configGet('pkg.version'), type: 'config' - }])).to.be.ok(); + }])).to.be.true(); } function expectElasticsearchUpdateQuery(server, configGet, doc) { - expect(isEqual(server.plugins.elasticsearch.client.update.callCount, 1)).to.be.ok(); + expect(server.plugins.elasticsearch.client.update.callCount).to.equal(1); expect(isEqual(server.plugins.elasticsearch.client.update.firstCall.args, [{ index: configGet('kibana.index'), id: configGet('pkg.version'), type: 'config', body: { doc } - }])).to.be.ok(); + }])).to.be.true(); } function instantiate({ getResult } = {}) { From 9133c3ff03c6605cea3fe44101ce1656e6a8fd8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Bevacqua?= Date: Thu, 7 Jul 2016 15:20:16 -0300 Subject: [PATCH 12/12] [test] To equal true. --- src/ui/settings/__tests__/index.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/ui/settings/__tests__/index.js b/src/ui/settings/__tests__/index.js index 7ec95e416744a..2e09f4c00a4e9 100644 --- a/src/ui/settings/__tests__/index.js +++ b/src/ui/settings/__tests__/index.js @@ -104,7 +104,7 @@ describe('ui settings', function () { it('is promised the default values', async function () { const { server, uiSettings, configGet } = instantiate(); const defaults = await uiSettings.getDefaults(); - expect(isEqual(defaults, defaultsProvider())).to.be.true(); + expect(isEqual(defaults, defaultsProvider())).to.equal(true); }); }); @@ -122,7 +122,7 @@ describe('ui settings', function () { const result = await uiSettings.getUserProvided(); expect(isEqual(result, { user: { userValue: 'customized' } - })).to.be.true(); + })).to.equal(true); }); it('ignores null user configuration (because default values)', async function () { @@ -131,7 +131,7 @@ describe('ui settings', function () { const result = await uiSettings.getUserProvided(); expect(isEqual(result, { user: { userValue: 'customized' }, something: { userValue: 'else' } - })).to.be.true(); + })).to.equal(true); }); }); @@ -147,7 +147,7 @@ describe('ui settings', function () { const getResult = {}; const { server, uiSettings, configGet } = instantiate({ getResult }); const result = await uiSettings.getRaw(); - expect(isEqual(result, defaultsProvider())).to.be.true(); + expect(isEqual(result, defaultsProvider())).to.equal(true); }); it(`user configuration gets merged with defaults`, async function () { @@ -156,7 +156,7 @@ describe('ui settings', function () { const result = await uiSettings.getRaw(); const merged = defaultsProvider(); merged.foo = { userValue: 'bar' }; - expect(isEqual(result, merged)).to.be.true(); + expect(isEqual(result, merged)).to.equal(true); }); it(`user configuration gets merged into defaults`, async function () { @@ -165,7 +165,7 @@ describe('ui settings', function () { const result = await uiSettings.getRaw(); const merged = defaultsProvider(); merged.dateFormat.userValue = 'YYYY-MM-DD'; - expect(isEqual(result, merged)).to.be.true(); + expect(isEqual(result, merged)).to.equal(true); }); }); @@ -186,7 +186,7 @@ describe('ui settings', function () { Object.keys(defaults).forEach(key => { expectation[key] = defaults[key].value; }); - expect(isEqual(result, expectation)).to.be.true(); + expect(isEqual(result, expectation)).to.equal(true); }); it(`returns key value pairs including user configuration`, async function () { @@ -199,7 +199,7 @@ describe('ui settings', function () { expectation[key] = defaults[key].value; }); expectation.something = 'user-provided'; - expect(isEqual(result, expectation)).to.be.true(); + expect(isEqual(result, expectation)).to.equal(true); }); it(`returns key value pairs including user configuration for existing settings`, async function () { @@ -212,7 +212,7 @@ describe('ui settings', function () { expectation[key] = defaults[key].value; }); expectation.dateFormat = 'YYYY-MM-DD'; - expect(isEqual(result, expectation)).to.be.true(); + expect(isEqual(result, expectation)).to.equal(true); }); }); @@ -254,7 +254,7 @@ function expectElasticsearchGetQuery(server, configGet) { index: configGet('kibana.index'), id: configGet('pkg.version'), type: 'config' - }])).to.be.true(); + }])).to.equal(true); } function expectElasticsearchUpdateQuery(server, configGet, doc) { @@ -264,7 +264,7 @@ function expectElasticsearchUpdateQuery(server, configGet, doc) { id: configGet('pkg.version'), type: 'config', body: { doc } - }])).to.be.true(); + }])).to.equal(true); } function instantiate({ getResult } = {}) {