import { act, render, screen } from '@testing-library/react' import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest' import { axe } from 'vitest-axe' import { Toc, TocList, TocItem, TocLink } from './toc' class MockIntersectionObserver { static instances: MockIntersectionObserver[] = [] callback: IntersectionObserverCallback elements = new Set() root = null rootMargin = '' thresholds = [] constructor(callback: IntersectionObserverCallback) { this.callback = callback MockIntersectionObserver.instances.push(this) } observe = (element: Element) => { this.elements.add(element) } unobserve = (element: Element) => { this.elements.delete(element) } disconnect = () => { this.elements.clear() } takeRecords = () => [] trigger(entries: Partial[]) { act(() => { this.callback(entries as IntersectionObserverEntry[], this as unknown as IntersectionObserver) }) } } class MockResizeObserver { observe() {} unobserve() {} disconnect() {} } beforeEach(() => { MockIntersectionObserver.instances = [] vi.stubGlobal('IntersectionObserver', MockIntersectionObserver) vi.stubGlobal('ResizeObserver', MockResizeObserver) }) afterEach(() => { vi.unstubAllGlobals() }) function lastObserver() { return MockIntersectionObserver.instances.at(-1)! } function entry(id: string, isIntersecting: boolean, rootBounds: Partial | null = null) { return { target: document.getElementById(id)!, isIntersecting, rootBounds: rootBounds as DOMRectReadOnly | null, } } function renderToc(props?: React.ComponentProps) { return render( <>

Intro

Usage

Props

Intro Usage Props , ) } describe('Toc', () => { it('renders a