import { describe, it, expect, beforeEach, afterEach } from 'vitest' import { createRoot } from '../lib/vdom.ts' import { renderToString } from '../lib/stream.ts' import { invariant } from '../lib/invariant.ts' describe('hydration', () => { let container: HTMLDivElement beforeEach(() => { container = document.createElement('div') document.body.appendChild(container) }) afterEach(() => { document.body.innerHTML = '' }) describe('form elements', () => { it('hydrates input with value attribute', async () => { let html = await renderToString() container.innerHTML = html let existingInput = container.querySelector('input') invariant(existingInput) let root = createRoot(container) root.render() root.flush() expect(container.querySelector('input')).toBe(existingInput) expect(existingInput.value).toBe('server value') }) it('hydrates input with defaultValue', async () => { let html = await renderToString() container.innerHTML = html let existingInput = container.querySelector('input') invariant(existingInput) let root = createRoot(container) root.render() root.flush() expect(container.querySelector('input')).toBe(existingInput) expect(existingInput.value).toBe('default') }) it('hydrates checkbox with checked attribute', async () => { let html = await renderToString() container.innerHTML = html let existingInput = container.querySelector('input') invariant(existingInput) let root = createRoot(container) root.render() root.flush() expect(container.querySelector('input')).toBe(existingInput) expect(existingInput.checked).toBe(true) }) it('hydrates checkbox with defaultChecked', async () => { let html = await renderToString() container.innerHTML = html let existingInput = container.querySelector('input') invariant(existingInput) let root = createRoot(container) root.render() root.flush() expect(container.querySelector('input')).toBe(existingInput) expect(existingInput.checked).toBe(true) }) it('hydrates radio with checked attribute', async () => { let html = await renderToString(
, ) container.innerHTML = html let inputs = container.querySelectorAll('input') expect(inputs[0].checked).toBe(true) expect(inputs[1].checked).toBe(false) let root = createRoot(container) root.render(
, ) root.flush() let hydratedInputs = container.querySelectorAll('input') expect(hydratedInputs[0]).toBe(inputs[0]) expect(hydratedInputs[1]).toBe(inputs[1]) expect(hydratedInputs[0].checked).toBe(true) expect(hydratedInputs[1].checked).toBe(false) }) it('hydrates textarea with value', async () => { let html = await renderToString(