From 4780f825b1bfb33f03c60133f293b122b8b43be4 Mon Sep 17 00:00:00 2001 From: bitpshr Date: Thu, 12 Apr 2018 23:26:50 -0400 Subject: Add ComposableObservableStore for subscription management --- test/setup.js | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 test/setup.js (limited to 'test') diff --git a/test/setup.js b/test/setup.js new file mode 100644 index 000000000..8e7965a37 --- /dev/null +++ b/test/setup.js @@ -0,0 +1,5 @@ +require('babel-register')({ + ignore: name => name.includes('node_modules') && !name.includes('obs-store'), +}) + +require('./helper') -- cgit From 8974f933fc97a37f5cd8dcd510ff0e6dc21d6751 Mon Sep 17 00:00:00 2001 From: bitpshr Date: Fri, 13 Apr 2018 13:13:36 -0400 Subject: Add tests for ComposableObservableStore --- test/unit/ComposableObservableStore.js | 35 ++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 test/unit/ComposableObservableStore.js (limited to 'test') 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' }) + }) +}) -- cgit From d0447f90583275868bb72aa7ae8f670bf3668173 Mon Sep 17 00:00:00 2001 From: bitpshr Date: Mon, 16 Apr 2018 11:21:06 -0400 Subject: Maintain token prices using a background service --- test/unit/token-rates-controller.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 test/unit/token-rates-controller.js (limited to 'test') diff --git a/test/unit/token-rates-controller.js b/test/unit/token-rates-controller.js new file mode 100644 index 000000000..55bfe7823 --- /dev/null +++ b/test/unit/token-rates-controller.js @@ -0,0 +1,28 @@ +const assert = require('assert') +const sinon = require('sinon') +const TokenRatesController = require('../../app/scripts/controllers/token-rates') +const ObservableStore = require('obs-store') + +describe('TokenRatesController', () => { + it('should listen for preferences store updates', () => { + const preferences = new ObservableStore({ tokens: [] }) + const controller = new TokenRatesController({ preferences }) + preferences.putState({ tokens: ['foo'] }) + assert.deepEqual(controller._tokens, ['foo']) + }) + + it('should poll on correct interval', async () => { + const stub = sinon.stub(global, 'setInterval') + new TokenRatesController({ interval: 1337 }) // eslint-disable-line no-new + assert.strictEqual(stub.getCall(0).args[1], 1337) + stub.restore() + }) + + it('should fetch each token rate based on address', async () => { + const controller = new TokenRatesController() + controller.fetchExchangeRate = address => address + controller.tokens = [{ address: 'foo' }, { address: 'bar' }] + await controller.updateExchangeRates() + assert.deepEqual(controller.store.getState().contractExchangeRates, { foo: 'foo', bar: 'bar' }) + }) +}) -- cgit From b4912f29cd3b6e0a5df81e9f69acc50b03e31c0c Mon Sep 17 00:00:00 2001 From: bitpshr Date: Mon, 16 Apr 2018 17:45:18 -0400 Subject: Disable token price polling when no client is active --- test/unit/token-rates-controller.js | 1 + 1 file changed, 1 insertion(+) (limited to 'test') diff --git a/test/unit/token-rates-controller.js b/test/unit/token-rates-controller.js index 55bfe7823..a49547313 100644 --- a/test/unit/token-rates-controller.js +++ b/test/unit/token-rates-controller.js @@ -20,6 +20,7 @@ describe('TokenRatesController', () => { it('should fetch each token rate based on address', async () => { const controller = new TokenRatesController() + controller.isActive = true controller.fetchExchangeRate = address => address controller.tokens = [{ address: 'foo' }, { address: 'bar' }] await controller.updateExchangeRates() -- cgit