diff options
author | Thomas <thomas.b.huang@gmail.com> | 2018-08-15 01:44:42 +0800 |
---|---|---|
committer | Thomas <thomas.b.huang@gmail.com> | 2018-08-15 01:44:42 +0800 |
commit | 96d789d2cffa91da9d65be0de75d2e864d309305 (patch) | |
tree | b3cea0eb6a4638244fd99bf64509c69b0b720a7a /test/unit | |
parent | 7918240833871648dea6f9787519ba20f1e51899 (diff) | |
parent | 742aa8bb11c1273151ad907171061d4c5e4d1dca (diff) | |
download | tangerine-wallet-browser-96d789d2cffa91da9d65be0de75d2e864d309305.tar tangerine-wallet-browser-96d789d2cffa91da9d65be0de75d2e864d309305.tar.gz tangerine-wallet-browser-96d789d2cffa91da9d65be0de75d2e864d309305.tar.bz2 tangerine-wallet-browser-96d789d2cffa91da9d65be0de75d2e864d309305.tar.lz tangerine-wallet-browser-96d789d2cffa91da9d65be0de75d2e864d309305.tar.xz tangerine-wallet-browser-96d789d2cffa91da9d65be0de75d2e864d309305.tar.zst tangerine-wallet-browser-96d789d2cffa91da9d65be0de75d2e864d309305.zip |
Merge branch 'develop' into network-remove-provider-engine
Override package-lock and fix merge conflicts
Diffstat (limited to 'test/unit')
-rw-r--r-- | test/unit/app/controllers/detect-tokens-test.js | 35 | ||||
-rw-r--r-- | test/unit/app/controllers/preferences-controller-test.js | 158 | ||||
-rw-r--r-- | test/unit/migrations/028-test.js | 46 |
3 files changed, 223 insertions, 16 deletions
diff --git a/test/unit/app/controllers/detect-tokens-test.js b/test/unit/app/controllers/detect-tokens-test.js index 4ee73599d..d659d51df 100644 --- a/test/unit/app/controllers/detect-tokens-test.js +++ b/test/unit/app/controllers/detect-tokens-test.js @@ -1,16 +1,14 @@ const assert = require('assert') -const sinon = require('sinon') const nock = require('nock') +const sinon = require('sinon') const ObservableStore = require('obs-store') const DetectTokensController = require('../../../../app/scripts/controllers/detect-tokens') const NetworkController = require('../../../../app/scripts/controllers/network/network') const PreferencesController = require('../../../../app/scripts/controllers/preferences') describe('DetectTokensController', () => { - - let clock, network, preferences, controller, keyringMemStore - const sandbox = sinon.createSandbox() + let clock, keyringMemStore, network, preferences, controller const noop = () => {} @@ -20,13 +18,14 @@ describe('DetectTokensController', () => { beforeEach(async () => { + nock('https://api.infura.io') .get(/.*/) .reply(200) keyringMemStore = new ObservableStore({ isUnlocked: false}) network = new NetworkController() - preferences = new PreferencesController() + preferences = new PreferencesController({ network }) controller = new DetectTokensController({ preferences: preferences, network: network, keyringMemStore: keyringMemStore }) network.initializeProvider(networkControllerProviderConfig) @@ -34,8 +33,8 @@ describe('DetectTokensController', () => { }) after(() => { - sandbox.restore() - nock.cleanAll() + sandbox.restore() + nock.cleanAll() }) it('should poll on correct interval', async () => { @@ -50,7 +49,7 @@ describe('DetectTokensController', () => { const network = new NetworkController() network.initializeProvider(networkControllerProviderConfig) network.setProviderType('mainnet') - const preferences = new PreferencesController() + const preferences = new PreferencesController({ network }) const controller = new DetectTokensController({ preferences: preferences, network: network, keyringMemStore: keyringMemStore }) controller.isOpen = true controller.isUnlocked = true @@ -68,7 +67,8 @@ describe('DetectTokensController', () => { }) it('should not check tokens while in test network', async () => { - network.setProviderType('rinkeby') + // network.setProviderType('rinkeby') + // const controller = new DetectTokensController({ preferences: preferences, network: network, keyringMemStore: keyringMemStore }) controller.isOpen = true controller.isUnlocked = true @@ -81,7 +81,8 @@ describe('DetectTokensController', () => { }) it('should only check and add tokens while in main network', async () => { - network.setProviderType('mainnet') + // network.setProviderType('mainnet') + const controller = new DetectTokensController({ preferences: preferences, network: network, keyringMemStore: keyringMemStore }) controller.isOpen = true controller.isUnlocked = true @@ -97,7 +98,8 @@ describe('DetectTokensController', () => { }) it('should not detect same token while in main network', async () => { - network.setProviderType('mainnet') + // network.setProviderType('mainnet') + preferences.addToken('0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4', 'J8T', 8) const controller = new DetectTokensController({ preferences: preferences, network: network, keyringMemStore: keyringMemStore }) controller.isOpen = true controller.isUnlocked = true @@ -114,7 +116,8 @@ describe('DetectTokensController', () => { }) it('should trigger detect new tokens when change address', async () => { - network.setProviderType('mainnet') + // network.setProviderType('mainnet') + // const controller = new DetectTokensController({ preferences: preferences, network: network, keyringMemStore: keyringMemStore }) controller.isOpen = true controller.isUnlocked = true var stub = sandbox.stub(controller, 'detectNewTokens') @@ -123,7 +126,8 @@ describe('DetectTokensController', () => { }) it('should trigger detect new tokens when submit password', async () => { - network.setProviderType('mainnet') + // network.setProviderType('mainnet') + // const controller = new DetectTokensController({ preferences: preferences, network: network, keyringMemStore: keyringMemStore }) controller.isOpen = true controller.selectedAddress = '0x0' var stub = sandbox.stub(controller, 'detectNewTokens') @@ -132,7 +136,8 @@ describe('DetectTokensController', () => { }) it('should not trigger detect new tokens when not open or not unlocked', async () => { - network.setProviderType('mainnet') + // network.setProviderType('mainnet') + // const controller = new DetectTokensController({ preferences: preferences, network: network, keyringMemStore: keyringMemStore }) controller.isOpen = true controller.isUnlocked = false var stub = sandbox.stub(controller, 'detectTokenBalance') @@ -143,4 +148,4 @@ describe('DetectTokensController', () => { clock.tick(180000) sandbox.assert.notCalled(stub) }) -}) +})
\ No newline at end of file diff --git a/test/unit/app/controllers/preferences-controller-test.js b/test/unit/app/controllers/preferences-controller-test.js index e055500b1..9b2c846bd 100644 --- a/test/unit/app/controllers/preferences-controller-test.js +++ b/test/unit/app/controllers/preferences-controller-test.js @@ -1,11 +1,14 @@ const assert = require('assert') +const ObservableStore = require('obs-store') const PreferencesController = require('../../../../app/scripts/controllers/preferences') describe('preferences controller', function () { let preferencesController + let network beforeEach(() => { - preferencesController = new PreferencesController() + network = {providerStore: new ObservableStore({ type: 'mainnet' })} + preferencesController = new PreferencesController({ network }) }) describe('setAddresses', function () { @@ -28,6 +31,20 @@ describe('preferences controller', function () { }) }) + it('should create account tokens for each account in the store', function () { + preferencesController.setAddresses([ + '0xda22le', + '0x7e57e2', + ]) + + const accountTokens = preferencesController.store.getState().accountTokens + + assert.deepEqual(accountTokens, { + '0xda22le': {}, + '0x7e57e2': {}, + }) + }) + it('should replace its list of addresses', function () { preferencesController.setAddresses([ '0xda22le', @@ -64,6 +81,17 @@ describe('preferences controller', function () { assert.equal(preferencesController.store.getState().identities['0xda22le'], undefined) }) + it('should remove an address from state and respective tokens', function () { + preferencesController.setAddresses([ + '0xda22le', + '0x7e57e2', + ]) + + preferencesController.removeAddress('0xda22le') + + assert.equal(preferencesController.store.getState().accountTokens['0xda22le'], undefined) + }) + it('should switch accounts if the selected address is removed', function () { preferencesController.setAddresses([ '0xda22le', @@ -158,6 +186,42 @@ describe('preferences controller', function () { await preferencesController.addToken(address, symbol, decimals) assert.equal(preferencesController.getTokens().length, 1, 'one token added for 2nd address') }) + + it('should add token per account', async function () { + const addressFirst = '0xabcdef1234567' + const addressSecond = '0xabcdef1234568' + const symbolFirst = 'ABBR' + const symbolSecond = 'ABBB' + const decimals = 5 + + await preferencesController.setSelectedAddress('0x7e57e2') + await preferencesController.addToken(addressFirst, symbolFirst, decimals) + const tokensFirstAddress = preferencesController.getTokens() + + await preferencesController.setSelectedAddress('0xda22le') + await preferencesController.addToken(addressSecond, symbolSecond, decimals) + const tokensSeconAddress = preferencesController.getTokens() + + assert.notEqual(tokensFirstAddress, tokensSeconAddress, 'add different tokens for two account and tokens are equal') + }) + + it('should add token per network', async function () { + const addressFirst = '0xabcdef1234567' + const addressSecond = '0xabcdef1234568' + const symbolFirst = 'ABBR' + const symbolSecond = 'ABBB' + const decimals = 5 + + network.providerStore.updateState({ type: 'mainnet' }) + await preferencesController.addToken(addressFirst, symbolFirst, decimals) + const tokensFirstAddress = preferencesController.getTokens() + + network.providerStore.updateState({ type: 'rinkeby' }) + await preferencesController.addToken(addressSecond, symbolSecond, decimals) + const tokensSeconAddress = preferencesController.getTokens() + + assert.notEqual(tokensFirstAddress, tokensSeconAddress, 'add different tokens for two networks and tokens are equal') + }) }) describe('removeToken', function () { @@ -182,6 +246,98 @@ describe('preferences controller', function () { const [token1] = tokens assert.deepEqual(token1, {address: '0xb', symbol: 'B', decimals: 5}) }) + + it('should remove a token from its state on corresponding address', async function () { + await preferencesController.setSelectedAddress('0x7e57e2') + await preferencesController.addToken('0xa', 'A', 4) + await preferencesController.addToken('0xb', 'B', 5) + await preferencesController.setSelectedAddress('0x7e57e3') + await preferencesController.addToken('0xa', 'A', 4) + await preferencesController.addToken('0xb', 'B', 5) + const initialTokensSecond = preferencesController.getTokens() + await preferencesController.setSelectedAddress('0x7e57e2') + await preferencesController.removeToken('0xa') + + const tokensFirst = preferencesController.getTokens() + assert.equal(tokensFirst.length, 1, 'one token removed in account') + + const [token1] = tokensFirst + assert.deepEqual(token1, {address: '0xb', symbol: 'B', decimals: 5}) + + await preferencesController.setSelectedAddress('0x7e57e3') + const tokensSecond = preferencesController.getTokens() + assert.deepEqual(tokensSecond, initialTokensSecond, 'token deleted for account') + }) + + it('should remove a token from its state on corresponding network', async function () { + network.providerStore.updateState({ type: 'mainnet' }) + await preferencesController.addToken('0xa', 'A', 4) + await preferencesController.addToken('0xb', 'B', 5) + network.providerStore.updateState({ type: 'rinkeby' }) + await preferencesController.addToken('0xa', 'A', 4) + await preferencesController.addToken('0xb', 'B', 5) + const initialTokensSecond = preferencesController.getTokens() + network.providerStore.updateState({ type: 'mainnet' }) + await preferencesController.removeToken('0xa') + + const tokensFirst = preferencesController.getTokens() + assert.equal(tokensFirst.length, 1, 'one token removed in network') + + const [token1] = tokensFirst + assert.deepEqual(token1, {address: '0xb', symbol: 'B', decimals: 5}) + + network.providerStore.updateState({ type: 'rinkeby' }) + const tokensSecond = preferencesController.getTokens() + assert.deepEqual(tokensSecond, initialTokensSecond, 'token deleted for network') + }) + }) + + describe('on setSelectedAddress', function () { + it('should update tokens from its state on corresponding address', async function () { + await preferencesController.setSelectedAddress('0x7e57e2') + await preferencesController.addToken('0xa', 'A', 4) + await preferencesController.addToken('0xb', 'B', 5) + await preferencesController.setSelectedAddress('0x7e57e3') + await preferencesController.addToken('0xa', 'C', 4) + await preferencesController.addToken('0xb', 'D', 5) + + await preferencesController.setSelectedAddress('0x7e57e2') + const initialTokensFirst = preferencesController.getTokens() + await preferencesController.setSelectedAddress('0x7e57e3') + const initialTokensSecond = preferencesController.getTokens() + + assert.notDeepEqual(initialTokensFirst, initialTokensSecond, 'tokens not equal for different accounts and tokens') + + await preferencesController.setSelectedAddress('0x7e57e2') + const tokensFirst = preferencesController.getTokens() + await preferencesController.setSelectedAddress('0x7e57e3') + const tokensSecond = preferencesController.getTokens() + + assert.deepEqual(tokensFirst, initialTokensFirst, 'tokens equal for same account') + assert.deepEqual(tokensSecond, initialTokensSecond, 'tokens equal for same account') + }) + }) + + describe('on updateStateNetworkType', function () { + it('should remove a token from its state on corresponding network', async function () { + network.providerStore.updateState({ type: 'mainnet' }) + await preferencesController.addToken('0xa', 'A', 4) + await preferencesController.addToken('0xb', 'B', 5) + const initialTokensFirst = preferencesController.getTokens() + network.providerStore.updateState({ type: 'rinkeby' }) + await preferencesController.addToken('0xa', 'C', 4) + await preferencesController.addToken('0xb', 'D', 5) + const initialTokensSecond = preferencesController.getTokens() + + assert.notDeepEqual(initialTokensFirst, initialTokensSecond, 'tokens not equal for different networks and tokens') + + network.providerStore.updateState({ type: 'mainnet' }) + const tokensFirst = preferencesController.getTokens() + network.providerStore.updateState({ type: 'rinkeby' }) + const tokensSecond = preferencesController.getTokens() + assert.deepEqual(tokensFirst, initialTokensFirst, 'tokens equal for same network') + assert.deepEqual(tokensSecond, initialTokensSecond, 'tokens equal for same network') + }) }) }) diff --git a/test/unit/migrations/028-test.js b/test/unit/migrations/028-test.js new file mode 100644 index 000000000..a9c7dcdf1 --- /dev/null +++ b/test/unit/migrations/028-test.js @@ -0,0 +1,46 @@ +const assert = require('assert') +const migration28 = require('../../../app/scripts/migrations/028') + +const oldStorage = { + 'meta': {}, + 'data': { + 'PreferencesController': { + 'tokens': [{address: '0xa', symbol: 'A', decimals: 4}, {address: '0xb', symbol: 'B', decimals: 4}], + 'identities': { + '0x6d14': {}, + '0x3695': {}, + }, + }, + }, +} + +describe('migration #28', () => { + it('should add corresponding tokens to accountTokens', (done) => { + migration28.migrate(oldStorage) + .then((newStorage) => { + const newTokens = newStorage.data.PreferencesController.tokens + const newAccountTokens = newStorage.data.PreferencesController.accountTokens + + const testTokens = [{address: '0xa', symbol: 'A', decimals: 4}, {address: '0xb', symbol: 'B', decimals: 4}] + assert.equal(newTokens.length, 0, 'tokens is expected to have the length of 0') + assert.equal(newAccountTokens['0x6d14']['mainnet'].length, 2, 'tokens for address is expected to have the length of 2') + assert.equal(newAccountTokens['0x3695']['mainnet'].length, 2, 'tokens for address is expected to have the length of 2') + assert.equal(Object.keys(newAccountTokens).length, 2, 'account tokens should be created for all identities') + assert.deepEqual(newAccountTokens['0x6d14']['mainnet'], testTokens, 'tokens for address should be the same than before') + assert.deepEqual(newAccountTokens['0x3695']['mainnet'], testTokens, 'tokens for address should be the same than before') + done() + }) + .catch(done) + }) + + it('should successfully migrate first time state', (done) => { + migration28.migrate({ + meta: {}, + data: require('../../../app/scripts/first-time-state'), + }) + .then((migratedData) => { + assert.equal(migratedData.meta.version, migration28.version) + done() + }).catch(done) + }) +}) |