diff --git a/src/plugins/kibana/public/settings/__tests__/to_editable_config.js b/src/plugins/kibana/public/settings/__tests__/to_editable_config.js new file mode 100644 index 000000000000..7a9a78d34f0a --- /dev/null +++ b/src/plugins/kibana/public/settings/__tests__/to_editable_config.js @@ -0,0 +1,86 @@ + +var toEditableConfig = require('plugins/kibana/settings/sections/advanced/lib/to_editable_config'); +var expect = require('expect.js'); + +describe('Settings', function () { + describe('Advanced', function () { + describe('toEditableConfig(def, name, value)', function () { + it('sets name', function () { + expect(invoke({ name: 'who' }).name).to.equal('who'); + }); + + it('sets value', function () { + expect(invoke({ value: 'what' }).value).to.equal('what'); + }); + + it('sets type', function () { + expect(invoke({ value: 'what' }).type).to.be('string'); + expect(invoke({ value: 0 }).type).to.be('number'); + expect(invoke({ value: [] }).type).to.be('array'); + }); + + context('when given a setting definition object', function () { + var def; + beforeEach(function () { + def = { + value: 'the original', + description: 'the one and only', + options: 'all the options' + }; + }); + + it('is not marked as custom', function () { + expect(invoke({ def }).isCustom).to.be.false; + }); + + it('sets a default value', function () { + expect(invoke({ def }).defVal).to.equal(def.value); + }); + + it('sets a description', function () { + expect(invoke({ def }).description).to.equal(def.description); + }); + + it('sets options', function () { + expect(invoke({ def }).options).to.equal(def.options); + }); + + context('that contains a type', function () { + it('sets that type', function () { + def.type = 'something'; + expect(invoke({ def }).type).to.equal(def.type); + }); + }); + + context('that contains a value of type array', function () { + it('sets type to array', function () { + def.value = []; + expect(invoke({ def }).type).to.equal('array'); + }); + }); + }); + + context('when not given a setting definition object', function () { + it('is marked as custom', function () { + expect(invoke().isCustom).to.be.true; + }); + + it('sets defVal to undefined', function () { + expect(invoke().defVal).to.be.undefined; + }); + + it('sets description to undefined', function () { + expect(invoke().description).to.be.undefined; + }); + + it('sets options to undefined', function () { + expect(invoke().options).to.be.undefined; + }); + }); + }); + }); +}); + +function invoke({ def = false, name = 'woah', value = 'forreal' } = {}) { + return toEditableConfig(def, name, value); +} diff --git a/src/plugins/kibana/public/settings/sections/advanced/index.js b/src/plugins/kibana/public/settings/sections/advanced/index.js index 9e604846e088..3f1918a37096 100644 --- a/src/plugins/kibana/public/settings/sections/advanced/index.js +++ b/src/plugins/kibana/public/settings/sections/advanced/index.js @@ -1,7 +1,6 @@ define(function (require) { var _ = require('lodash'); - var getValType = require('plugins/kibana/settings/sections/advanced/lib/get_val_type'); - var getEditorType = require('plugins/kibana/settings/sections/advanced/lib/get_editor_type'); + var toEditableConfig = require('plugins/kibana/settings/sections/advanced/lib/to_editable_config'); require('plugins/kibana/settings/sections/advanced/advanced_row'); @@ -35,31 +34,6 @@ define(function (require) { return !_.contains(IMMUTABLE_CONFIG_VALS, configName); } - function toEditableConfig(def, name, value) { - var isCustom = !def; - if (isCustom) def = {}; - - var conf = { - name, - value, - isCustom, - defVal: def.value, - type: getValType(def, value), - description: def.description, - options: def.options - }; - - var editor = getEditorType(conf); - conf.json = editor === 'json'; - conf.select = editor === 'select'; - conf.bool = editor === 'boolean'; - conf.array = editor === 'array'; - conf.normal = editor === 'normal'; - conf.tooComplex = !editor; - - return conf; - } - function readConfigVals() { var configVals = config._vals(); diff --git a/src/plugins/kibana/public/settings/sections/advanced/lib/to_editable_config.js b/src/plugins/kibana/public/settings/sections/advanced/lib/to_editable_config.js new file mode 100644 index 000000000000..5b0bb8ea22bf --- /dev/null +++ b/src/plugins/kibana/public/settings/sections/advanced/lib/to_editable_config.js @@ -0,0 +1,38 @@ +define(function (require) { + var _ = require('lodash'); + var getValType = require('./get_val_type'); + var getEditorType = require('./get_editor_type'); + + /** + * @param {object} advanced setting definition object + * @param {object} name of setting + * @param {object} current value of setting + * @returns {object} the editable config object + */ + function toEditableConfig(def, name, value) { + var isCustom = !def; + if (isCustom) def = {}; + + var conf = { + name, + value, + isCustom, + defVal: def.value, + type: getValType(def, value), + description: def.description, + options: def.options + }; + + var editor = getEditorType(conf); + conf.json = editor === 'json'; + conf.select = editor === 'select'; + conf.bool = editor === 'boolean'; + conf.array = editor === 'array'; + conf.normal = editor === 'normal'; + conf.tooComplex = !editor; + + return conf; + } + + return toEditableConfig; +});