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', () => {
const state = initialState
@ -10,58 +10,48 @@ test('CHANNEL_DISCONNECTED', () => {
expect(output.channelDisconnected).toBe(true)
})
describe('PAGE_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([])
})
describe('ELEMENTS_LOAD', () => {
test('page 1 with skipped blocks', () => {
const state = initialState
window.localized = {}
const state = Object.assign({}, initialState, {
beyondPageOne: false
})
const action = {
type: 'PAGE_LOAD',
beyondPageOne: false,
blockNumbers: [4, 1]
type: 'ELEMENTS_LOAD',
blocks: [
{ blockNumber: 4, blockHtml: 'test 4' },
{ blockNumber: 1, blockHtml: 'test 1' }
]
}
const output = reducer(state, action)
expect(output.beyondPageOne).toBe(false)
expect(output.blockNumbers).toEqual([4, 3, 2, 1])
expect(output.skippedBlockNumbers).toEqual([3, 2])
expect(output.blocks).toEqual([
{ blockNumber: 4, blockHtml: 'test 4' },
{ blockNumber: 3, blockHtml: placeHolderBlock(3) },
{ blockNumber: 2, blockHtml: placeHolderBlock(2) },
{ blockNumber: 1, blockHtml: 'test 1' }
])
})
test('page 2 with skipped blocks', () => {
const state = initialState
window.localized = {}
const state = Object.assign({}, initialState, {
beyondPageOne: true
})
const action = {
type: 'PAGE_LOAD',
beyondPageOne: true,
blockNumbers: [4, 1]
type: 'ELEMENTS_LOAD',
blocks: [
{ blockNumber: 4, blockHtml: 'test 4' },
{ blockNumber: 1, blockHtml: 'test 1' }
]
}
const output = reducer(state, action)
expect(output.beyondPageOne).toBe(true)
expect(output.blockNumbers).toEqual([4, 3, 2, 1])
expect(output.skippedBlockNumbers).toEqual([3, 2])
expect(output.blocks).toEqual([
{ blockNumber: 4, blockHtml: 'test 4' },
{ 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)
expect(output.newBlock).toBe('test')
expect(output.blockNumbers).toEqual([1])
expect(output.blocks).toEqual([
{ blockNumber: 1, blockHtml: 'test' }
])
})
test('on page 2+', () => {
const state = Object.assign({}, initialState, {
@ -91,48 +82,37 @@ describe('RECEIVED_NEW_BLOCK', () => {
}
const output = reducer(state, action)
expect(output.newBlock).toBe(null)
expect(output.blockNumbers).toEqual([])
expect(output.skippedBlockNumbers).toEqual([])
expect(output.blocks).toEqual([])
})
test('inserts place holders if block received out of order', () => {
window.localized = {}
const state = Object.assign({}, initialState, {
blockNumbers: [2]
blocks: [
{ blockNumber: 2, blockHtml: 'test 2' }
]
})
const action = {
type: 'RECEIVED_NEW_BLOCK',
msg: {
blockHtml: 'test5',
blockHtml: 'test 5',
blockNumber: 5
}
}
const output = reducer(state, action)
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: [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])
expect(output.blocks).toEqual([
{ blockNumber: 5, blockHtml: 'test 5' },
{ blockNumber: 4, blockHtml: placeHolderBlock(4) },
{ blockNumber: 3, blockHtml: placeHolderBlock(3) },
{ blockNumber: 2, blockHtml: 'test 2' }
])
})
test('replaces duplicated block', () => {
const state = Object.assign({}, initialState, {
blockNumbers: [5, 4]
blocks: [
{ blockNumber: 5, blockHtml: 'test 5' },
{ blockNumber: 4, blockHtml: 'test 4' }
]
})
const action = {
type: 'RECEIVED_NEW_BLOCK',
@ -143,23 +123,34 @@ describe('RECEIVED_NEW_BLOCK', () => {
}
const output = reducer(state, action)
expect(output.newBlock).toBe('test5')
expect(output.blockNumbers).toEqual([5, 4])
expect(output.blocks).toEqual([
{ blockNumber: 5, blockHtml: 'test5' },
{ blockNumber: 4, blockHtml: 'test 4' }
])
})
test('skips if new block height is lower than lowest on page', () => {
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 = {
type: 'RECEIVED_NEW_BLOCK',
msg: {
blockHtml: 'test1',
blockNumber: 1
blockNumber: 1,
blockHtml: 'test 1'
}
}
const output = reducer(state, action)
expect(output.newBlock).toBe(null)
expect(output.blockNumbers).toEqual([5, 4, 3, 2])
expect(output.blocks).toEqual([
{ 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', () => {
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([])
})
describe('ELEMENTS_LOAD', () => {
test('loads with skipped blocks', () => {
window.localized = {}
const state = initialState
const action = {
type: 'PAGE_LOAD',
blockNumbers: [4, 1]
type: 'ELEMENTS_LOAD',
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)
expect(output.blockNumbers).toEqual([4, 3, 2, 1])
expect(output.skippedBlockNumbers).toEqual([3, 2])
expect(output.blocks).toEqual([
{ 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', () => {
const state = Object.assign({}, initialState, {
averageBlockTime: '6 seconds',
blockNumbers: [1],
newBlock: 'last new block'
blocks: [
{ blockNumber: 1, chainBlockHtml: 'test 1' },
{ blockNumber: 0, chainBlockHtml: 'test 0' }
]
})
const action = {
type: 'RECEIVED_NEW_BLOCK',
@ -58,121 +59,138 @@ describe('RECEIVED_NEW_BLOCK', () => {
const output = reducer(state, action)
expect(output.averageBlockTime).toEqual('5 seconds')
expect(output.newBlock).toEqual('new block')
expect(output.blockNumbers).toEqual([2, 1])
expect(output.blocks).toEqual([
{ blockNumber: 2, chainBlockHtml: 'new block', averageBlockTime: '5 seconds' },
{ blockNumber: 1, chainBlockHtml: 'test 1' }
])
})
test('inserts place holders if block received out of order', () => {
window.localized = {}
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 = {
type: 'RECEIVED_NEW_BLOCK',
msg: {
averageBlockTime: '5 seconds',
chainBlockHtml: 'test5',
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
chainBlockHtml: 'test 6',
blockNumber: 6
}
}
const output = reducer(state, action)
expect(output.averageBlockTime).toEqual('5 seconds')
expect(output.newBlock).toBe('test2')
expect(output.blockNumbers).toEqual([4, 3, 2, 1])
expect(output.skippedBlockNumbers).toEqual([3, 1])
expect(output.blocks).toEqual([
{ blockNumber: 6, chainBlockHtml: 'test 6' },
{ blockNumber: 5, chainBlockHtml: placeHolderBlock(5) },
{ blockNumber: 4, chainBlockHtml: placeHolderBlock(4) },
{ blockNumber: 3, chainBlockHtml: 'test 3' }
])
})
test('replaces duplicated block', () => {
const state = Object.assign({}, initialState, {
blockNumbers: [5, 4]
blocks: [
{ blockNumber: 5, chainBlockHtml: 'test 5' },
{ blockNumber: 4, chainBlockHtml: 'test 4' }
]
})
const action = {
type: 'RECEIVED_NEW_BLOCK',
msg: {
averageBlockTime: '5 seconds',
chainBlockHtml: 'test5',
blockNumber: 5
}
}
const output = reducer(state, action)
expect(output.averageBlockTime).toEqual('5 seconds')
expect(output.newBlock).toBe('test5')
expect(output.blockNumbers).toEqual([5, 4])
expect(output.blocks).toEqual([
{ blockNumber: 5, chainBlockHtml: 'test5' },
{ blockNumber: 4, chainBlockHtml: 'test 4' }
])
})
test('skips if new block height is lower than lowest on page', () => {
window.localized = {}
const state = Object.assign({}, initialState, {
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 = {
type: 'RECEIVED_NEW_BLOCK',
msg: {
averageBlockTime: '9 seconds',
chainBlockHtml: 'test1',
blockNumber: 1
blockNumber: 1,
chainBlockHtml: 'test 1'
}
}
const output = reducer(state, action)
expect(output.averageBlockTime).toEqual('5 seconds')
expect(output.newBlock).toBe(null)
expect(output.blockNumbers).toEqual([5, 4, 3, 2])
expect(output.blocks).toEqual([
{ 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', () => {
const state = Object.assign({}, initialState, {
blockNumbers: [5, 4, 3, 2],
skippedBlockNumbers: [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 = {
type: 'RECEIVED_NEW_BLOCK',
msg: {
chainBlockHtml: 'test6',
chainBlockHtml: 'test 6',
blockNumber: 6
}
}
const output = reducer(state, action)
expect(output.newBlock).toBe('test6')
expect(output.blockNumbers).toEqual([6, 5, 4, 3])
expect(output.skippedBlockNumbers).toEqual([4, 3])
expect(output.blocks).toEqual([
{ blockNumber: 6, chainBlockHtml: 'test 6' },
{ 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', () => {
window.localized = {}
const state = Object.assign({}, initialState, {
blockNumbers: [5, 4, 3, 2],
skippedBlockNumbers: [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 = {
type: 'RECEIVED_NEW_BLOCK',
msg: {
chainBlockHtml: 'test10',
blockNumber: 10
blockNumber: 10,
chainBlockHtml: 'test 10'
}
}
const output = reducer(state, action)
expect(output.newBlock).toBe('test10')
expect(output.blockNumbers).toEqual([10, 9, 8, 7])
expect(output.skippedBlockNumbers).toEqual([9, 8, 7])
expect(output.blocks).toEqual([
{ blockNumber: 10, chainBlockHtml: 'test 10' },
{ 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 = {
type: 'RECEIVED_NEW_TRANSACTION',
msg: {
transactionHash: '0x01',
transactionHtml: 'test'
}
}
const output = reducer(state, action)
expect(output.newTransactions).toEqual(['test'])
expect(output.transactions).toEqual([
{ transactionHash: '0x01', transactionHtml: 'test' }
])
expect(output.transactionCount).toEqual(1)
})
test('single transaction after single transaction', () => {
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 = {
type: 'RECEIVED_NEW_TRANSACTION',
msg: {
transactionHtml: 'test 2'
transactionHash: '0x05',
transactionHtml: 'test 5'
}
}
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': {
if (state.channelDisconnected || state.beyondPageOne) return state
if (_.find(state.blocks, { blockNumber: action.msg.blockNumber })) {
return Object.assign({}, state, {
blocks: state.blocks.map((block) => block.blockNumber === action.msg.blockNumber ? action.msg : block)
})
} else {
if (!state.blocks.length || state.blocks[0].blockNumber < action.msg.blockNumber) {
return Object.assign({}, state, {
blocks: [
action.msg,
...state.blocks
]
})
} else {
return Object.assign({}, state, {
blocks: state.blocks.map((block) => block.blockNumber === action.msg.blockNumber ? action.msg : block)
})
}
}
default:
@ -110,7 +110,7 @@ if ($blockListPage.length) {
}))
}
function placeHolderBlock (blockNumber) {
export function placeHolderBlock (blockNumber) {
return `
<div class="my-3" style="height: 98px;" data-selector="place-holder" data-block-number="${blockNumber}">
<div

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

Loading…
Cancel
Save