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.1 kB · 48 lines
TypeScript
at main
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849import { describe, expect, it, vi } from 'vitest';import { renderHook } from 'vitest-browser-react';import { act } from 'react';
import useTick from './useTick.js';
const itIfWindowDefined = it.runIf(typeof window !== 'undefined');const itIfWindowUndefined = it.runIf(typeof window === 'undefined');
vi.useFakeTimers();
describe('useTick()', () => { itIfWindowDefined('should increment the counter in given intervals', async () => { const { result } = await renderHook(() => useTick(1000));
expect(result.current).toBe(0);
act(() => { vi.advanceTimersByTime(1000); });
expect(result.current).toBe(1);
act(() => { vi.advanceTimersByTime(1000); });
expect(result.current).toBe(2); });
itIfWindowUndefined('should not do anything', async () => { const { result } = await renderHook(() => useTick(1000));
expect(result.current).toBe(0);
act(() => { vi.advanceTimersByTime(1000); });
expect(result.current).toBe(0);
act(() => { vi.advanceTimersByTime(1000); });
expect(result.current).toBe(0); });});