-
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
7 changed files
with
112 additions
and
4 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
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,53 @@ | ||
import { useTimeout } from '@/hooks/state' | ||
import { renderHook } from '@testing-library/react' | ||
|
||
vi.useFakeTimers() | ||
|
||
describe('useTimeout', () => { | ||
it('should call callback after the delay', () => { | ||
const callback = vi.fn() | ||
renderHook(() => useTimeout(callback, 1000)) | ||
|
||
vi.advanceTimersByTime(1000) | ||
expect(callback).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('should clear timeout on unmount', () => { | ||
const callback = vi.fn() | ||
const { unmount } = renderHook(() => useTimeout(callback, 1000)) | ||
|
||
unmount() | ||
vi.advanceTimersByTime(1000) | ||
expect(callback).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('should reset timeout if delay changes', () => { | ||
const callback = vi.fn() | ||
const { rerender } = renderHook(({ delay }) => useTimeout(callback, delay), { | ||
initialProps: { delay: 1000 }, | ||
}) | ||
|
||
vi.advanceTimersByTime(500) | ||
rerender({ delay: 2000 }) // Change delay | ||
|
||
vi.advanceTimersByTime(1500) // Total elapsed 2000ms (500ms + 1500ms) | ||
expect(callback).not.toHaveBeenCalled() // Callback should not run because timeout reset | ||
|
||
vi.advanceTimersByTime(500) // 2000ms reached | ||
expect(callback).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('should run callback immediately if delay is 0', () => { | ||
const callback = vi.fn() | ||
renderHook(() => useTimeout(callback, 0)) | ||
|
||
expect(callback).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('should run callback only once if delay is negative', () => { | ||
const callback = vi.fn() | ||
renderHook(() => useTimeout(callback, -1000)) | ||
|
||
expect(callback).toHaveBeenCalledTimes(1) | ||
}) | ||
}) |
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,48 @@ | ||
'use client' | ||
|
||
import { useEffect, useRef } from 'react' | ||
|
||
/** | ||
* Hook that runs a function after a specified delay. | ||
* The timeout resets if the dependencies change. | ||
* | ||
* @param {(() => void) | void} callback - The function to execute after the timeout. Can be a function or a direct callable reference. | ||
* @param {number} [delay] - The delay in milliseconds. Defaults to 0. | ||
* | ||
* @example | ||
* ```tsx | ||
* const MyComponent = () => { | ||
* const [isVisible, setIsVisible] = useState(true) | ||
* | ||
* useTimeout(() => setIsVisible(false), 1000) | ||
* // OR | ||
* useTimeout(setIsVisible.bind(null, false), 1000) | ||
* | ||
* return <div>{isVisible ? 'Visible' : 'Hidden'}</div> | ||
* } | ||
* ``` | ||
*/ | ||
export const useTimeout = (callback: (() => void) | void, delay: number = 0): void => { | ||
const timeoutRef = useRef<NodeJS.Timeout | null>(null) | ||
|
||
useEffect(() => { | ||
if (delay <= 0) { | ||
if (callback instanceof Function) { | ||
callback() | ||
} | ||
return | ||
} | ||
|
||
timeoutRef.current = setTimeout(() => { | ||
if (callback instanceof Function) { | ||
callback() | ||
} | ||
}, delay) | ||
|
||
return () => { | ||
if (timeoutRef.current) { | ||
clearTimeout(timeoutRef.current) | ||
} | ||
} | ||
}, [callback, delay]) | ||
} |