-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(react): add useSetTimeout to reduce code (#158)
* refactor(react): add useSetTimeout to reduce code * Create friendly-ants-grab.md
- Loading branch information
Showing
6 changed files
with
42 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@suspensive/react": patch | ||
--- | ||
|
||
refactor(react): add useSetTimeout to reduce code |
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,22 @@ | ||
import { act, renderHook } from '@testing-library/react' | ||
import { describe, expect, it, vi } from 'vitest' | ||
import { MS_100 } from '../utils/toTest' | ||
import { useSetTimeout } from '.' | ||
|
||
vi.useFakeTimers() | ||
|
||
describe('useSetTimeout', () => { | ||
it('should run given function once after given timeout', () => { | ||
const fn = vi.fn() | ||
const rendered = renderHook(() => useSetTimeout(fn, MS_100)) | ||
expect(fn).toHaveBeenCalledTimes(0) | ||
act(() => vi.advanceTimersByTime(MS_100)) | ||
expect(fn).toHaveBeenCalledTimes(1) | ||
act(() => vi.advanceTimersByTime(MS_100)) | ||
expect(fn).toHaveBeenCalledTimes(1) | ||
rendered.rerender() | ||
expect(fn).toHaveBeenCalledTimes(1) | ||
act(() => vi.advanceTimersByTime(MS_100)) | ||
expect(fn).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,7 @@ | ||
import { useEffect } from 'react' | ||
|
||
export const useSetTimeout = (fn: (...args: []) => void, delay: number) => | ||
useEffect(() => { | ||
const timeout = setTimeout(fn, delay) | ||
return () => clearTimeout(timeout) | ||
}, [fn, delay]) |
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
2c30f6d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
visualization – ./websites/visualization
visualization.suspensive.org
visualization-git-main-suspensive.vercel.app
visualization-suspensive.vercel.app