-
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
1 parent
15f943f
commit 5a6c51d
Showing
1 changed file
with
79 additions
and
0 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,79 @@ | ||
import { renderHook, act } from '@testing-library/react'; | ||
import { useDebounce } from '@/hooks/useDebounce'; | ||
|
||
jest.useFakeTimers(); | ||
|
||
describe('useDebounce 테스트', () => { | ||
it('지연 시간 후에 값이 업데이트 되어야 한다.', () => { | ||
const initialDelay = 500; | ||
const { result, rerender } = renderHook( | ||
({ value, delay }) => useDebounce(value, delay), | ||
{ | ||
initialProps: { value: 'initial', delay: initialDelay }, | ||
} | ||
); | ||
expect(result.current).toBe('initial'); | ||
|
||
rerender({ value: 'updated', delay: initialDelay }); | ||
|
||
act(() => { | ||
jest.advanceTimersByTime(initialDelay); | ||
}); | ||
|
||
expect(result.current).toBe('updated'); | ||
}); | ||
|
||
it('지연 시간 내에 여러 값이 변경되어도 마지막 값만 반영되어야 한다.', () => { | ||
const initialDelay = 500; | ||
const { result, rerender } = renderHook( | ||
({ value, delay }) => useDebounce(value, delay), | ||
{ | ||
initialProps: { value: 'initial', delay: initialDelay }, | ||
} | ||
); | ||
|
||
rerender({ value: 'first change', delay: initialDelay }); | ||
act(() => { | ||
jest.advanceTimersByTime(100); | ||
}); | ||
|
||
rerender({ value: 'second change', delay: initialDelay }); | ||
act(() => { | ||
jest.advanceTimersByTime(200); | ||
}); | ||
|
||
expect(result.current).toBe('initial'); | ||
|
||
act(() => { | ||
jest.advanceTimersByTime(200); | ||
}); | ||
|
||
expect(result.current).toBe('second change'); | ||
}); | ||
|
||
it('지연 시간이 변경되면 타이머가 초기화되어야 한다.', () => { | ||
const initialDelay = 300; | ||
const updatedDelay = 500; | ||
const { result, rerender } = renderHook( | ||
({ value, delay }) => useDebounce(value, delay), | ||
{ | ||
initialProps: { value: 'initial', delay: initialDelay }, | ||
} | ||
); | ||
|
||
rerender({ value: 'first change', delay: initialDelay }); | ||
act(() => { | ||
jest.advanceTimersByTime(150); | ||
}); | ||
|
||
rerender({ value: 'first change', delay: updatedDelay }); | ||
|
||
expect(result.current).toBe('initial'); | ||
|
||
act(() => { | ||
jest.advanceTimersByTime(500); | ||
}); | ||
|
||
expect(result.current).toBe('first change'); | ||
}); | ||
}); |