Finish updating the tests

pull/1049/head
jimmay5469 6 years ago
parent 9f1e75fb60
commit 6c6f2641ab
  1. 147
      apps/block_scout_web/assets/__tests__/pages/blocks.js
  2. 184
      apps/block_scout_web/assets/__tests__/pages/chain.js
  3. 12
      apps/block_scout_web/assets/js/pages/blocks.js
  4. 17
      apps/block_scout_web/assets/js/pages/chain.js

@ -1,4 +1,4 @@
import { reducer, initialState } from '../../js/pages/blocks' import { reducer, initialState, placeHolderBlock } from '../../js/pages/blocks'
test('CHANNEL_DISCONNECTED', () => { test('CHANNEL_DISCONNECTED', () => {
const state = initialState const state = initialState
@ -10,58 +10,48 @@ test('CHANNEL_DISCONNECTED', () => {
expect(output.channelDisconnected).toBe(true) expect(output.channelDisconnected).toBe(true)
}) })
describe('PAGE_LOAD', () => { describe('ELEMENTS_LOAD', () => {
test('page 1 loads block numbers', () => {
const state = initialState
const action = {
type: 'PAGE_LOAD',
beyondPageOne: false,
blockNumbers: [2, 1]
}
const output = reducer(state, action)
expect(output.beyondPageOne).toBe(false)
expect(output.blockNumbers).toEqual([2, 1])
expect(output.skippedBlockNumbers).toEqual([])
})
test('page 2 loads block numbers', () => {
const state = initialState
const action = {
type: 'PAGE_LOAD',
beyondPageOne: true,
blockNumbers: [2, 1]
}
const output = reducer(state, action)
expect(output.beyondPageOne).toBe(true)
expect(output.blockNumbers).toEqual([2, 1])
expect(output.skippedBlockNumbers).toEqual([])
})
test('page 1 with skipped blocks', () => { test('page 1 with skipped blocks', () => {
const state = initialState window.localized = {}
const state = Object.assign({}, initialState, {
beyondPageOne: false
})
const action = { const action = {
type: 'PAGE_LOAD', type: 'ELEMENTS_LOAD',
beyondPageOne: false, blocks: [
blockNumbers: [4, 1] { blockNumber: 4, blockHtml: 'test 4' },
{ blockNumber: 1, blockHtml: 'test 1' }
]
} }
const output = reducer(state, action) const output = reducer(state, action)
expect(output.beyondPageOne).toBe(false) expect(output.blocks).toEqual([
expect(output.blockNumbers).toEqual([4, 3, 2, 1]) { blockNumber: 4, blockHtml: 'test 4' },
expect(output.skippedBlockNumbers).toEqual([3, 2]) { blockNumber: 3, blockHtml: placeHolderBlock(3) },
{ blockNumber: 2, blockHtml: placeHolderBlock(2) },
{ blockNumber: 1, blockHtml: 'test 1' }
])
}) })
test('page 2 with skipped blocks', () => { test('page 2 with skipped blocks', () => {
const state = initialState window.localized = {}
const state = Object.assign({}, initialState, {
beyondPageOne: true
})
const action = { const action = {
type: 'PAGE_LOAD', type: 'ELEMENTS_LOAD',
beyondPageOne: true, blocks: [
blockNumbers: [4, 1] { blockNumber: 4, blockHtml: 'test 4' },
{ blockNumber: 1, blockHtml: 'test 1' }
]
} }
const output = reducer(state, action) const output = reducer(state, action)
expect(output.beyondPageOne).toBe(true) expect(output.blocks).toEqual([
expect(output.blockNumbers).toEqual([4, 3, 2, 1]) { blockNumber: 4, blockHtml: 'test 4' },
expect(output.skippedBlockNumbers).toEqual([3, 2]) { blockNumber: 3, blockHtml: placeHolderBlock(3) },
{ blockNumber: 2, blockHtml: placeHolderBlock(2) },
{ blockNumber: 1, blockHtml: 'test 1' }
])
}) })
}) })
@ -76,8 +66,9 @@ describe('RECEIVED_NEW_BLOCK', () => {
} }
const output = reducer(initialState, action) const output = reducer(initialState, action)
expect(output.newBlock).toBe('test') expect(output.blocks).toEqual([
expect(output.blockNumbers).toEqual([1]) { blockNumber: 1, blockHtml: 'test' }
])
}) })
test('on page 2+', () => { test('on page 2+', () => {
const state = Object.assign({}, initialState, { const state = Object.assign({}, initialState, {
@ -91,48 +82,37 @@ describe('RECEIVED_NEW_BLOCK', () => {
} }
const output = reducer(state, action) const output = reducer(state, action)
expect(output.newBlock).toBe(null) expect(output.blocks).toEqual([])
expect(output.blockNumbers).toEqual([])
expect(output.skippedBlockNumbers).toEqual([])
}) })
test('inserts place holders if block received out of order', () => { test('inserts place holders if block received out of order', () => {
window.localized = {}
const state = Object.assign({}, initialState, { const state = Object.assign({}, initialState, {
blockNumbers: [2] blocks: [
{ blockNumber: 2, blockHtml: 'test 2' }
]
}) })
const action = { const action = {
type: 'RECEIVED_NEW_BLOCK', type: 'RECEIVED_NEW_BLOCK',
msg: { msg: {
blockHtml: 'test5', blockHtml: 'test 5',
blockNumber: 5 blockNumber: 5
} }
} }
const output = reducer(state, action) const output = reducer(state, action)
expect(output.newBlock).toBe('test5') expect(output.blocks).toEqual([
expect(output.blockNumbers).toEqual([5, 4, 3, 2]) { blockNumber: 5, blockHtml: 'test 5' },
expect(output.skippedBlockNumbers).toEqual([4, 3]) { blockNumber: 4, blockHtml: placeHolderBlock(4) },
}) { blockNumber: 3, blockHtml: placeHolderBlock(3) },
test('replaces skipped block', () => { { blockNumber: 2, blockHtml: 'test 2' }
const state = Object.assign({}, initialState, { ])
blockNumbers: [5, 4, 3, 2, 1],
skippedBlockNumbers: [4, 3, 1]
})
const action = {
type: 'RECEIVED_NEW_BLOCK',
msg: {
blockHtml: 'test3',
blockNumber: 3
}
}
const output = reducer(state, action)
expect(output.newBlock).toBe('test3')
expect(output.blockNumbers).toEqual([5, 4, 3, 2, 1])
expect(output.skippedBlockNumbers).toEqual([4, 1])
}) })
test('replaces duplicated block', () => { test('replaces duplicated block', () => {
const state = Object.assign({}, initialState, { const state = Object.assign({}, initialState, {
blockNumbers: [5, 4] blocks: [
{ blockNumber: 5, blockHtml: 'test 5' },
{ blockNumber: 4, blockHtml: 'test 4' }
]
}) })
const action = { const action = {
type: 'RECEIVED_NEW_BLOCK', type: 'RECEIVED_NEW_BLOCK',
@ -143,23 +123,34 @@ describe('RECEIVED_NEW_BLOCK', () => {
} }
const output = reducer(state, action) const output = reducer(state, action)
expect(output.newBlock).toBe('test5') expect(output.blocks).toEqual([
expect(output.blockNumbers).toEqual([5, 4]) { blockNumber: 5, blockHtml: 'test5' },
{ blockNumber: 4, blockHtml: 'test 4' }
])
}) })
test('skips if new block height is lower than lowest on page', () => { test('skips if new block height is lower than lowest on page', () => {
const state = Object.assign({}, initialState, { const state = Object.assign({}, initialState, {
blockNumbers: [5, 4, 3, 2] blocks: [
{ blockNumber: 5, blockHtml: 'test 5' },
{ blockNumber: 4, blockHtml: 'test 4' },
{ blockNumber: 3, blockHtml: 'test 3' },
{ blockNumber: 2, blockHtml: 'test 2' }
]
}) })
const action = { const action = {
type: 'RECEIVED_NEW_BLOCK', type: 'RECEIVED_NEW_BLOCK',
msg: { msg: {
blockHtml: 'test1', blockNumber: 1,
blockNumber: 1 blockHtml: 'test 1'
} }
} }
const output = reducer(state, action) const output = reducer(state, action)
expect(output.newBlock).toBe(null) expect(output.blocks).toEqual([
expect(output.blockNumbers).toEqual([5, 4, 3, 2]) { blockNumber: 5, blockHtml: 'test 5' },
{ blockNumber: 4, blockHtml: 'test 4' },
{ blockNumber: 3, blockHtml: 'test 3' },
{ blockNumber: 2, blockHtml: 'test 2' }
])
}) })
}) })

@ -1,27 +1,26 @@
import { reducer, initialState } from '../../js/pages/chain' import { reducer, initialState, placeHolderBlock } from '../../js/pages/chain'
describe('PAGE_LOAD', () => { describe('ELEMENTS_LOAD', () => {
test('loads block numbers', () => {
const state = initialState
const action = {
type: 'PAGE_LOAD',
blockNumbers: [2, 1]
}
const output = reducer(state, action)
expect(output.blockNumbers).toEqual([2, 1])
expect(output.skippedBlockNumbers).toEqual([])
})
test('loads with skipped blocks', () => { test('loads with skipped blocks', () => {
window.localized = {}
const state = initialState const state = initialState
const action = { const action = {
type: 'PAGE_LOAD', type: 'ELEMENTS_LOAD',
blockNumbers: [4, 1] blocks: [
{ blockNumber: 6, chainBlockHtml: 'test 6' },
{ blockNumber: 3, chainBlockHtml: 'test 3' },
{ blockNumber: 2, chainBlockHtml: 'test 2' },
{ blockNumber: 1, chainBlockHtml: 'test 1' }
]
} }
const output = reducer(state, action) const output = reducer(state, action)
expect(output.blockNumbers).toEqual([4, 3, 2, 1]) expect(output.blocks).toEqual([
expect(output.skippedBlockNumbers).toEqual([3, 2]) { blockNumber: 6, chainBlockHtml: 'test 6' },
{ blockNumber: 5, chainBlockHtml: placeHolderBlock(5) },
{ blockNumber: 4, chainBlockHtml: placeHolderBlock(4) },
{ blockNumber: 3, chainBlockHtml: 'test 3' }
])
}) })
}) })
@ -44,8 +43,10 @@ describe('RECEIVED_NEW_BLOCK', () => {
test('receives new block', () => { test('receives new block', () => {
const state = Object.assign({}, initialState, { const state = Object.assign({}, initialState, {
averageBlockTime: '6 seconds', averageBlockTime: '6 seconds',
blockNumbers: [1], blocks: [
newBlock: 'last new block' { blockNumber: 1, chainBlockHtml: 'test 1' },
{ blockNumber: 0, chainBlockHtml: 'test 0' }
]
}) })
const action = { const action = {
type: 'RECEIVED_NEW_BLOCK', type: 'RECEIVED_NEW_BLOCK',
@ -58,121 +59,138 @@ describe('RECEIVED_NEW_BLOCK', () => {
const output = reducer(state, action) const output = reducer(state, action)
expect(output.averageBlockTime).toEqual('5 seconds') expect(output.averageBlockTime).toEqual('5 seconds')
expect(output.newBlock).toEqual('new block') expect(output.blocks).toEqual([
expect(output.blockNumbers).toEqual([2, 1]) { blockNumber: 2, chainBlockHtml: 'new block', averageBlockTime: '5 seconds' },
{ blockNumber: 1, chainBlockHtml: 'test 1' }
])
}) })
test('inserts place holders if block received out of order', () => { test('inserts place holders if block received out of order', () => {
window.localized = {}
const state = Object.assign({}, initialState, { const state = Object.assign({}, initialState, {
blockNumbers: [2] blocks: [
{ blockNumber: 3, chainBlockHtml: 'test 3' },
{ blockNumber: 2, chainBlockHtml: 'test 2' },
{ blockNumber: 1, chainBlockHtml: 'test 1' },
{ blockNumber: 0, chainBlockHtml: 'test 0' }
]
}) })
const action = { const action = {
type: 'RECEIVED_NEW_BLOCK', type: 'RECEIVED_NEW_BLOCK',
msg: { msg: {
averageBlockTime: '5 seconds', chainBlockHtml: 'test 6',
chainBlockHtml: 'test5', blockNumber: 6
blockNumber: 5
}
}
const output = reducer(state, action)
expect(output.averageBlockTime).toEqual('5 seconds')
expect(output.newBlock).toBe('test5')
expect(output.blockNumbers).toEqual([5, 4, 3, 2])
expect(output.skippedBlockNumbers).toEqual([4, 3])
})
test('replaces skipped block', () => {
const state = Object.assign({}, initialState, {
blockNumbers: [4, 3, 2, 1],
skippedBlockNumbers: [3, 2, 1]
})
const action = {
type: 'RECEIVED_NEW_BLOCK',
msg: {
averageBlockTime: '5 seconds',
chainBlockHtml: 'test2',
blockNumber: 2
} }
} }
const output = reducer(state, action) const output = reducer(state, action)
expect(output.averageBlockTime).toEqual('5 seconds') expect(output.blocks).toEqual([
expect(output.newBlock).toBe('test2') { blockNumber: 6, chainBlockHtml: 'test 6' },
expect(output.blockNumbers).toEqual([4, 3, 2, 1]) { blockNumber: 5, chainBlockHtml: placeHolderBlock(5) },
expect(output.skippedBlockNumbers).toEqual([3, 1]) { blockNumber: 4, chainBlockHtml: placeHolderBlock(4) },
{ blockNumber: 3, chainBlockHtml: 'test 3' }
])
}) })
test('replaces duplicated block', () => { test('replaces duplicated block', () => {
const state = Object.assign({}, initialState, { const state = Object.assign({}, initialState, {
blockNumbers: [5, 4] blocks: [
{ blockNumber: 5, chainBlockHtml: 'test 5' },
{ blockNumber: 4, chainBlockHtml: 'test 4' }
]
}) })
const action = { const action = {
type: 'RECEIVED_NEW_BLOCK', type: 'RECEIVED_NEW_BLOCK',
msg: { msg: {
averageBlockTime: '5 seconds',
chainBlockHtml: 'test5', chainBlockHtml: 'test5',
blockNumber: 5 blockNumber: 5
} }
} }
const output = reducer(state, action) const output = reducer(state, action)
expect(output.averageBlockTime).toEqual('5 seconds') expect(output.blocks).toEqual([
expect(output.newBlock).toBe('test5') { blockNumber: 5, chainBlockHtml: 'test5' },
expect(output.blockNumbers).toEqual([5, 4]) { blockNumber: 4, chainBlockHtml: 'test 4' }
])
}) })
test('skips if new block height is lower than lowest on page', () => { test('skips if new block height is lower than lowest on page', () => {
window.localized = {}
const state = Object.assign({}, initialState, { const state = Object.assign({}, initialState, {
averageBlockTime: '5 seconds', averageBlockTime: '5 seconds',
blockNumbers: [5, 4, 3, 2] blocks: [
{ blockNumber: 5, chainBlockHtml: 'test 5' },
{ blockNumber: 4, chainBlockHtml: 'test 4' },
{ blockNumber: 3, chainBlockHtml: 'test 3' },
{ blockNumber: 2, chainBlockHtml: 'test 2' }
]
}) })
const action = { const action = {
type: 'RECEIVED_NEW_BLOCK', type: 'RECEIVED_NEW_BLOCK',
msg: { msg: {
averageBlockTime: '9 seconds', averageBlockTime: '9 seconds',
chainBlockHtml: 'test1', blockNumber: 1,
blockNumber: 1 chainBlockHtml: 'test 1'
} }
} }
const output = reducer(state, action) const output = reducer(state, action)
expect(output.averageBlockTime).toEqual('5 seconds') expect(output.averageBlockTime).toEqual('5 seconds')
expect(output.newBlock).toBe(null) expect(output.blocks).toEqual([
expect(output.blockNumbers).toEqual([5, 4, 3, 2]) { blockNumber: 5, chainBlockHtml: 'test 5' },
{ blockNumber: 4, chainBlockHtml: 'test 4' },
{ blockNumber: 3, chainBlockHtml: 'test 3' },
{ blockNumber: 2, chainBlockHtml: 'test 2' }
])
}) })
test('only tracks 4 blocks based on page display limit', () => { test('only tracks 4 blocks based on page display limit', () => {
const state = Object.assign({}, initialState, { const state = Object.assign({}, initialState, {
blockNumbers: [5, 4, 3, 2], blocks: [
skippedBlockNumbers: [4, 3, 2] { blockNumber: 5, chainBlockHtml: 'test 5' },
{ blockNumber: 4, chainBlockHtml: 'test 4' },
{ blockNumber: 3, chainBlockHtml: 'test 3' },
{ blockNumber: 2, chainBlockHtml: 'test 2' }
]
}) })
const action = { const action = {
type: 'RECEIVED_NEW_BLOCK', type: 'RECEIVED_NEW_BLOCK',
msg: { msg: {
chainBlockHtml: 'test6', chainBlockHtml: 'test 6',
blockNumber: 6 blockNumber: 6
} }
} }
const output = reducer(state, action) const output = reducer(state, action)
expect(output.newBlock).toBe('test6') expect(output.blocks).toEqual([
expect(output.blockNumbers).toEqual([6, 5, 4, 3]) { blockNumber: 6, chainBlockHtml: 'test 6' },
expect(output.skippedBlockNumbers).toEqual([4, 3]) { blockNumber: 5, chainBlockHtml: 'test 5' },
{ blockNumber: 4, chainBlockHtml: 'test 4' },
{ blockNumber: 3, chainBlockHtml: 'test 3' }
])
}) })
test('skipped blocks list replaced when another block comes in with +3 blockheight', () => { test('skipped blocks list replaced when another block comes in with +3 blockheight', () => {
window.localized = {}
const state = Object.assign({}, initialState, { const state = Object.assign({}, initialState, {
blockNumbers: [5, 4, 3, 2], blocks: [
skippedBlockNumbers: [4, 3, 2] { blockNumber: 5, chainBlockHtml: 'test 5' },
{ blockNumber: 4, chainBlockHtml: 'test 4' },
{ blockNumber: 3, chainBlockHtml: 'test 3' },
{ blockNumber: 2, chainBlockHtml: 'test 2' }
]
}) })
const action = { const action = {
type: 'RECEIVED_NEW_BLOCK', type: 'RECEIVED_NEW_BLOCK',
msg: { msg: {
chainBlockHtml: 'test10', blockNumber: 10,
blockNumber: 10 chainBlockHtml: 'test 10'
} }
} }
const output = reducer(state, action) const output = reducer(state, action)
expect(output.newBlock).toBe('test10') expect(output.blocks).toEqual([
expect(output.blockNumbers).toEqual([10, 9, 8, 7]) { blockNumber: 10, chainBlockHtml: 'test 10' },
expect(output.skippedBlockNumbers).toEqual([9, 8, 7]) { blockNumber: 9, chainBlockHtml: placeHolderBlock(9) },
{ blockNumber: 8, chainBlockHtml: placeHolderBlock(8) },
{ blockNumber: 7, chainBlockHtml: placeHolderBlock(7) }
])
}) })
}) })
@ -201,26 +219,40 @@ describe('RECEIVED_NEW_TRANSACTION', () => {
const action = { const action = {
type: 'RECEIVED_NEW_TRANSACTION', type: 'RECEIVED_NEW_TRANSACTION',
msg: { msg: {
transactionHash: '0x01',
transactionHtml: 'test' transactionHtml: 'test'
} }
} }
const output = reducer(state, action) const output = reducer(state, action)
expect(output.newTransactions).toEqual(['test']) expect(output.transactions).toEqual([
{ transactionHash: '0x01', transactionHtml: 'test' }
])
expect(output.transactionCount).toEqual(1) expect(output.transactionCount).toEqual(1)
}) })
test('single transaction after single transaction', () => { test('single transaction after single transaction', () => {
const state = Object.assign({}, initialState, { const state = Object.assign({}, initialState, {
newTransactions: ['test 1'] transactions: [
{ transactionHash: '0x04', transactionHtml: 'test 4' },
{ transactionHash: '0x03', transactionHtml: 'test 3' },
{ transactionHash: '0x02', transactionHtml: 'test 2' },
{ transactionHash: '0x01', transactionHtml: 'test 1' }
]
}) })
const action = { const action = {
type: 'RECEIVED_NEW_TRANSACTION', type: 'RECEIVED_NEW_TRANSACTION',
msg: { msg: {
transactionHtml: 'test 2' transactionHash: '0x05',
transactionHtml: 'test 5'
} }
} }
const output = reducer(state, action) const output = reducer(state, action)
expect(output.newTransactions).toEqual(['test 1', 'test 2']) expect(output.transactions).toEqual([
{ transactionHash: '0x05', transactionHtml: 'test 5' },
{ transactionHash: '0x04', transactionHtml: 'test 4' },
{ transactionHash: '0x03', transactionHtml: 'test 3' },
{ transactionHash: '0x02', transactionHtml: 'test 2' }
])
}) })
}) })

@ -29,17 +29,17 @@ function baseReducer (state = initialState, action) {
case 'RECEIVED_NEW_BLOCK': { case 'RECEIVED_NEW_BLOCK': {
if (state.channelDisconnected || state.beyondPageOne) return state if (state.channelDisconnected || state.beyondPageOne) return state
if (_.find(state.blocks, { blockNumber: action.msg.blockNumber })) { if (!state.blocks.length || state.blocks[0].blockNumber < action.msg.blockNumber) {
return Object.assign({}, state, {
blocks: state.blocks.map((block) => block.blockNumber === action.msg.blockNumber ? action.msg : block)
})
} else {
return Object.assign({}, state, { return Object.assign({}, state, {
blocks: [ blocks: [
action.msg, action.msg,
...state.blocks ...state.blocks
] ]
}) })
} else {
return Object.assign({}, state, {
blocks: state.blocks.map((block) => block.blockNumber === action.msg.blockNumber ? action.msg : block)
})
} }
} }
default: default:
@ -110,7 +110,7 @@ if ($blockListPage.length) {
})) }))
} }
function placeHolderBlock (blockNumber) { export function placeHolderBlock (blockNumber) {
return ` return `
<div class="my-3" style="height: 98px;" data-selector="place-holder" data-block-number="${blockNumber}"> <div class="my-3" style="height: 98px;" data-selector="place-holder" data-block-number="${blockNumber}">
<div <div

@ -12,8 +12,8 @@ export const initialState = {
availableSupply: null, availableSupply: null,
averageBlockTime: null, averageBlockTime: null,
marketHistoryData: null, marketHistoryData: null,
blocks: null, blocks: [],
transactions: null, transactions: [],
transactionCount: null, transactionCount: null,
usdMarketCap: null usdMarketCap: null
} }
@ -31,12 +31,7 @@ function baseReducer (state = initialState, action) {
}) })
} }
case 'RECEIVED_NEW_BLOCK': { case 'RECEIVED_NEW_BLOCK': {
if (_.find(state.blocks, { blockNumber: action.msg.blockNumber })) { if (!state.blocks.length || state.blocks[0].blockNumber < action.msg.blockNumber) {
return Object.assign({}, state, {
averageBlockTime: action.msg.averageBlockTime,
blocks: state.blocks.map((block) => block.blockNumber === action.msg.blockNumber ? action.msg : block)
})
} else {
return Object.assign({}, state, { return Object.assign({}, state, {
averageBlockTime: action.msg.averageBlockTime, averageBlockTime: action.msg.averageBlockTime,
blocks: [ blocks: [
@ -44,6 +39,10 @@ function baseReducer (state = initialState, action) {
...state.blocks.slice(0, -1) ...state.blocks.slice(0, -1)
] ]
}) })
} else {
return Object.assign({}, state, {
blocks: state.blocks.map((block) => block.blockNumber === action.msg.blockNumber ? action.msg : block)
})
} }
} }
case 'RECEIVED_NEW_EXCHANGE_RATE': { case 'RECEIVED_NEW_EXCHANGE_RATE': {
@ -190,7 +189,7 @@ if ($chainDetailsPage.length) {
})) }))
} }
function placeHolderBlock (blockNumber) { export function placeHolderBlock (blockNumber) {
return ` return `
<div <div
class="col-lg-3 fade-up-blocks-chain" class="col-lg-3 fade-up-blocks-chain"

Loading…
Cancel
Save