diff --git a/src/settings.js b/src/settings.js index b9642c0dce..c235d7cc54 100644 --- a/src/settings.js +++ b/src/settings.js @@ -1,6 +1,7 @@ 'use strict'; var meta = require('./meta'); +var pubsub = require('./pubsub'); function expandObjBy(obj1, obj2) { var key; @@ -68,6 +69,8 @@ function Settings(hash, version, defCfg, callback, forceUpdate, reset) { this.hash = hash; this.version = version || this.version; this.defCfg = defCfg; + var self = this; + if (reset) { this.reset(callback); } else { @@ -75,6 +78,11 @@ function Settings(hash, version, defCfg, callback, forceUpdate, reset) { this.checkStructure(callback, forceUpdate); }); } + pubsub.on('action:settings.set.' + hash, function (data) { + try { + self.cfg._ = JSON.parse(data._); + } catch (err) {} + }); } Settings.prototype.hash = ''; diff --git a/test/search.js b/test/search.js index e3998e89c9..b38e5f2806 100644 --- a/test/search.js +++ b/test/search.js @@ -193,7 +193,6 @@ describe('Search', function () { query: 'xxxxxxxxxxxxxx', }, function (err, data) { assert.ifError(err); - console.log(data); assert(Array.isArray(data.posts)); assert(!data.matchCount); done(); diff --git a/test/settings.js b/test/settings.js new file mode 100644 index 0000000000..8435fdcd42 --- /dev/null +++ b/test/settings.js @@ -0,0 +1,38 @@ +'use strict'; + +var assert = require('assert'); +var nconf = require('nconf'); + +var db = require('./mocks/databasemock'); +var settings = require('../src/settings'); + +describe('settings v3', function () { + var settings1; + var settings2; + + it('should create a new settings object', function (done) { + settings1 = new settings('my-plugin', '1.0', { foo: 1, bar: { derp: 2 } }, done); + }); + + it('should get the saved settings ', function (done) { + assert.equal(settings1.get('foo'), 1); + assert.equal(settings1.get('bar.derp'), 2); + done(); + }); + + it('should create a new settings instance for same key', function (done) { + settings2 = new settings('my-plugin', '1.0', { foo: 1, bar: { derp: 2 } }, done); + }); + + it('should pass change between settings object over pubsub', function (done) { + settings1.set('foo', 3); + settings1.persist(function (err) { + assert.ifError(err); + // give pubsub time to complete + setTimeout(function () { + assert.equal(settings2.get('foo'), 3); + done(); + }, 500); + }); + }); +});