Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomquirk committed Jan 23, 2024
1 parent e777d38 commit 657e3fc
Showing 1 changed file with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,20 @@ describe('useCountdownClock', () => {
const now = Date.now() / 1000
const tenSecondsFromNow = now + 10
const { result } = renderHook(() => useCountdownClock(tenSecondsFromNow))
expect(result.current).toEqual('0d 0h 0m 10s')
expect(result.current).toEqual({
remainingTimeText: '0d 0h 0m 10s',
secondsRemaining: 10,
})
})
it('updates every second', () => {
const now = Date.now() / 1000
const tenSecondsFromNow = now + 10
const { result } = renderHook(() => useCountdownClock(tenSecondsFromNow))
for (let i = 10; i >= 0; i--) {
expect(result.current).toEqual(`0d 0h 0m ${i}s`)
expect(result.current).toEqual({
remainingTimeText: `0d 0h 0m ${i}s`,
secondsRemaining: i,
})
act(() => jest.advanceTimersByTime(1000))
}
})
Expand All @@ -32,7 +38,10 @@ describe('useCountdownClock', () => {
const now = Date.now() / 1000
const tenSecondsAgo = now - 10
const { result } = renderHook(() => useCountdownClock(tenSecondsAgo))
expect(result.current).toEqual('0d 0h 0m 0s')
expect(result.current).toEqual({
remainingTimeText: '0d 0h 0m 0s',
secondsRemaining: 0,
})
})

test('timer is cleared on unmount', () => {
Expand All @@ -41,15 +50,24 @@ describe('useCountdownClock', () => {
const { result, unmount } = renderHook(() =>
useCountdownClock(tenSecondsFromNow),
)
expect(result.current).toEqual('0d 0h 0m 10s')
expect(result.current).toEqual({
remainingTimeText: '0d 0h 0m 10s',
secondsRemaining: 10,
})
act(() => {
jest.advanceTimersByTime(1000)
})
expect(result.current).toEqual('0d 0h 0m 9s')
expect(result.current).toEqual({
remainingTimeText: '0d 0h 0m 9s',
secondsRemaining: 9,
})
unmount()
act(() => {
jest.advanceTimersByTime(1000)
})
expect(result.current).toEqual('0d 0h 0m 9s')
expect(result.current).toEqual({
remainingTimeText: '0d 0h 0m 9s',
secondsRemaining: 9,
})
})
})

0 comments on commit 657e3fc

Please sign in to comment.