diff options
author | kumavis <aaron@kumavis.me> | 2018-07-04 03:42:14 +0800 |
---|---|---|
committer | kumavis <aaron@kumavis.me> | 2018-07-04 03:42:14 +0800 |
commit | 77daed5c74aa3c1fdbf1d9c71f544928f47f3993 (patch) | |
tree | 4a614dbdfce84b452562dcabccffa53b8393e2b0 /test/unit/app/controllers/transactions/pending-tx-test.js | |
parent | 2b2cd87e2d6e93dadb3ddd20ebf854ab6f2d2382 (diff) | |
parent | e467eda4f720fa89141ded2e709566102402479b (diff) | |
download | tangerine-wallet-browser-77daed5c74aa3c1fdbf1d9c71f544928f47f3993.tar.gz tangerine-wallet-browser-77daed5c74aa3c1fdbf1d9c71f544928f47f3993.tar.zst tangerine-wallet-browser-77daed5c74aa3c1fdbf1d9c71f544928f47f3993.zip |
Merge branch 'develop' of github.com:MetaMask/metamask-extension into network-remove-provider-engine
Diffstat (limited to 'test/unit/app/controllers/transactions/pending-tx-test.js')
-rw-r--r-- | test/unit/app/controllers/transactions/pending-tx-test.js | 94 |
1 files changed, 67 insertions, 27 deletions
diff --git a/test/unit/app/controllers/transactions/pending-tx-test.js b/test/unit/app/controllers/transactions/pending-tx-test.js index f0096dadd..2a2db0560 100644 --- a/test/unit/app/controllers/transactions/pending-tx-test.js +++ b/test/unit/app/controllers/transactions/pending-tx-test.js @@ -1,20 +1,12 @@ const assert = require('assert') -const ethUtil = require('ethereumjs-util') -const EthTx = require('ethereumjs-tx') -const ObservableStore = require('obs-store') -const clone = require('clone') const { createTestProviderTools } = require('../../../../stub/provider') const PendingTransactionTracker = require('../../../../../app/scripts/controllers/transactions/pending-tx-tracker') const MockTxGen = require('../../../../lib/mock-tx-gen') const sinon = require('sinon') -const noop =()=>true -const currentNetworkId = 42 -const otherNetworkId = 36 -const privKey = new Buffer('8718b9618a37d1fc78c436511fc6df3c8258d3250635bba617f33003270ec03e', 'hex') describe('PendingTransactionTracker', function () { - let pendingTxTracker, txMeta, txMetaNoHash, txMetaNoRawTx, providerResultStub, + let pendingTxTracker, txMeta, txMetaNoHash, providerResultStub, provider, txMeta3, txList, knownErrors this.timeout(10000) @@ -35,11 +27,7 @@ describe('PendingTransactionTracker', function () { status: 'signed', txParams: { from: '0x1678a085c290ebd122dc42cba69373b5953b831d'}, } - txMetaNoRawTx = { - hash: '0x0593ee121b92e10d63150ad08b4b8f9c7857d1bd160195ee648fb9a0f8d00eeb', - status: 'signed', - txParams: { from: '0x1678a085c290ebd122dc42cba69373b5953b831d'}, - } + providerResultStub = {} provider = createTestProviderTools({ scaffold: providerResultStub }).provider @@ -48,10 +36,10 @@ describe('PendingTransactionTracker', function () { nonceTracker: { getGlobalLock: async () => { return { releaseLock: () => {} } - } + }, }, - getPendingTransactions: () => {return []}, - getCompletedTransactions: () => {return []}, + getPendingTransactions: () => { return [] }, + getCompletedTransactions: () => { return [] }, publishTransaction: () => {}, confirmTransaction: () => {}, }) @@ -108,6 +96,58 @@ describe('PendingTransactionTracker', function () { }) }) + describe('#checkForTxInBlock', function () { + it('should return if no pending transactions', function () { + // throw a type error if it trys to do anything on the block + // thus failing the test + const block = Proxy.revocable({}, {}).revoke() + pendingTxTracker.checkForTxInBlock(block) + }) + it('should emit \'tx:failed\' if the txMeta does not have a hash', function (done) { + const block = Proxy.revocable({}, {}).revoke() + pendingTxTracker.getPendingTransactions = () => [txMetaNoHash] + pendingTxTracker.once('tx:failed', (txId, err) => { + assert(txId, txMetaNoHash.id, 'should pass txId') + done() + }) + pendingTxTracker.checkForTxInBlock(block) + }) + it('should emit \'txConfirmed\' if the tx is in the block', function (done) { + const block = { transactions: [txMeta]} + pendingTxTracker.getPendingTransactions = () => [txMeta] + pendingTxTracker.once('tx:confirmed', (txId) => { + assert(txId, txMeta.id, 'should pass txId') + done() + }) + pendingTxTracker.once('tx:failed', (_, err) => { done(err) }) + pendingTxTracker.checkForTxInBlock(block) + }) + }) + describe('#queryPendingTxs', function () { + it('should call #_checkPendingTxs if their is no oldBlock', function (done) { + let oldBlock + const newBlock = { number: '0x01' } + pendingTxTracker._checkPendingTxs = done + pendingTxTracker.queryPendingTxs({ oldBlock, newBlock }) + }) + it('should call #_checkPendingTxs if oldBlock and the newBlock have a diff of greater then 1', function (done) { + const oldBlock = { number: '0x01' } + const newBlock = { number: '0x03' } + pendingTxTracker._checkPendingTxs = done + pendingTxTracker.queryPendingTxs({ oldBlock, newBlock }) + }) + it('should not call #_checkPendingTxs if oldBlock and the newBlock have a diff of 1 or less', function (done) { + const oldBlock = { number: '0x1' } + const newBlock = { number: '0x2' } + pendingTxTracker._checkPendingTxs = () => { + const err = new Error('should not call #_checkPendingTxs if oldBlock and the newBlock have a diff of 1 or less') + done(err) + } + pendingTxTracker.queryPendingTxs({ oldBlock, newBlock }) + done() + }) + }) + describe('#_checkPendingTx', function () { it('should emit \'tx:failed\' if the txMeta does not have a hash', function (done) { pendingTxTracker.once('tx:failed', (txId, err) => { @@ -129,7 +169,7 @@ describe('PendingTransactionTracker', function () { txMeta2.id = 2 txMeta3.id = 3 txList = [txMeta, txMeta2, txMeta3].map((tx) => { - tx.processed = new Promise ((resolve) => { tx.resolve = resolve }) + tx.processed = new Promise((resolve) => { tx.resolve = resolve }) return tx }) }) @@ -146,7 +186,7 @@ describe('PendingTransactionTracker', function () { }) describe('#resubmitPendingTxs', function () { - const blockNuberStub = '0x0' + const blockNumberStub = '0x0' beforeEach(function () { const txMeta2 = txMeta3 = txMeta txList = [txMeta, txMeta2, txMeta3].map((tx) => { @@ -164,7 +204,7 @@ describe('PendingTransactionTracker', function () { Promise.all(txList.map((tx) => tx.processed)) .then((txCompletedList) => done()) .catch(done) - pendingTxTracker.resubmitPendingTxs(blockNuberStub) + pendingTxTracker.resubmitPendingTxs(blockNumberStub) }) it('should not emit \'tx:failed\' if the txMeta throws a known txError', function (done) { knownErrors = [ @@ -191,7 +231,7 @@ describe('PendingTransactionTracker', function () { .then((txCompletedList) => done()) .catch(done) - pendingTxTracker.resubmitPendingTxs(blockNuberStub) + pendingTxTracker.resubmitPendingTxs(blockNumberStub) }) it('should emit \'tx:warning\' if it encountered a real error', function (done) { pendingTxTracker.once('tx:warning', (txMeta, err) => { @@ -209,12 +249,12 @@ describe('PendingTransactionTracker', function () { .then((txCompletedList) => done()) .catch(done) - pendingTxTracker.resubmitPendingTxs(blockNuberStub) + pendingTxTracker.resubmitPendingTxs(blockNumberStub) }) }) describe('#_resubmitTx', function () { const mockFirstRetryBlockNumber = '0x1' - let txMetaToTestExponentialBackoff + let txMetaToTestExponentialBackoff, enoughBalance beforeEach(() => { pendingTxTracker.getBalance = (address) => { @@ -237,7 +277,7 @@ describe('PendingTransactionTracker', function () { }) it('should publish the transaction', function (done) { - const enoughBalance = '0x100000' + enoughBalance = '0x100000' // Stubbing out current account state: // Adding the fake tx: @@ -252,7 +292,7 @@ describe('PendingTransactionTracker', function () { }) it('should not publish the transaction if the limit of retries has been exceeded', function (done) { - const enoughBalance = '0x100000' + enoughBalance = '0x100000' const mockLatestBlockNumber = '0x5' pendingTxTracker._resubmitTx(txMetaToTestExponentialBackoff, mockLatestBlockNumber) @@ -266,7 +306,7 @@ describe('PendingTransactionTracker', function () { }) it('should publish the transaction if the number of blocks since last retry exceeds the last set limit', function (done) { - const enoughBalance = '0x100000' + enoughBalance = '0x100000' const mockLatestBlockNumber = '0x11' pendingTxTracker._resubmitTx(txMetaToTestExponentialBackoff, mockLatestBlockNumber) @@ -282,7 +322,7 @@ describe('PendingTransactionTracker', function () { describe('#_checkIfNonceIsTaken', function () { beforeEach(function () { - let confirmedTxList = [{ + const confirmedTxList = [{ id: 1, hash: '0x0593ee121b92e10d63150ad08b4b8f9c7857d1bd160195ee648fb9a0f8d00eeb', status: 'confirmed', |