Skip to content

Commit

Permalink
feat : UseDebounce 훅 테스트 스크립트 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee-Dongwook committed Oct 28, 2024
1 parent 15f943f commit 5a6c51d
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/hooks/test/useDebounce.test.ts
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');
});
});

0 comments on commit 5a6c51d

Please sign in to comment.