import { describe, expect, it, vi } from 'vitest'; import { renderHook } from 'vitest-browser-react'; import { act } from 'react'; import useSetInterval from './useSetInterval.js'; const itIfWindowDefined = it.runIf(typeof window !== 'undefined'); const itIfWindowUndefined = it.runIf(typeof window === 'undefined'); vi.useFakeTimers(); describe('useSetInterval()', () => { itIfWindowDefined('should run given function in given intervals', async () => { const fn = vi.fn(); await renderHook(() => useSetInterval(fn, 1000)); expect(fn).not.toHaveBeenCalled(); act(() => { vi.advanceTimersByTime(1000); }); expect(fn).toHaveBeenCalledTimes(1); act(() => { vi.advanceTimersByTime(1000); }); expect(fn).toHaveBeenCalledTimes(2); }); itIfWindowUndefined('should not do anything', async () => { const fn = vi.fn(); await renderHook(() => useSetInterval(fn, 1000)); expect(fn).not.toHaveBeenCalled(); act(() => { vi.advanceTimersByTime(1000); }); expect(fn).not.toHaveBeenCalled(); act(() => { vi.advanceTimersByTime(1000); }); expect(fn).not.toHaveBeenCalled(); }); });