Skip to content

Commit

Permalink
fix: use fake timers
Browse files Browse the repository at this point in the history
  • Loading branch information
iamacook committed Nov 8, 2023
1 parent e6b1f9e commit 234d5ff
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/hooks/useBlockTimestamp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ describe('useBlockTimestamp', () => {
})

it('should update the timestamp every INTERVAL', async () => {
jest.useFakeTimers()

const timestamp = 69420

mockGetBlock.mockResolvedValue({
Expand All @@ -63,15 +65,21 @@ describe('useBlockTimestamp', () => {
expect(result.current).toBe(timestamp)
})

jest.advanceTimersByTime(1_000)

await waitFor(() => {
expect(result.current).toBe(timestamp + 1)
})

jest.advanceTimersByTime(1_000)

await waitFor(() => {
expect(result.current).toBe(timestamp + 2)
})

// Interval is used to update the timestamp after initial getBlock call
expect(mockGetBlock).toHaveBeenCalledTimes(1)

jest.useRealTimers()
})
})
4 changes: 2 additions & 2 deletions src/hooks/useBlockTimestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function useBlockTimestamp(): number | undefined {

const timeout = setInterval(() => {
setTimestamp((prev) => {
return prev ? prev + 1 : prev
return prev ? prev + 1 : block.timestamp
})
}, INTERVAL)

Expand All @@ -32,5 +32,5 @@ export function useBlockTimestamp(): number | undefined {
}
}, [block])

return timestamp ?? block?.timestamp
return timestamp
}

0 comments on commit 234d5ff

Please sign in to comment.