import { act, render } from '@testing-library/react' import { afterEach, describe, expect, it, vi } from 'vitest' import Script from './script' // The shim's load caches are module-level (like Next's), so each test uses // its own id/src. // // jsdom runs scripts against its own window, which vitest's test-side // `window` doesn't share, so inline scripts record runs on the shared // document instead of a window global. const inlineCounter = (key: string) => `var el = document.documentElement; el.dataset['run_${key}'] = String(Number(el.dataset['run_${key}'] || 0) + 1);` const runs = (key: string) => Number(document.documentElement.dataset[`run_${key}`] ?? 0) afterEach(() => { document.body.querySelectorAll('script[data-nscript]').forEach((el) => el.remove()) }) describe('next/script compat shim', () => { it('executes an inline dangerouslySetInnerHTML script and renders nothing in place', () => { const onReady = vi.fn() const { container } = render( ) expect(runs('inlineChildren')).toBe(1) }) it('executes a script with the same id only once across instances and remounts', () => { const body = { __html: inlineCounter('dedupe') } const first = render( <>