fix(multicall reducer): add test and fix update multicall results

This commit is contained in:
Moody Salem 2020-05-30 03:10:49 -04:00
parent 73d3df05f2
commit f289dec684
No known key found for this signature in database
GPG Key ID: 8CB5CD10385138DB
2 changed files with 86 additions and 2 deletions

@ -1,4 +1,4 @@
import { addMulticallListeners, removeMulticallListeners } from './actions'
import { addMulticallListeners, removeMulticallListeners, updateMulticallResults } from './actions'
import reducer, { MulticallState } from './reducer'
import { Store, createStore } from '@reduxjs/toolkit'
@ -80,4 +80,88 @@ describe('multicall reducer', () => {
expect(store.getState()).toEqual({ callResults: {}, callListeners: { [1]: { '0x-0x': {} } } })
})
})
describe('updateMulticallResults', () => {
it('updates data if not present', () => {
store.dispatch(
updateMulticallResults({
chainId: 1,
blockNumber: 1,
results: {
abc: '0x'
}
})
)
expect(store.getState()).toEqual({
callResults: {
[1]: {
abc: {
blockNumber: 1,
data: '0x'
}
}
}
})
})
it('updates old data', () => {
store.dispatch(
updateMulticallResults({
chainId: 1,
blockNumber: 1,
results: {
abc: '0x'
}
})
)
store.dispatch(
updateMulticallResults({
chainId: 1,
blockNumber: 2,
results: {
abc: '0x2'
}
})
)
expect(store.getState()).toEqual({
callResults: {
[1]: {
abc: {
blockNumber: 2,
data: '0x2'
}
}
}
})
})
it('ignores late updates', () => {
store.dispatch(
updateMulticallResults({
chainId: 1,
blockNumber: 2,
results: {
abc: '0x2'
}
})
)
store.dispatch(
updateMulticallResults({
chainId: 1,
blockNumber: 1,
results: {
abc: '0x1'
}
})
)
expect(store.getState()).toEqual({
callResults: {
[1]: {
abc: {
blockNumber: 2,
data: '0x2'
}
}
}
})
})
})
})

@ -97,7 +97,7 @@ export default createReducer(initialState, builder =>
state.callResults[chainId] = state.callResults[chainId] ?? {}
Object.keys(results).forEach(callKey => {
const current = state.callResults[chainId][callKey]
if (current?.blockNumber ?? 0 > blockNumber) return
if ((current?.blockNumber ?? 0) > blockNumber) return
state.callResults[chainId][callKey] = {
data: results[callKey],
blockNumber