diff --git a/karma.conf.js b/karma.conf.js index d9b224d28..ebbeacaed 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -79,6 +79,7 @@ module.exports = function(karma) { 'tests/transaction_spec.js.coffee', //(OK) 'tests/transaction_spend_spec.js.coffee', //(OK) 'tests/wallet_spec.js.coffee', //(PARTIAL) + 'tests/wallet_store_spec.js.coffee', 'tests/bip38_spec.js.coffee', //(OK) 'tests/address_spec.js.coffee', 'tests/keychain_spec.js.coffee', diff --git a/src/transaction-list.js b/src/transaction-list.js index 10fc3ac35..706b8cc65 100644 --- a/src/transaction-list.js +++ b/src/transaction-list.js @@ -57,13 +57,17 @@ TransactionList.prototype.fetchTxs = function (amount) { }; TransactionList.prototype.pushTxs = function (txs) { - txs = Helpers.toArrayFormat(txs).map(Tx.factory); + txs = Helpers.toArrayFormat(txs).map(Tx.factory).filter(function (tx) { + return !this.transaction(tx.hash); + }.bind(this)); this._transactions = this._transactions.concat(txs); this._events.emit('update'); }; TransactionList.prototype.shiftTxs = function (txs) { - txs = Helpers.toArrayFormat(txs).map(Tx.factory); + txs = Helpers.toArrayFormat(txs).map(Tx.factory).filter(function (tx) { + return !this.transaction(tx.hash); + }.bind(this)); this._transactions = txs.concat(this._transactions); this._events.emit('update'); }; diff --git a/src/wallet-store.js b/src/wallet-store.js index 035003526..33ebb02c6 100644 --- a/src/wallet-store.js +++ b/src/wallet-store.js @@ -101,6 +101,9 @@ var WalletStore = (function() { getCurrencies: function() { return currencyCodeToCurrency; }, + getTransaction: function(hash) { + return transactions.filter(function(tx){return tx.hash === hash})[0] + }, getTransactions: function() { return transactions; }, diff --git a/tests/blockchain_wallet_spec.js.coffee b/tests/blockchain_wallet_spec.js.coffee index b635d1958..0a5be56c1 100644 --- a/tests/blockchain_wallet_spec.js.coffee +++ b/tests/blockchain_wallet_spec.js.coffee @@ -3,6 +3,7 @@ MyWallet = undefined Address = undefined Wallet = undefined HDWallet = undefined +WalletStore = undefined describe "HDWallet", -> wallet = undefined @@ -77,11 +78,21 @@ describe "HDWallet", -> isInstanceOf: (candidate, theClass) -> candidate.label != undefined || typeof(candidate) == "object" + walletStoreTxs = [] + WalletStore = { + pushTransaction: (tx) -> walletStoreTxs.push(tx) + getTransactions: () -> walletStoreTxs + getTransaction: (hash) -> walletStoreTxs.filter( + (tx) -> tx.hash == hash + )[0] + } + stubs = { './wallet' : MyWallet, './address' : Address, './helpers' : Helpers, - './hd-wallet': HDWallet + './hd-wallet': HDWallet, + './wallet-store' : WalletStore } Wallet = proxyquire('../src/blockchain-wallet', stubs) @@ -446,6 +457,42 @@ describe "HDWallet", -> it ".getPrivateKeyForAddress", -> pending() + describe "_updateWalletInfo()", -> + multiaddr = { + wallet: + total_sent: 1 + total_received: 0 + final_balance: 0 + n_tx: 1 + addresses: [ + { + address:"1CzYCAAi46b8CFiybd3CcGbUykCAeqaocj", + n_tx:1, + total_received: 0, + total_sent: 1, + final_balance: 0 + } + ] + txs: [ + { + hash: "1234" + } + ] + info: + latest_block: 300000 + } + beforeEach -> + spyOn(WalletStore, "pushTransaction").and.callThrough() + + it "should add a new transaction", -> + wallet._updateWalletInfo(multiaddr) + expect(WalletStore.pushTransaction).toHaveBeenCalled() + + it "should not add a duplicate transaction", -> + wallet._updateWalletInfo(multiaddr) + wallet._updateWalletInfo(multiaddr) + expect(WalletStore.getTransactions().length).toEqual(1) + describe "JSON serialization", -> it 'should hold: fromJSON . toJSON = id', -> diff --git a/tests/transaction_list_spec.js.coffee b/tests/transaction_list_spec.js.coffee index 1e13aba8b..c25c9847b 100644 --- a/tests/transaction_list_spec.js.coffee +++ b/tests/transaction_list_spec.js.coffee @@ -50,13 +50,33 @@ describe 'TransactionList', -> expect(txList.transactions().length).toEqual(1) done() + it 'should not add duplicate tx to the list', (done) -> + txList.fetchTxs().then (numFetched1) -> + + txList.fetchTxs().then (numFetched2) -> + expect(numFetched1).toEqual(1) + expect(numFetched2).toEqual(1) + + expect(txList.transactions().length).toEqual(1) + done() + it 'should add txs to the tx list', -> txList.pushTxs({ txType: 'sent' }) expect(txList.transactions.length).toEqual(1) it 'should prepend txs to the tx list', -> - txList.shiftTxs({ txType: 'sent' }) - expect(txList.transactions.length).toEqual(1) + expect(txList.transactions().length).toEqual(0) + + txList.shiftTxs({ txType: 'sent', hash: "1234" }) + expect(txList.transactions().length).toEqual(1) + + it 'should not prepend duplicate txs to the tx list', -> + txList.shiftTxs({ txType: 'sent', hash: "1234"}) + txList.shiftTxs({ txType: 'sent', hash: "1234"}) + + expect(txList.transactions().length).toEqual(1) + + describe 'events', -> spy = undefined diff --git a/tests/wallet_store_spec.js.coffee b/tests/wallet_store_spec.js.coffee new file mode 100644 index 000000000..a89b51cd5 --- /dev/null +++ b/tests/wallet_store_spec.js.coffee @@ -0,0 +1,44 @@ +proxyquire = require('proxyquireify')(require) +MyWallet = { + wallet: { + getHistory: () -> + syncWallet: () -> + } +} + +stubs = { + './wallet': MyWallet, +} + +WalletStore = proxyquire('../src/wallet-store', stubs) + +describe "WalletStore", -> + + beforeEach -> + spyOn(MyWallet, "syncWallet") + spyOn(MyWallet.wallet, "getHistory") + + describe "instance", -> + beforeEach -> + + describe "getTransactions()", -> + it "should return nothing initially", -> + expect(WalletStore.getTransactions()).toEqual([]) + + describe "pushTransaction()", -> + + it "add a transaction", -> + tx = {hash: "1234"} + WalletStore.pushTransaction(tx) + expect(WalletStore.getTransactions()).toEqual([tx]) + + describe "getTransaction()", -> + it "should return a transaction with the right hash", -> + tx1 = {hash: "1234"} + tx2 = {hash: "5678"} + + WalletStore.pushTransaction(tx1) + WalletStore.pushTransaction(tx2) + + expect(WalletStore.getTransaction("1234")).toEqual(tx1) + expect(WalletStore.getTransaction("5678")).toEqual(tx2)