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.2 kB · 52 lines
TypeScript
at main
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253import { describe, expect, it, vi } from 'vitest';import { renderHook } from 'vitest-browser-react';import { act } from 'react';
import useSetTimeout from './useSetTimeout.js';
const itIfWindowDefined = it.runIf(typeof window !== 'undefined');const itIfWindowUndefined = it.runIf(typeof window === 'undefined');
vi.useFakeTimers();
describe('useSetTimeout()', () => { itIfWindowDefined('should run given function once after given timeout', async () => { const fn = vi.fn();
await renderHook(() => useSetTimeout(fn, 1000));
expect(fn).not.toHaveBeenCalled();
act(() => { vi.advanceTimersByTime(1000); });
expect(fn).toHaveBeenCalledTimes(1);
act(() => { vi.advanceTimersByTime(1000); });
expect(fn).toHaveBeenCalledTimes(1); });
itIfWindowUndefined('should not do anything', async () => { const fn = vi.fn();
await renderHook(() => useSetTimeout(fn, 1000));
expect(fn).not.toHaveBeenCalled();
act(() => { vi.advanceTimersByTime(1000); });
expect(fn).not.toHaveBeenCalled();
act(() => { vi.advanceTimersByTime(1000); });
expect(fn).not.toHaveBeenCalled(); });});