-
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.
test(react): add useTimeout test case (#268)
fix #265 # Overview @manudeli π ## PR Checklist - [x] I did below actions if need 1. I read the [Contributing Guide](https://github.com/suspensive/react/blob/main/CONTRIBUTING.md) 2. I added documents and tests. --------- Co-authored-by: Jonghyeon Ko <[email protected]>
- Loading branch information
Showing
3 changed files
with
52 additions
and
22 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 | ||
--- | ||
|
||
test(react): add useTimeout test case |
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { MS_100 } from '@suspensive/test-utils' | ||
import { act, render, renderHook, screen } from '@testing-library/react' | ||
import { useState } from 'react' | ||
import { describe, expect, it, vi } from 'vitest' | ||
import { useTimeout } from '.' | ||
|
||
vi.useFakeTimers() | ||
|
||
const TestComponent = () => { | ||
const [number, setNumber] = useState(0) | ||
|
||
useTimeout(() => { | ||
setNumber(number + 1) | ||
}, MS_100) | ||
|
||
return <div>{number}</div> | ||
} | ||
|
||
describe('useTimeout', () => { | ||
it('should run given function once after given timeout', () => { | ||
const fn = vi.fn() | ||
const rendered = renderHook(() => useTimeout(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) | ||
}) | ||
|
||
it('should not re-call callback reveived as argument even if component using this hook is rerendered', () => { | ||
const { rerender } = render(<TestComponent />) | ||
|
||
expect(screen.getByText('0')).toBeInTheDocument() | ||
|
||
act(() => vi.advanceTimersByTime(MS_100)) | ||
expect(screen.getByText('1')).toBeInTheDocument() | ||
|
||
rerender(<TestComponent />) | ||
|
||
act(() => vi.advanceTimersByTime(MS_100)) | ||
expect(screen.getByText('1')).toBeInTheDocument() | ||
}) | ||
}) |
b962122
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:
docs β ./docs
docs-seven-omega.vercel.app
docs-git-main-suspensive.vercel.app
docs-suspensive.vercel.app
suspensive.org
www.suspensive.org
b962122
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