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.3 kB · 50 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051import { afterEach, describe, expect, it, vi } from 'vitest';import { renderHook } from 'vitest-browser-react';
import useMutationObserver from './useMutationObserver.js';
const itIfWindowDefined = it.runIf(typeof window !== 'undefined');
async function waitForAsync() { await new Promise((resolve) => { setTimeout(resolve, 0); });}
describe('useMutationObserver()', () => { const config = { childList: true, subtree: true, };
afterEach(() => { vi.clearAllMocks(); });
it('does nothing given falsy element', async () => { const listener = () => { // Intentionally empty };
const { result } = await renderHook(() => useMutationObserver(null, config, listener));
expect(result.current).toBe(undefined); });
itIfWindowDefined('observes the given element', async () => { const element = document.createElement('div'); const listener = vi.fn();
await renderHook(() => useMutationObserver(element, config, listener));
element.appendChild(document.createElement('span'));
await waitForAsync();
expect(listener).toHaveBeenCalledTimes(1); expect(listener).toHaveBeenCalledWith( [expect.any(MutationRecord)], expect.any(MutationObserver), ); });});