From 54de3f985f79a9631a678c98032c4448bd483783 Mon Sep 17 00:00:00 2001 From: Felipe Renan Date: Tue, 16 Oct 2018 18:05:59 -0300 Subject: [PATCH] Fix the Address page to count only transactions sent --- .../assets/__tests__/pages/address.js | 38 +++++++++++++++---- .../assets/js/pages/address.js | 14 ++++++- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/apps/block_scout_web/assets/__tests__/pages/address.js b/apps/block_scout_web/assets/__tests__/pages/address.js index 426bb4fbe8..02f319caa7 100644 --- a/apps/block_scout_web/assets/__tests__/pages/address.js +++ b/apps/block_scout_web/assets/__tests__/pages/address.js @@ -265,11 +265,14 @@ describe('RECEIVED_NEW_PENDING_TRANSACTION_BATCH', () => { describe('RECEIVED_NEW_TRANSACTION_BATCH', () => { test('single transaction', () => { - const state = initialState + const state = Object.assign({}, initialState, { + addressHash: '0x111' + }) const action = { type: 'RECEIVED_NEW_TRANSACTION_BATCH', msgs: [{ - transactionHtml: 'test' + transactionHtml: 'test', + fromAddressHash: '0x111' }] } const output = reducer(state, action) @@ -310,7 +313,6 @@ describe('RECEIVED_NEW_TRANSACTION_BATCH', () => { expect(output.newTransactions).toEqual([]) expect(output.batchCountAccumulator).toEqual(11) - expect(output.transactionCount).toEqual(11) }) test('single transaction after single transaction', () => { const state = Object.assign({}, initialState, { @@ -379,6 +381,28 @@ describe('RECEIVED_NEW_TRANSACTION_BATCH', () => { expect(output.newTransactions).toEqual([]) expect(output.batchCountAccumulator).toEqual(22) }) + test('increments the transactions count only when the address sent transactions', () => { + const state = Object.assign({}, initialState, { + newTransactions: [], + addressHash: '0x111', + transactionCount: 1 + }) + const action = { + type: 'RECEIVED_NEW_TRANSACTION_BATCH', + msgs: [{ + transactionHtml: 'test 12', + fromAddressHash: '0x111', + toAddressHash: '0x222' + },{ + transactionHtml: 'test 13', + fromAddressHash: '0x222', + toAddressHash: '0x111' + }] + } + const output = reducer(state, action) + + expect(output.transactionCount).toEqual(2) + }) test('after disconnection', () => { const state = Object.assign({}, initialState, { channelDisconnected: true @@ -397,12 +421,14 @@ describe('RECEIVED_NEW_TRANSACTION_BATCH', () => { test('on page 2', () => { const state = Object.assign({}, initialState, { beyondPageOne: true, - transactionCount: 1 + transactionCount: 1, + addressHash: '0x111' }) const action = { type: 'RECEIVED_NEW_TRANSACTION_BATCH', msgs: [{ - transactionHtml: 'test' + transactionHtml: 'test', + fromAddressHash: '0x111' }] } const output = reducer(state, action) @@ -488,7 +514,6 @@ describe('RECEIVED_NEW_TRANSACTION_BATCH', () => { expect(output.newTransactions).toEqual(['test']) expect(output.batchCountAccumulator).toEqual(0) - expect(output.transactionCount).toEqual(1) }) test('large batch of transactions', () => { const state = initialState @@ -532,6 +557,5 @@ describe('RECEIVED_NEW_TRANSACTION_BATCH', () => { const output = reducer(state, action) expect(output.newTransactions).toEqual([]) - expect(output.transactionCount).toEqual(11) }) }) diff --git a/apps/block_scout_web/assets/js/pages/address.js b/apps/block_scout_web/assets/js/pages/address.js index 9a6bc7e5ac..d8e7beb849 100644 --- a/apps/block_scout_web/assets/js/pages/address.js +++ b/apps/block_scout_web/assets/js/pages/address.js @@ -11,6 +11,18 @@ import { loadTokenBalanceDropdown } from '../lib/token_balance_dropdown' const BATCH_THRESHOLD = 10 +const incrementTransactionsCount = (transactions, addressHash, currentValue) => { + const reducer = (accumulator, {fromAddressHash}) => { + if (fromAddressHash === addressHash) { + accumulator++ + } + + return accumulator + } + + return transactions.reduce(reducer, currentValue) +} + export const initialState = { addressHash: null, balance: null, @@ -109,7 +121,7 @@ export function reducer (state = initialState, action) { case 'RECEIVED_NEW_TRANSACTION_BATCH': { if (state.channelDisconnected) return state - const transactionCount = state.transactionCount + action.msgs.length + const transactionCount = incrementTransactionsCount(action.msgs, state.addressHash, state.transactionCount) if (state.beyondPageOne) return Object.assign({}, state, { transactionCount })