import type { ServerUnhead } from '../src/server' import type { CreateClientHeadOptions, CreateServerHeadOptions, ResolvableHead, SSRHeadPayload } from '../src/types' import { JSDOM } from 'jsdom' import { createHead as createClientHead } from '../src/client' import { createHead as createServerHead } from '../src/server' import { createStreamableHead as createServerStreamableHead } from '../src/stream/server' export function createClientHeadWithContext(resolvedOptions: CreateClientHeadOptions = {}) { return createClientHead(resolvedOptions) } export function createServerHeadWithContext(resolvedOptions: CreateServerHeadOptions = {}): ServerUnhead { return createServerHead({ disableDefaults: true, ...resolvedOptions, }) } export function createStreamableServerHead(options: { streamKey?: string, writesBodyTags?: boolean } = {}) { const { head } = createServerStreamableHead({ disableDefaults: true, ...options, }) return head } // eslint-disable-next-line import/no-mutable-exports export let activeDom: JSDOM | null = null export function useDom(payload?: Partial, extra?: Partial) { if (typeof window !== 'undefined') { // reset window const window = new JSDOM().window // just apply the below to the current document const attrsString = `${extra?.htmlAttrs || ''}${payload?.htmlAttrs || ''}` // split into key => value const attrsObj = attrsString.split(' ').reduce((acc, attr) => { const [key, value] = attr.split('=') acc[key] = value return acc }, {} as Record) Object.entries(attrsObj).forEach(([key, value]) => { if (key.length) { window.document.documentElement.setAttribute(key, String(value).replaceAll('"', '')) } }) window.document.documentElement.innerHTML = ` ${payload?.headTags || ''} ${payload?.bodyTagsOpen || ''}

hello world

${payload?.bodyTags || ''} ` return { window, serialize: () => window.document.documentElement.outerHTML, } as any as JSDOM } return new JSDOM( ` ${payload?.headTags || ''} ${payload?.bodyTagsOpen || ''}

hello world

${payload?.bodyTags || ''} `, ) } export const basicSchema = { htmlAttrs: { lang: 'en', dir: 'ltr' as const, }, bodyAttrs: { class: 'dark', }, script: [ { src: 'https://cdn.example.com/script.js', }, ], meta: [ { charset: 'utf-8' as const, }, ], link: [ { rel: 'icon' as const, type: 'image/x-icon', href: 'https://cdn.example.com/favicon.ico', }, ], } satisfies ResolvableHead export function useDOMHead(options: CreateClientHeadOptions = {}) { // If no document is provided, create a fresh DOM for this test if (!options.document) { activeDom = useDom() options.document = activeDom.window.document } return createClientHeadWithContext(options) } export function useDelayedSerializedDom() { return new Promise((resolve) => { setTimeout(() => resolve(activeDom!.serialize()), 250) }) } // Helper to get the current active DOM for tests that need it export function getActiveDom() { return activeDom } // Helper to reset the active DOM between tests export function resetActiveDom() { activeDom = null }