Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/wojtekmaj/react-hooks. A collection of React Hooks.
hooks react react-hooks
Something went wrong. Try again.
1.6 kB · 63 lines
TypeScript
at main
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364import { describe, expect, it, vi } from 'vitest';import { renderHook } from 'vitest-browser-react';import { act } from 'react';
import useDebouncedValue from './useDebouncedValue.js';
const itIfWindowDefined = it.runIf(typeof window !== 'undefined');
vi.useFakeTimers();
describe('useDebouncedValue()', () => { it('should return the initial value immediately', async () => { const { result } = await renderHook(() => useDebouncedValue('test', 500));
expect(result.current).toBe('test'); });
itIfWindowDefined('should not update the value before the debounce time', async () => { const { result, rerender } = await renderHook( (props) => { if (!props) { throw new Error('Props are required'); }
const { value, time } = props;
return useDebouncedValue(value, time); }, { initialProps: { value: 'test', time: 500 }, }, );
await rerender({ value: 'updated', time: 500 });
expect(result.current).toBe('test'); });
itIfWindowDefined('should update the value after the debounce time', async () => { const { result, rerender } = await renderHook( (props) => { if (!props) { throw new Error('Props are required'); }
const { value, time } = props;
return useDebouncedValue(value, time); }, { initialProps: { value: 'test', time: 500 }, }, );
await rerender({ value: 'updated', time: 500 });
act(() => { vi.advanceTimersByTime(500); });
expect(result.current).toBe('updated'); });});