-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
24fde95
commit 9078d52
Showing
4 changed files
with
179 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { renderHook, act } from '@testing-library/react' | ||
import { useInterval } from '../use-interval' | ||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' | ||
|
||
describe('useInterval', () => { | ||
beforeEach(() => { | ||
vi.useFakeTimers() | ||
}) | ||
|
||
afterEach(() => { | ||
vi.restoreAllMocks() | ||
}) | ||
|
||
it('should call the callback function at the specified interval', () => { | ||
const callback = vi.fn() | ||
const { result } = renderHook(() => useInterval(callback, 1000)) | ||
|
||
expect(callback).not.toBeCalled() | ||
|
||
act(() => { | ||
vi.advanceTimersByTime(1000) | ||
}) | ||
expect(callback).toHaveBeenCalledTimes(1) | ||
|
||
act(() => { | ||
vi.advanceTimersByTime(1000) | ||
}) | ||
expect(callback).toHaveBeenCalledTimes(2) | ||
}) | ||
|
||
it('should stop and start the interval', () => { | ||
const callback = vi.fn() | ||
const { result } = renderHook(() => useInterval(callback, 1000)) | ||
|
||
act(() => { | ||
vi.advanceTimersByTime(1000) | ||
}) | ||
expect(callback).toHaveBeenCalledTimes(1) | ||
|
||
act(() => { | ||
result.current.stopInterval() | ||
}) | ||
|
||
act(() => { | ||
vi.advanceTimersByTime(2000) | ||
}) | ||
expect(callback).toHaveBeenCalledTimes(1) | ||
|
||
act(() => { | ||
result.current.startInterval() | ||
}) | ||
|
||
act(() => { | ||
vi.advanceTimersByTime(1000) | ||
}) | ||
expect(callback).toHaveBeenCalledTimes(2) | ||
}) | ||
|
||
it('should update the delay when updateDelay is called', () => { | ||
const callback = vi.fn() | ||
const { result } = renderHook(() => useInterval(callback, 1000)) | ||
|
||
act(() => { | ||
vi.advanceTimersByTime(1000) | ||
}) | ||
expect(callback).toHaveBeenCalledTimes(1) | ||
|
||
act(() => { | ||
result.current.updateDelay(500) | ||
}) | ||
|
||
act(() => { | ||
vi.advanceTimersByTime(500) | ||
}) | ||
expect(callback).toHaveBeenCalledTimes(2) | ||
}) | ||
|
||
it('should provide correct isRunning and delay values', () => { | ||
const callback = vi.fn() | ||
const { result } = renderHook(() => useInterval(callback, 1000)) | ||
|
||
expect(result.current.isRunning).toBe(true) | ||
expect(result.current.delay).toBe(1000) | ||
|
||
act(() => { | ||
result.current.stopInterval() | ||
}) | ||
expect(result.current.isRunning).toBe(false) | ||
|
||
act(() => { | ||
result.current.updateDelay(2000) | ||
}) | ||
expect(result.current.delay).toBe(2000) | ||
|
||
act(() => { | ||
result.current.startInterval() | ||
}) | ||
expect(result.current.isRunning).toBe(true) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { useEffect, useRef, useState, useCallback } from 'react'; | ||
|
||
type IntervalFunction = () => void; | ||
|
||
interface IntervalHookResult { | ||
isRunning: boolean; | ||
delay: number; | ||
startInterval: () => void; | ||
stopInterval: () => void; | ||
updateDelay: (newDelay: number) => void; | ||
} | ||
|
||
export const useInterval = (initialCallback: IntervalFunction, initialDelay: number): IntervalHookResult => { | ||
const [isRunning, setIsRunning] = useState<boolean>(true); | ||
const [delay, setDelay] = useState<number>(initialDelay); | ||
const savedCallback = useRef<IntervalFunction>(initialCallback); | ||
|
||
useEffect(() => { | ||
savedCallback.current = initialCallback; | ||
}, [initialCallback]); | ||
|
||
useEffect(() => { | ||
if (!isRunning) return undefined; | ||
|
||
const id = setInterval(() => savedCallback.current(), delay); | ||
|
||
return () => clearInterval(id); | ||
}, [delay, isRunning]); | ||
|
||
const startInterval = useCallback((): void => { | ||
setIsRunning(true); | ||
}, []); | ||
|
||
const stopInterval = useCallback((): void => { | ||
setIsRunning(false); | ||
}, []); | ||
|
||
const updateDelay = useCallback((newDelay: number): void => { | ||
setDelay(newDelay); | ||
}, []); | ||
|
||
return { | ||
isRunning, | ||
delay, | ||
startInterval, | ||
stopInterval, | ||
updateDelay, | ||
}; | ||
}; |