Commit f289dec6 authored by Moody Salem's avatar Moody Salem

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

parent 73d3df05
import { addMulticallListeners, removeMulticallListeners } from './actions' import { addMulticallListeners, removeMulticallListeners, updateMulticallResults } from './actions'
import reducer, { MulticallState } from './reducer' import reducer, { MulticallState } from './reducer'
import { Store, createStore } from '@reduxjs/toolkit' import { Store, createStore } from '@reduxjs/toolkit'
...@@ -80,4 +80,88 @@ describe('multicall reducer', () => { ...@@ -80,4 +80,88 @@ describe('multicall reducer', () => {
expect(store.getState()).toEqual({ callResults: {}, callListeners: { [1]: { '0x-0x': {} } } }) 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 => ...@@ -97,7 +97,7 @@ export default createReducer(initialState, builder =>
state.callResults[chainId] = state.callResults[chainId] ?? {} state.callResults[chainId] = state.callResults[chainId] ?? {}
Object.keys(results).forEach(callKey => { Object.keys(results).forEach(callKey => {
const current = state.callResults[chainId][callKey] const current = state.callResults[chainId][callKey]
if (current?.blockNumber ?? 0 > blockNumber) return if ((current?.blockNumber ?? 0) > blockNumber) return
state.callResults[chainId][callKey] = { state.callResults[chainId][callKey] = {
data: results[callKey], data: results[callKey],
blockNumber blockNumber
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment