forked from andychase/gbajs2
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: remove additional hooks library (#252)
- remove usehooks-ts - partial prep for move to react 19
- Loading branch information
1 parent
daadf78
commit 81127d3
Showing
7 changed files
with
156 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,133 @@ | ||
import { renderHook, act } from '@testing-library/react'; | ||
import { vi, describe, it, expect, beforeEach } from 'vitest'; | ||
|
||
import { useInterval } from './use-interval.ts'; | ||
|
||
describe('useInterval', () => { | ||
beforeEach(() => { | ||
vi.useFakeTimers(); | ||
}); | ||
|
||
it('should call the callback at the specified interval', () => { | ||
const callback = vi.fn(); | ||
|
||
renderHook(() => useInterval(callback, 1000)); | ||
|
||
act(() => { | ||
vi.advanceTimersByTime(3000); | ||
}); | ||
|
||
expect(callback).toHaveBeenCalledTimes(3); | ||
}); | ||
|
||
it('should not call the callback if delay is null', () => { | ||
const callback = vi.fn(); | ||
|
||
renderHook(() => useInterval(callback, null)); | ||
|
||
act(() => { | ||
vi.advanceTimersByTime(1000); | ||
}); | ||
|
||
expect(callback).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not call the callback after unmounting', () => { | ||
const callback = vi.fn(); | ||
|
||
const { unmount } = renderHook(() => useInterval(callback, 1000)); | ||
|
||
act(() => { | ||
vi.advanceTimersByTime(1000); | ||
}); | ||
|
||
expect(callback).toHaveBeenCalledTimes(1); | ||
|
||
unmount(); | ||
|
||
act(() => { | ||
vi.advanceTimersByTime(1000); | ||
}); | ||
|
||
expect(callback).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should update the callback without resetting the interval', () => { | ||
const initialCallback = vi.fn(); | ||
const updatedCallback = vi.fn(); | ||
|
||
const { rerender } = renderHook( | ||
({ callback }) => useInterval(callback, 1000), | ||
{ | ||
initialProps: { callback: initialCallback } | ||
} | ||
); | ||
|
||
act(() => { | ||
vi.advanceTimersByTime(1000); | ||
}); | ||
|
||
expect(initialCallback).toHaveBeenCalledTimes(1); | ||
expect(updatedCallback).not.toHaveBeenCalled(); | ||
|
||
rerender({ callback: updatedCallback }); | ||
|
||
act(() => { | ||
vi.advanceTimersByTime(1000); | ||
}); | ||
|
||
expect(initialCallback).toHaveBeenCalledTimes(1); | ||
expect(updatedCallback).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should update the interval when delay changes', () => { | ||
const callback = vi.fn(); | ||
|
||
const { rerender } = renderHook( | ||
({ delay }) => useInterval(callback, delay), | ||
{ | ||
initialProps: { delay: 1000 } | ||
} | ||
); | ||
|
||
act(() => { | ||
vi.advanceTimersByTime(1000); | ||
}); | ||
|
||
expect(callback).toHaveBeenCalledTimes(1); | ||
|
||
rerender({ delay: 200 }); | ||
|
||
act(() => { | ||
vi.advanceTimersByTime(500); | ||
}); | ||
|
||
expect(callback).toHaveBeenCalledTimes(3); | ||
}); | ||
|
||
it('should clear the interval when delay becomes null', () => { | ||
const callback = vi.fn(); | ||
const initialProps: { delay: number | null } = { delay: 1000 }; | ||
|
||
const { rerender } = renderHook( | ||
({ delay }: { delay: number | null }) => useInterval(callback, delay), | ||
{ | ||
initialProps: initialProps | ||
} | ||
); | ||
|
||
act(() => { | ||
vi.advanceTimersByTime(1000); | ||
}); | ||
|
||
expect(callback).toHaveBeenCalledTimes(1); | ||
|
||
rerender({ delay: null }); | ||
|
||
act(() => { | ||
vi.advanceTimersByTime(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { useEffect, useLayoutEffect, useRef } from 'react'; | ||
|
||
export const useInterval = (callback: () => void, delay: number | null) => { | ||
const savedCallback = useRef(callback); | ||
|
||
useLayoutEffect(() => { | ||
savedCallback.current = callback; | ||
}, [callback]); | ||
|
||
useEffect(() => { | ||
if (delay === null) return; | ||
|
||
const id = setInterval(() => savedCallback.current(), delay); | ||
|
||
return () => clearInterval(id); | ||
}, [delay]); | ||
}; |