diff options
author | Thomas Huang <tmashuang@users.noreply.github.com> | 2019-06-05 05:55:56 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-06-05 05:55:56 +0800 |
commit | be911711942914e7f6df535afff1a3ee87e23b6f (patch) | |
tree | bbd890e0a6a1bd72d2560e734ce66ed42a294722 /test/unit/app/controllers | |
parent | 3d7bdd6ecb5e8e17cb8ccf83f56ad389971332c1 (diff) | |
parent | 28e030a11d75df81e16f3ebf726bfc59fcee679e (diff) | |
download | tangerine-wallet-browser-be911711942914e7f6df535afff1a3ee87e23b6f.tar tangerine-wallet-browser-be911711942914e7f6df535afff1a3ee87e23b6f.tar.gz tangerine-wallet-browser-be911711942914e7f6df535afff1a3ee87e23b6f.tar.bz2 tangerine-wallet-browser-be911711942914e7f6df535afff1a3ee87e23b6f.tar.lz tangerine-wallet-browser-be911711942914e7f6df535afff1a3ee87e23b6f.tar.xz tangerine-wallet-browser-be911711942914e7f6df535afff1a3ee87e23b6f.tar.zst tangerine-wallet-browser-be911711942914e7f6df535afff1a3ee87e23b6f.zip |
Merge pull request #6683 from MetaMask/develop
Merge dev to master
Diffstat (limited to 'test/unit/app/controllers')
4 files changed, 74 insertions, 323 deletions
diff --git a/test/unit/app/controllers/currency-controller-test.js b/test/unit/app/controllers/currency-controller-test.js deleted file mode 100644 index 8b6fbb719..000000000 --- a/test/unit/app/controllers/currency-controller-test.js +++ /dev/null @@ -1,78 +0,0 @@ -const assert = require('assert') -const nock = require('nock') -const CurrencyController = require('../../../../app/scripts/controllers/currency') - -describe('currency-controller', function () { - var currencyController - - beforeEach(function () { - currencyController = new CurrencyController() - }) - - describe('currency conversions', function () { - describe('#setCurrentCurrency', function () { - it('should return USD as default', function () { - assert.equal(currencyController.getCurrentCurrency(), 'usd') - }) - - it('should be able to set to other currency', function () { - assert.equal(currencyController.getCurrentCurrency(), 'usd') - currencyController.setCurrentCurrency('JPY') - var result = currencyController.getCurrentCurrency() - assert.equal(result, 'JPY') - }) - }) - - describe('#getConversionRate', function () { - it('should return undefined if non-existent', function () { - var result = currencyController.getConversionRate() - assert.ok(!result) - }) - }) - - describe('#updateConversionRate', function () { - it('should retrieve an update for ETH to USD and set it in memory', function (done) { - this.timeout(15000) - nock('https://api.infura.io') - .get('/v1/ticker/ethusd') - .reply(200, '{"base": "ETH", "quote": "USD", "bid": 288.45, "ask": 288.46, "volume": 112888.17569277, "exchange": "bitfinex", "total_volume": 272175.00106721005, "num_exchanges": 8, "timestamp": 1506444677}') - - assert.equal(currencyController.getConversionRate(), 0) - currencyController.setCurrentCurrency('usd') - currencyController.updateConversionRate() - .then(function () { - var result = currencyController.getConversionRate() - assert.equal(typeof result, 'number') - done() - }).catch(function (err) { - done(err) - }) - }) - - it('should work for JPY as well.', function () { - this.timeout(15000) - assert.equal(currencyController.getConversionRate(), 0) - - nock('https://api.infura.io') - .get('/v1/ticker/ethjpy') - .reply(200, '{"base": "ETH", "quote": "JPY", "bid": 32300.0, "ask": 32400.0, "volume": 247.4616071, "exchange": "kraken", "total_volume": 247.4616071, "num_exchanges": 1, "timestamp": 1506444676}') - - - var promise = new Promise( - function (resolve) { - currencyController.setCurrentCurrency('jpy') - currencyController.updateConversionRate().then(function () { - resolve() - }) - }) - - promise.then(function () { - var result = currencyController.getConversionRate() - assert.equal(typeof result, 'number') - }).catch(function (done, err) { - done(err) - }) - }) - }) - }) -}) diff --git a/test/unit/app/controllers/metamask-controller-test.js b/test/unit/app/controllers/metamask-controller-test.js index a56b8adbd..1b8cde42e 100644 --- a/test/unit/app/controllers/metamask-controller-test.js +++ b/test/unit/app/controllers/metamask-controller-test.js @@ -45,6 +45,11 @@ describe('MetaMaskController', function () { .get(/.*/) .reply(200) + nock('https://min-api.cryptocompare.com') + .persist() + .get(/.*/) + .reply(200, '{"JPY":12415.9}') + metamaskController = new MetaMaskController({ showUnapprovedTx: noop, showUnconfirmedMessage: noop, @@ -441,7 +446,7 @@ describe('MetaMaskController', function () { let defaultMetaMaskCurrency beforeEach(function () { - defaultMetaMaskCurrency = metamaskController.currencyController.getCurrentCurrency() + defaultMetaMaskCurrency = metamaskController.currencyRateController.state.currentCurrency }) it('defaults to usd', function () { @@ -450,7 +455,7 @@ describe('MetaMaskController', function () { it('sets currency to JPY', function () { metamaskController.setCurrentCurrency('JPY', noop) - assert.equal(metamaskController.currencyController.getCurrentCurrency(), 'JPY') + assert.equal(metamaskController.currencyRateController.state.currentCurrency, 'JPY') }) }) diff --git a/test/unit/app/controllers/transactions/nonce-tracker-test.js b/test/unit/app/controllers/transactions/nonce-tracker-test.js deleted file mode 100644 index 51ac390e9..000000000 --- a/test/unit/app/controllers/transactions/nonce-tracker-test.js +++ /dev/null @@ -1,238 +0,0 @@ -const assert = require('assert') -const NonceTracker = require('../../../../../app/scripts/controllers/transactions/nonce-tracker') -const MockTxGen = require('../../../../lib/mock-tx-gen') -const providerResultStub = {} - -describe('Nonce Tracker', function () { - let nonceTracker, pendingTxs, confirmedTxs - - describe('#getNonceLock', function () { - - describe('with 3 confirmed and 1 pending', function () { - beforeEach(function () { - const txGen = new MockTxGen() - confirmedTxs = txGen.generate({ status: 'confirmed' }, { count: 3 }) - pendingTxs = txGen.generate({ status: 'submitted' }, { count: 1 }) - nonceTracker = generateNonceTrackerWith(pendingTxs, confirmedTxs, '0x1') - }) - - it('should return 4', async function () { - this.timeout(15000) - const nonceLock = await nonceTracker.getNonceLock('0x7d3517b0d011698406d6e0aed8453f0be2697926') - assert.equal(nonceLock.nextNonce, '4', `nonce should be 4 got ${nonceLock.nextNonce}`) - await nonceLock.releaseLock() - }) - - it('should use localNonce if network returns a nonce lower then a confirmed tx in state', async function () { - this.timeout(15000) - const nonceLock = await nonceTracker.getNonceLock('0x7d3517b0d011698406d6e0aed8453f0be2697926') - assert.equal(nonceLock.nextNonce, '4', 'nonce should be 4') - await nonceLock.releaseLock() - }) - }) - - describe('sentry issue 476304902', function () { - beforeEach(function () { - const txGen = new MockTxGen() - pendingTxs = txGen.generate({ status: 'submitted' }, { - fromNonce: 3, - count: 29, - }) - nonceTracker = generateNonceTrackerWith(pendingTxs, [], '0x3') - }) - - it('should return 9', async function () { - this.timeout(15000) - const nonceLock = await nonceTracker.getNonceLock('0x7d3517b0d011698406d6e0aed8453f0be2697926') - assert.equal(nonceLock.nextNonce, '32', `nonce should be 32 got ${nonceLock.nextNonce}`) - await nonceLock.releaseLock() - }) - }) - - describe('issue 3670', function () { - beforeEach(function () { - const txGen = new MockTxGen() - pendingTxs = txGen.generate({ status: 'submitted' }, { - fromNonce: 6, - count: 3, - }) - nonceTracker = generateNonceTrackerWith(pendingTxs, [], '0x6') - }) - - it('should return 9', async function () { - this.timeout(15000) - const nonceLock = await nonceTracker.getNonceLock('0x7d3517b0d011698406d6e0aed8453f0be2697926') - assert.equal(nonceLock.nextNonce, '9', `nonce should be 9 got ${nonceLock.nextNonce}`) - await nonceLock.releaseLock() - }) - }) - - describe('with no previous txs', function () { - beforeEach(function () { - nonceTracker = generateNonceTrackerWith([], []) - }) - - it('should return 0', async function () { - this.timeout(15000) - const nonceLock = await nonceTracker.getNonceLock('0x7d3517b0d011698406d6e0aed8453f0be2697926') - assert.equal(nonceLock.nextNonce, '0', `nonce should be 0 returned ${nonceLock.nextNonce}`) - await nonceLock.releaseLock() - }) - }) - - describe('with multiple previous txs with same nonce', function () { - beforeEach(function () { - const txGen = new MockTxGen() - confirmedTxs = txGen.generate({ status: 'confirmed' }, { count: 1 }) - pendingTxs = txGen.generate({ - status: 'submitted', - txParams: { nonce: '0x01' }, - }, { count: 5 }) - - nonceTracker = generateNonceTrackerWith(pendingTxs, confirmedTxs, '0x0') - }) - - it('should return nonce after those', async function () { - this.timeout(15000) - const nonceLock = await nonceTracker.getNonceLock('0x7d3517b0d011698406d6e0aed8453f0be2697926') - assert.equal(nonceLock.nextNonce, '2', `nonce should be 2 got ${nonceLock.nextNonce}`) - await nonceLock.releaseLock() - }) - }) - - describe('when local confirmed count is higher than network nonce', function () { - beforeEach(function () { - const txGen = new MockTxGen() - confirmedTxs = txGen.generate({ status: 'confirmed' }, { count: 3 }) - nonceTracker = generateNonceTrackerWith([], confirmedTxs, '0x1') - }) - - it('should return nonce after those', async function () { - this.timeout(15000) - const nonceLock = await nonceTracker.getNonceLock('0x7d3517b0d011698406d6e0aed8453f0be2697926') - assert.equal(nonceLock.nextNonce, '3', `nonce should be 3 got ${nonceLock.nextNonce}`) - await nonceLock.releaseLock() - }) - }) - - describe('when local pending count is higher than other metrics', function () { - beforeEach(function () { - const txGen = new MockTxGen() - pendingTxs = txGen.generate({ status: 'submitted' }, { count: 2 }) - nonceTracker = generateNonceTrackerWith(pendingTxs, []) - }) - - it('should return nonce after those', async function () { - this.timeout(15000) - const nonceLock = await nonceTracker.getNonceLock('0x7d3517b0d011698406d6e0aed8453f0be2697926') - assert.equal(nonceLock.nextNonce, '2', `nonce should be 2 got ${nonceLock.nextNonce}`) - await nonceLock.releaseLock() - }) - }) - - describe('when provider nonce is higher than other metrics', function () { - beforeEach(function () { - const txGen = new MockTxGen() - pendingTxs = txGen.generate({ status: 'submitted' }, { count: 2 }) - nonceTracker = generateNonceTrackerWith(pendingTxs, [], '0x05') - }) - - it('should return nonce after those', async function () { - this.timeout(15000) - const nonceLock = await nonceTracker.getNonceLock('0x7d3517b0d011698406d6e0aed8453f0be2697926') - assert.equal(nonceLock.nextNonce, '5', `nonce should be 5 got ${nonceLock.nextNonce}`) - await nonceLock.releaseLock() - }) - }) - - describe('when there are some pending nonces below the remote one and some over.', function () { - beforeEach(function () { - const txGen = new MockTxGen() - pendingTxs = txGen.generate({ status: 'submitted' }, { count: 5 }) - nonceTracker = generateNonceTrackerWith(pendingTxs, [], '0x03') - }) - - it('should return nonce after those', async function () { - this.timeout(15000) - const nonceLock = await nonceTracker.getNonceLock('0x7d3517b0d011698406d6e0aed8453f0be2697926') - assert.equal(nonceLock.nextNonce, '5', `nonce should be 5 got ${nonceLock.nextNonce}`) - await nonceLock.releaseLock() - }) - }) - - describe('when there are pending nonces non sequentially over the network nonce.', function () { - beforeEach(function () { - const txGen = new MockTxGen() - txGen.generate({ status: 'submitted' }, { count: 5 }) - // 5 over that number - pendingTxs = txGen.generate({ status: 'submitted' }, { count: 5 }) - nonceTracker = generateNonceTrackerWith(pendingTxs, [], '0x00') - }) - - it('should return nonce after network nonce', async function () { - this.timeout(15000) - const nonceLock = await nonceTracker.getNonceLock('0x7d3517b0d011698406d6e0aed8453f0be2697926') - assert.equal(nonceLock.nextNonce, '0', `nonce should be 0 got ${nonceLock.nextNonce}`) - await nonceLock.releaseLock() - }) - }) - - describe('When all three return different values', function () { - beforeEach(function () { - const txGen = new MockTxGen() - confirmedTxs = txGen.generate({ status: 'confirmed' }, { count: 10 }) - pendingTxs = txGen.generate({ - status: 'submitted', - nonce: 100, - }, { count: 1 }) - // 0x32 is 50 in hex: - nonceTracker = generateNonceTrackerWith(pendingTxs, confirmedTxs, '0x32') - }) - - it('should return nonce after network nonce', async function () { - this.timeout(15000) - const nonceLock = await nonceTracker.getNonceLock('0x7d3517b0d011698406d6e0aed8453f0be2697926') - assert.equal(nonceLock.nextNonce, '50', `nonce should be 50 got ${nonceLock.nextNonce}`) - await nonceLock.releaseLock() - }) - }) - - describe('Faq issue 67', function () { - beforeEach(function () { - const txGen = new MockTxGen() - confirmedTxs = txGen.generate({ status: 'confirmed' }, { count: 64 }) - pendingTxs = txGen.generate({ - status: 'submitted', - }, { count: 10 }) - // 0x40 is 64 in hex: - nonceTracker = generateNonceTrackerWith(pendingTxs, [], '0x40') - }) - - it('should return nonce after network nonce', async function () { - this.timeout(15000) - const nonceLock = await nonceTracker.getNonceLock('0x7d3517b0d011698406d6e0aed8453f0be2697926') - assert.equal(nonceLock.nextNonce, '74', `nonce should be 74 got ${nonceLock.nextNonce}`) - await nonceLock.releaseLock() - }) - }) - }) -}) - -function generateNonceTrackerWith (pending, confirmed, providerStub = '0x0') { - const getPendingTransactions = () => pending - const getConfirmedTransactions = () => confirmed - providerResultStub.result = providerStub - const provider = { - sendAsync: (_, cb) => { cb(undefined, providerResultStub) }, - } - const blockTracker = { - getCurrentBlock: () => '0x11b568', - getLatestBlock: async () => '0x11b568', - } - return new NonceTracker({ - provider, - blockTracker, - getPendingTransactions, - getConfirmedTransactions, - }) -} diff --git a/test/unit/app/controllers/transactions/pending-tx-test.js b/test/unit/app/controllers/transactions/pending-tx-test.js index 1c5f59f5a..b37ac2766 100644 --- a/test/unit/app/controllers/transactions/pending-tx-test.js +++ b/test/unit/app/controllers/transactions/pending-tx-test.js @@ -58,7 +58,7 @@ describe('PendingTransactionTracker', function () { } }) - it('should become failed if another tx with the same nonce succeeds', async function () { + it('should emit dropped if another tx with the same nonce succeeds', async function () { // SETUP const txGen = new MockTxGen() @@ -84,17 +84,16 @@ describe('PendingTransactionTracker', function () { // THE EXPECTATION const spy = sinon.spy() - pendingTxTracker.on('tx:failed', (txId, err) => { + pendingTxTracker.on('tx:dropped', (txId) => { assert.equal(txId, pending.id, 'should fail the pending tx') - assert.equal(err.name, 'NonceTakenErr', 'should emit a nonce taken error.') - spy(txId, err) + spy(txId) }) // THE METHOD await pendingTxTracker._checkPendingTx(pending) // THE ASSERTION - assert.ok(spy.calledWith(pending.id), 'tx failed should be emitted') + assert.ok(spy.calledWith(pending.id), 'tx dropped should be emitted') }) }) @@ -107,6 +106,38 @@ describe('PendingTransactionTracker', function () { pendingTxTracker._checkPendingTx(txMetaNoHash) }) + it('should emit tx:dropped with the txMetas id only after the second call', function (done) { + txMeta = { + id: 1, + hash: '0x0593ee121b92e10d63150ad08b4b8f9c7857d1bd160195ee648fb9a0f8d00eeb', + status: 'submitted', + txParams: { + from: '0x1678a085c290ebd122dc42cba69373b5953b831d', + nonce: '0x1', + value: '0xfffff', + }, + history: [{}], + rawTx: '0xf86c808504a817c800827b0d940c62bb85faa3311a998d3aba8098c1235c564966880de0b6b3a7640000802aa08ff665feb887a25d4099e40e11f0fef93ee9608f404bd3f853dd9e84ed3317a6a02ec9d3d1d6e176d4d2593dd760e74ccac753e6a0ea0d00cc9789d0d7ff1f471d', + } + + providerResultStub['eth_getTransactionCount'] = '0x02' + providerResultStub['eth_getTransactionByHash'] = {} + pendingTxTracker.once('tx:dropped', (id) => { + if (id === txMeta.id) { + delete providerResultStub['eth_getTransactionCount'] + delete providerResultStub['eth_getTransactionByHash'] + return done() + } else { + done(new Error('wrong tx Id')) + } + }) + + pendingTxTracker._checkPendingTx(txMeta).then(() => { + pendingTxTracker._checkPendingTx(txMeta).catch(done) + }).catch(done) + }) + + it('should should return if query does not return txParams', function () { providerResultStub.eth_getTransactionByHash = null pendingTxTracker._checkPendingTx(txMeta) @@ -283,6 +314,37 @@ describe('PendingTransactionTracker', function () { }) }) + describe('#_checkIftxWasDropped', () => { + const txMeta = { + id: 1, + hash: '0x0593ee121b92e10d63150ad08b4b8f9c7857d1bd160195ee648fb9a0f8d00eeb', + status: 'submitted', + txParams: { + from: '0x1678a085c290ebd122dc42cba69373b5953b831d', + nonce: '0x1', + value: '0xfffff', + }, + rawTx: '0xf86c808504a817c800827b0d940c62bb85faa3311a998d3aba8098c1235c564966880de0b6b3a7640000802aa08ff665feb887a25d4099e40e11f0fef93ee9608f404bd3f853dd9e84ed3317a6a02ec9d3d1d6e176d4d2593dd760e74ccac753e6a0ea0d00cc9789d0d7ff1f471d', + } + it('should return false when the nonce is the suggested network nonce', (done) => { + providerResultStub['eth_getTransactionCount'] = '0x01' + providerResultStub['eth_getTransactionByHash'] = {} + pendingTxTracker._checkIftxWasDropped(txMeta).then((dropped) => { + assert(!dropped, 'should be false') + done() + }).catch(done) + }) + + it('should return true when the network nonce is higher then the txMeta nonce', function (done) { + providerResultStub['eth_getTransactionCount'] = '0x02' + providerResultStub['eth_getTransactionByHash'] = {} + pendingTxTracker._checkIftxWasDropped(txMeta).then((dropped) => { + assert(dropped, 'should be true') + done() + }).catch(done) + }) + }) + describe('#_checkIfNonceIsTaken', function () { beforeEach(function () { const confirmedTxList = [{ |