From 924cc1fcf7de1896ac09bbe7a400d5ff5df0b50d Mon Sep 17 00:00:00 2001 From: Whymarrh Whitby Date: Thu, 19 Apr 2018 01:03:51 -0230 Subject: Move setAccountLabel into PreferencesController --- test/unit/actions/save_account_label_test.js | 35 ------------------- test/unit/actions/set_account_label_test.js | 34 +++++++++++++++++++ test/unit/preferences-controller-test.js | 51 +++++++++++++++++++++++++++- 3 files changed, 84 insertions(+), 36 deletions(-) delete mode 100644 test/unit/actions/save_account_label_test.js create mode 100644 test/unit/actions/set_account_label_test.js (limited to 'test/unit') diff --git a/test/unit/actions/save_account_label_test.js b/test/unit/actions/save_account_label_test.js deleted file mode 100644 index c5ffd6cbf..000000000 --- a/test/unit/actions/save_account_label_test.js +++ /dev/null @@ -1,35 +0,0 @@ -// var jsdom = require('mocha-jsdom') -var assert = require('assert') -var freeze = require('deep-freeze-strict') -var path = require('path') - -var actions = require(path.join(__dirname, '..', '..', '..', 'ui', 'app', 'actions.js')) -var reducers = require(path.join(__dirname, '..', '..', '..', 'ui', 'app', 'reducers.js')) - -describe('SAVE_ACCOUNT_LABEL', function () { - it('updates the state.metamask.identities[:i].name property of the state to the action.value.label', function () { - var initialState = { - metamask: { - identities: { - foo: { - name: 'bar', - }, - }, - }, - } - freeze(initialState) - - const action = { - type: actions.SAVE_ACCOUNT_LABEL, - value: { - account: 'foo', - label: 'baz', - }, - } - freeze(action) - - var resultingState = reducers(initialState, action) - assert.equal(resultingState.metamask.identities.foo.name, action.value.label) - }) -}) - diff --git a/test/unit/actions/set_account_label_test.js b/test/unit/actions/set_account_label_test.js new file mode 100644 index 000000000..53ea1d130 --- /dev/null +++ b/test/unit/actions/set_account_label_test.js @@ -0,0 +1,34 @@ +const assert = require('assert') +const freeze = require('deep-freeze-strict') +const path = require('path') + +const actions = require(path.join(__dirname, '..', '..', '..', 'ui', 'app', 'actions.js')) +const reducers = require(path.join(__dirname, '..', '..', '..', 'ui', 'app', 'reducers.js')) + +describe('SET_ACCOUNT_LABEL', function () { + it('updates the state.metamask.identities[:i].name property of the state to the action.value.label', function () { + const initialState = { + metamask: { + identities: { + foo: { + name: 'bar', + }, + }, + }, + } + freeze(initialState) + + const action = { + type: actions.SET_ACCOUNT_LABEL, + value: { + account: 'foo', + label: 'baz', + }, + } + freeze(action) + + const resultingState = reducers(initialState, action) + assert.equal(resultingState.metamask.identities.foo.name, action.value.label) + }) +}) + diff --git a/test/unit/preferences-controller-test.js b/test/unit/preferences-controller-test.js index 9fb5e4251..8ff1c8648 100644 --- a/test/unit/preferences-controller-test.js +++ b/test/unit/preferences-controller-test.js @@ -4,16 +4,26 @@ const PreferencesController = require('../../app/scripts/controllers/preferences describe('preferences controller', function () { let preferencesController - before(() => { + beforeEach(() => { preferencesController = new PreferencesController() }) + describe('getTokens', function () { + it('should return an empty list initially', async function () { + await preferencesController.setSelectedAddress('0x7e57e2') + + const tokens = preferencesController.getTokens() + assert.equal(tokens.length, 0, 'empty list of tokens') + }) + }) + describe('addToken', function () { it('should add that token to its state', async function () { const address = '0xabcdef1234567' const symbol = 'ABBR' const decimals = 5 + await preferencesController.setSelectedAddress('0x7e57e2') await preferencesController.addToken(address, symbol, decimals) const tokens = preferencesController.getTokens() @@ -30,6 +40,7 @@ describe('preferences controller', function () { const symbol = 'ABBR' const decimals = 5 + await preferencesController.setSelectedAddress('0x7e57e2') await preferencesController.addToken(address, symbol, decimals) const newDecimals = 6 @@ -43,6 +54,44 @@ describe('preferences controller', function () { assert.equal(added.symbol, symbol, 'set symbol correctly') assert.equal(added.decimals, newDecimals, 'updated decimals correctly') }) + + it('should allow adding tokens to two separate addresses', async function () { + const address = '0xabcdef1234567' + const symbol = 'ABBR' + const decimals = 5 + + await preferencesController.setSelectedAddress('0x7e57e2') + await preferencesController.addToken(address, symbol, decimals) + assert.equal(preferencesController.getTokens().length, 1, 'one token added for 1st address') + + await preferencesController.setSelectedAddress('0xda22le') + await preferencesController.addToken(address, symbol, decimals) + assert.equal(preferencesController.getTokens().length, 1, 'one token added for 2nd address') + }) + }) + + describe('removeToken', function () { + it('should remove the only token from its state', async function () { + await preferencesController.setSelectedAddress('0x7e57e2') + await preferencesController.addToken('0xa', 'A', 5) + await preferencesController.removeToken('0xa') + + const tokens = preferencesController.getTokens() + assert.equal(tokens.length, 0, 'one token removed') + }) + + it('should remove a token from its state', async function () { + await preferencesController.setSelectedAddress('0x7e57e2') + await preferencesController.addToken('0xa', 'A', 4) + await preferencesController.addToken('0xb', 'B', 5) + await preferencesController.removeToken('0xa') + + const tokens = preferencesController.getTokens() + assert.equal(tokens.length, 1, 'one token removed') + + const [token1] = tokens + assert.deepEqual(token1, {address: '0xb', symbol: 'B', decimals: 5}) + }) }) }) -- cgit From cbe4d0d88c83ee1d8dd8efde537ee753bf19596a Mon Sep 17 00:00:00 2001 From: Whymarrh Whitby Date: Thu, 19 Apr 2018 01:03:51 -0230 Subject: Update AddressBookController to read from preferences store --- test/unit/address-book-controller.js | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) (limited to 'test/unit') diff --git a/test/unit/address-book-controller.js b/test/unit/address-book-controller.js index 655c9022c..e48e69d98 100644 --- a/test/unit/address-book-controller.js +++ b/test/unit/address-book-controller.js @@ -1,26 +1,26 @@ const assert = require('assert') const AddressBookController = require('../../app/scripts/controllers/address-book') -const mockKeyringController = { - memStore: { - getState: function () { - return { - identities: { - '0x0aaa': { - address: '0x0aaa', - name: 'owned', - }, +const stubPreferencesStore = { + getState: function () { + return { + identities: { + '0x0aaa': { + address: '0x0aaa', + name: 'owned', }, - } - }, + }, + } }, -} +}; describe('address-book-controller', function () { var addressBookController beforeEach(function () { - addressBookController = new AddressBookController({}, mockKeyringController) + addressBookController = new AddressBookController({ + preferencesStore: stubPreferencesStore, + }) }) describe('addres book management', function () { -- cgit From c54e4c719110c2033b7cc3757676f97ad3329d42 Mon Sep 17 00:00:00 2001 From: Whymarrh Whitby Date: Thu, 19 Apr 2018 01:03:51 -0230 Subject: Add PreferencesController#setAddresses to update ids --- test/unit/preferences-controller-test.js | 44 ++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'test/unit') diff --git a/test/unit/preferences-controller-test.js b/test/unit/preferences-controller-test.js index 8ff1c8648..4b40b1159 100644 --- a/test/unit/preferences-controller-test.js +++ b/test/unit/preferences-controller-test.js @@ -8,6 +8,50 @@ describe('preferences controller', function () { preferencesController = new PreferencesController() }) + describe('setAddresses', function () { + it('should keep a map of addresses to names and addresses in the store', function () { + preferencesController.setAddresses([ + '0xda22le', + '0x7e57e2', + ]) + + const {identities} = preferencesController.store.getState() + assert.deepEqual(identities, { + '0xda22le': { + name: 'Account 1', + address: '0xda22le', + }, + '0x7e57e2': { + name: 'Account 2', + address: '0x7e57e2', + }, + }) + }) + + it('should replace its list of addresses', function () { + preferencesController.setAddresses([ + '0xda22le', + '0x7e57e2', + ]) + preferencesController.setAddresses([ + '0xda22le77', + '0x7e57e277', + ]) + + const {identities} = preferencesController.store.getState() + assert.deepEqual(identities, { + '0xda22le77': { + name: 'Account 1', + address: '0xda22le77', + }, + '0x7e57e277': { + name: 'Account 2', + address: '0x7e57e277', + }, + }) + }) + }) + describe('getTokens', function () { it('should return an empty list initially', async function () { await preferencesController.setSelectedAddress('0x7e57e2') -- cgit From 50af02e74bf8967232b97a43db4f3befb6939566 Mon Sep 17 00:00:00 2001 From: Whymarrh Whitby Date: Thu, 19 Apr 2018 01:03:51 -0230 Subject: Add test case for PreferencesController#setAccountLabel --- test/unit/preferences-controller-test.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'test/unit') diff --git a/test/unit/preferences-controller-test.js b/test/unit/preferences-controller-test.js index 4b40b1159..c613c68f9 100644 --- a/test/unit/preferences-controller-test.js +++ b/test/unit/preferences-controller-test.js @@ -52,6 +52,27 @@ describe('preferences controller', function () { }) }) + describe('setAccountLabel', function () { + it('should update a label for the given account', function () { + preferencesController.setAddresses([ + '0xda22le', + '0x7e57e2', + ]) + + assert.deepEqual(preferencesController.store.getState().identities['0xda22le'], { + name: 'Account 1', + address: '0xda22le', + }) + + + preferencesController.setAccountLabel('0xda22le', 'Dazzle') + assert.deepEqual(preferencesController.store.getState().identities['0xda22le'], { + name: 'Dazzle', + address: '0xda22le', + }) + }) + }) + describe('getTokens', function () { it('should return an empty list initially', async function () { await preferencesController.setSelectedAddress('0x7e57e2') -- cgit From 2d13fac476cfbb7b0140611dca00fa95ee8d91ab Mon Sep 17 00:00:00 2001 From: Whymarrh Whitby Date: Thu, 19 Apr 2018 01:03:51 -0230 Subject: Add migration to move identities from KeyringController --- test/unit/migrations/026-test.js | 41 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 test/unit/migrations/026-test.js (limited to 'test/unit') diff --git a/test/unit/migrations/026-test.js b/test/unit/migrations/026-test.js new file mode 100644 index 000000000..ac0da3fb2 --- /dev/null +++ b/test/unit/migrations/026-test.js @@ -0,0 +1,41 @@ +const assert = require('assert') +const migration26 = require('../../../app/scripts/migrations/026') +const oldStorage = { + 'meta': {'version': 25}, + 'data': { + 'PreferencesController': {}, + 'KeyringController': { + 'walletNicknames': { + '0x1e77e2': 'Test Account 1', + '0x7e57e2': 'Test Account 2', + }, + }, + }, +} + +describe('migration #26', () => { + it('should move the identities from KeyringController', (done) => { + migration26.migrate(oldStorage) + .then((newStorage) => { + const identities = newStorage.data.PreferencesController.identities + assert.deepEqual(identities, { + '0x1e77e2': 'Test Account 1', + '0x7e57e2': 'Test Account 2', + }) + assert.strictEqual(newStorage.data.KeyringController.walletNicknames, undefined) + done() + }) + .catch(done) + }) + + it('should successfully migrate first time state', (done) => { + migration26.migrate({ + meta: {}, + data: require('../../../app/scripts/first-time-state'), + }) + .then((migratedData) => { + assert.equal(migratedData.meta.version, migration26.version) + done() + }).catch(done) + }) +}) -- cgit From e546f4b9041c03deb74ed115b8bcfe3869670b6e Mon Sep 17 00:00:00 2001 From: Whymarrh Whitby Date: Mon, 14 May 2018 17:16:24 -0230 Subject: Update MetaMaskController vault restoration test for new KC api --- test/unit/metamask-controller-test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'test/unit') diff --git a/test/unit/metamask-controller-test.js b/test/unit/metamask-controller-test.js index 18c3f9ab9..649b26e1e 100644 --- a/test/unit/metamask-controller-test.js +++ b/test/unit/metamask-controller-test.js @@ -106,7 +106,7 @@ describe('MetaMaskController', function () { [TEST_ADDRESS]: { address: TEST_ADDRESS, name: DEFAULT_LABEL }, }) - await metamaskController.keyringController.saveAccountLabel(TEST_ADDRESS, 'Account Foo') + await metamaskController.preferencesController.setAccountLabel(TEST_ADDRESS, 'Account Foo') assert.deepEqual(metamaskController.getState().identities, { [TEST_ADDRESS]: { address: TEST_ADDRESS, name: 'Account Foo' }, }) -- cgit From 67310e151ea75c7aa4fc00bfd63bf41cd4f2db51 Mon Sep 17 00:00:00 2001 From: Whymarrh Whitby Date: Thu, 17 May 2018 13:35:38 -0230 Subject: Fix migration 026 to produce the correct shape for state.identities --- test/unit/migrations/026-test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'test/unit') diff --git a/test/unit/migrations/026-test.js b/test/unit/migrations/026-test.js index ac0da3fb2..b3f5470cf 100644 --- a/test/unit/migrations/026-test.js +++ b/test/unit/migrations/026-test.js @@ -19,8 +19,8 @@ describe('migration #26', () => { .then((newStorage) => { const identities = newStorage.data.PreferencesController.identities assert.deepEqual(identities, { - '0x1e77e2': 'Test Account 1', - '0x7e57e2': 'Test Account 2', + '0x1e77e2': {name: 'Test Account 1', address: '0x1e77e2'}, + '0x7e57e2': {name: 'Test Account 2', address: '0x7e57e2'}, }) assert.strictEqual(newStorage.data.KeyringController.walletNicknames, undefined) done() -- cgit