diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-04-22 11:56:34 +0200 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-04-22 11:56:34 +0200 |
commit | 4f86a9978b4b27b12e40ab0b5b9e18b8e0615266 (patch) | |
tree | 8c955eaf8f96da8a2dd811a75afffaf17dcc4fbc /lib/models | |
parent | cc78b880cef726201f1670b5c9f7f6f3512c2dbe (diff) | |
download | gitbook-4f86a9978b4b27b12e40ab0b5b9e18b8e0615266.zip gitbook-4f86a9978b4b27b12e40ab0b5b9e18b8e0615266.tar.gz gitbook-4f86a9978b4b27b12e40ab0b5b9e18b8e0615266.tar.bz2 |
Add test for config
Diffstat (limited to 'lib/models')
-rw-r--r-- | lib/models/__tests__/config.js | 63 | ||||
-rw-r--r-- | lib/models/config.js | 53 |
2 files changed, 112 insertions, 4 deletions
diff --git a/lib/models/__tests__/config.js b/lib/models/__tests__/config.js new file mode 100644 index 0000000..8445cef --- /dev/null +++ b/lib/models/__tests__/config.js @@ -0,0 +1,63 @@ +jest.autoMockOff(); + +var Immutable = require('immutable'); + +describe('Config', function() { + var Config = require('../config'); + + var config = Config.createWithValues({ + hello: { + world: 1, + test: 'Hello', + isFalse: false + } + }); + + describe('getValue', function() { + it('must return value as immutable', function() { + var value = config.getValue('hello'); + expect(Immutable.Map.isMap(value)).toBeTruthy(); + }); + + it('must return deep value', function() { + var value = config.getValue('hello.world'); + expect(value).toBe(1); + }); + + it('must return default value if non existant', function() { + var value = config.getValue('hello.nonExistant', 'defaultValue'); + expect(value).toBe('defaultValue'); + }); + + it('must not return default value for falsy values', function() { + var value = config.getValue('hello.isFalse', 'defaultValue'); + expect(value).toBe(false); + }); + }); + + describe('setValue', function() { + it('must set value as immutable', function() { + var testConfig = config.setValue('hello', { + 'cool': 1 + }); + var value = testConfig.getValue('hello'); + + expect(Immutable.Map.isMap(value)).toBeTruthy(); + expect(value.size).toBe(1); + expect(value.has('cool')).toBeTruthy(); + }); + + it('must set deep value', function() { + var testConfig = config.setValue('hello.world', 2); + var hello = testConfig.getValue('hello'); + var world = testConfig.getValue('hello.world'); + + expect(Immutable.Map.isMap(hello)).toBeTruthy(); + expect(hello.size).toBe(3); + + expect(world).toBe(2); + }); + }); +}); + + diff --git a/lib/models/config.js b/lib/models/config.js index fd4201d..e32ccdc 100644 --- a/lib/models/config.js +++ b/lib/models/config.js @@ -6,7 +6,7 @@ var File = require('./file'); var Config = Immutable.Record({ file: File(), values: Immutable.Map() -}); +}, 'Config'); Config.prototype.getPath = function() { return this.get('path'); @@ -24,13 +24,35 @@ Config.prototype.getValues = function() { */ Config.prototype.getValue = function(keyPath, def) { var values = this.getValues(); - if (is.string(keyPath)) keyPath = keyPath.split('.'); + keyPath = Config.keyToKeyPath(keyPath); + + if (!values.hasIn(keyPath)) { + return def; + } + + return values.getIn(keyPath); +}; + +/** + Update a configuration value + + @param {String} key + @param {Mixed} value + @return {Mixed} +*/ +Config.prototype.setValue = function(keyPath, value) { + keyPath = Config.keyToKeyPath(keyPath); + + value = Immutable.fromJS(value); - return values.getIn(keyPath) || def; + var values = this.getValues(); + values = values.setIn(keyPath, value); + + return this.set('values', values); }; /** - Create a new config, throw error if invalid + Create a new config for a file @param {File} file @param {Object} values @@ -43,5 +65,28 @@ Config.create = function(file, values) { }); }; +/** + Create a new config + + @param {Object} values + @returns {Config} +*/ +Config.createWithValues = function(values) { + return new Config({ + values: Immutable.fromJS(values) + }); +}; + + +/** + Convert a keyPath to an array of keys + + @param {String|Array} + @return {Array} +*/ +Config.keyToKeyPath = function(keyPath) { + if (is.string(keyPath)) keyPath = keyPath.split('.'); + return keyPath; +} module.exports = Config; |