Skip to content

Commit

Permalink
fix(multicall reducer): add test and fix update multicall results
Browse files Browse the repository at this point in the history
  • Loading branch information
moodysalem committed May 30, 2020
1 parent 73d3df0 commit f289dec
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
86 changes: 85 additions & 1 deletion src/state/multicall/reducer.test.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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'
}
}
}
})
})
})
})
2 changes: 1 addition & 1 deletion src/state/multicall/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f289dec

Please sign in to comment.