diff options
Add tests for ComposableObservableStore
-rw-r--r-- | app/scripts/lib/ComposableObservableStore.js | 22 | ||||
-rw-r--r-- | test/unit/ComposableObservableStore.js | 35 |
2 files changed, 46 insertions, 11 deletions
diff --git a/app/scripts/lib/ComposableObservableStore.js b/app/scripts/lib/ComposableObservableStore.js index 688594b6d..d5ee708a1 100644 --- a/app/scripts/lib/ComposableObservableStore.js +++ b/app/scripts/lib/ComposableObservableStore.js @@ -11,24 +11,24 @@ class ComposableObservableStore extends ObservableStore { * @param {Object} [initState] - The initial store state * @param {Object} [config] - Map of internal state keys to child stores */ - constructor (initState, config) { - super() - this.updateStructure(config) - } + constructor (initState, config) { + super(initState) + this.updateStructure(config) + } /** * Composes a new internal store subscription structure * * @param {Object} [config] - Map of internal state keys to child stores */ - updateStructure (config) { + updateStructure (config) { this.config = config - this.removeAllListeners() - for (const key in config) { - config[key].subscribe((state) => { - this.updateState({ [key]: state }) - }) - } + this.removeAllListeners() + for (const key in config) { + config[key].subscribe((state) => { + this.updateState({ [key]: state }) + }) + } } /** diff --git a/test/unit/ComposableObservableStore.js b/test/unit/ComposableObservableStore.js new file mode 100644 index 000000000..3fba200c1 --- /dev/null +++ b/test/unit/ComposableObservableStore.js @@ -0,0 +1,35 @@ +const assert = require('assert') +const ComposableObservableStore = require('../../app/scripts/lib/ComposableObservableStore') +const ObservableStore = require('obs-store') + +describe('ComposableObservableStore', () => { + it('should register initial state', () => { + const store = new ComposableObservableStore('state') + assert.strictEqual(store.getState(), 'state') + }) + + it('should register initial structure', () => { + const testStore = new ObservableStore() + const store = new ComposableObservableStore(null, { TestStore: testStore }) + testStore.putState('state') + assert.deepEqual(store.getState(), { TestStore: 'state' }) + }) + + it('should update structure', () => { + const testStore = new ObservableStore() + const store = new ComposableObservableStore() + store.updateStructure({ TestStore: testStore }) + testStore.putState('state') + assert.deepEqual(store.getState(), { TestStore: 'state' }) + }) + + it('should return flattened state', () => { + const fooStore = new ObservableStore({ foo: 'foo' }) + const barStore = new ObservableStore({ bar: 'bar' }) + const store = new ComposableObservableStore(null, { + FooStore: fooStore, + BarStore: barStore, + }) + assert.deepEqual(store.getFlatState(), { foo: 'foo', bar: 'bar' }) + }) +}) |