-
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.
refactor: use debounce 테스트 코드 작성 및 리팩토링 (#ATR-600)
- Loading branch information
Showing
2 changed files
with
59 additions
and
4 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
packages/design-system/packages/hooks/src/use-debounce/useDebounce.spec.ts
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,55 @@ | ||
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest' | ||
import { act, renderHook } from '@testing-library/react' | ||
import useDebounce from './useDebounce' | ||
|
||
describe('@/use-debounce', () => { | ||
const delay = 500 | ||
const initialValue = 'initial value' | ||
const changedValue = 'changed value' | ||
|
||
beforeEach(() => { | ||
vi.useFakeTimers() | ||
}) | ||
|
||
afterEach(() => { | ||
vi.useRealTimers() | ||
}) | ||
|
||
test('지연된 후에 값이 올바르게 변경됨', () => { | ||
const { result, rerender } = renderHook( | ||
({ value }) => useDebounce(value, delay), | ||
{ initialProps: { value: initialValue } }, | ||
) | ||
|
||
expect(result.current).toBe(initialValue) | ||
|
||
rerender({ value: changedValue }) | ||
|
||
expect(result.current).toBe(initialValue) | ||
|
||
act(() => vi.advanceTimersByTime(delay * 2)) | ||
|
||
expect(result.current).toBe(changedValue) | ||
}) | ||
|
||
test('unmount 됐을 때 실행 중이던 debounce가 취소됨', () => { | ||
const { result, rerender, unmount } = renderHook( | ||
({ value }) => useDebounce(value, delay), | ||
{ initialProps: { value: initialValue } }, | ||
) | ||
|
||
expect(result.current).toBe(initialValue) | ||
|
||
rerender({ value: changedValue }) | ||
|
||
expect(result.current).toBe(initialValue) | ||
|
||
act(() => vi.advanceTimersByTime(delay / 2)) | ||
|
||
unmount() | ||
|
||
act(() => vi.advanceTimersByTime(delay)) | ||
|
||
expect(result.current).toBe(initialValue) | ||
}) | ||
}) |
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