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.
2.1 kB · 83 lines
TypeScript
at main
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';import { renderHook } from 'vitest-browser-react';
import useIntersectionObserver from './useIntersectionObserver.js';
import type { Mock } from 'vitest';
const itIfWindowDefined = it.runIf(typeof window !== 'undefined');
async function waitForAsync() { await new Promise((resolve) => { setTimeout(resolve, 0); });}
describe('useIntersectionObserver()', () => { const config = {};
let observe: Mock; let disconnect: Mock;
beforeEach(() => { if (typeof window !== 'undefined') { observe = vi.fn(); disconnect = vi.fn();
function MockIntersectionObserver() { return { observe, unobserve: () => { //Intentionally empty }, disconnect, root: null, rootMargin: '', thresholds: [], takeRecords: () => { return []; }, }; }
Object.defineProperty(window, 'IntersectionObserver', { configurable: true, enumerable: true, get: () => MockIntersectionObserver, });
vi.spyOn(window, 'IntersectionObserver'); } });
afterEach(() => { vi.clearAllMocks(); });
it('does nothing given falsy element', async () => { const listener = () => { // Intentionally empty };
const { result } = await renderHook(() => useIntersectionObserver(null, config, listener));
expect(result.current).toBe(undefined); });
itIfWindowDefined('observes the given element', async () => { const element = document.createElement('div'); const listener = () => { // Intentionally empty };
await renderHook(() => useIntersectionObserver(element, config, listener));
await waitForAsync();
expect(window.IntersectionObserver).toHaveBeenCalledTimes(1); expect(window.IntersectionObserver).toHaveBeenCalledWith(listener, config);
expect(observe).toHaveBeenCalledTimes(1); expect(observe).toHaveBeenCalledWith(element); });});