Move editable config creation to utility file

The function is now unit tested as well. There are a few flags of the
resulting configuration object that are not yet tested, but they're
really behaviors of the returned value based on the current state rather
than behaviors of toEditableConfig itself.
This commit is contained in:
Court Ewing 2015-09-11 13:51:48 -04:00
parent 3932bd00d5
commit 120a2a7f54
3 changed files with 125 additions and 27 deletions

View file

@ -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);
}

View file

@ -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();

View file

@ -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;
});