import * as assert from '@remix-run/assert' import { describe, it } from '@remix-run/test' import { jsx, unsafeHTML, type RemixNode } from './vnode.ts' import { Frame } from './frame.ts' import type { PortalProps } from './component.ts' import { Portal } from './portal.ts' import { toVNode } from './vdom.ts' import type { CommittedComponentNode, CommittedFrameNode, CommittedHostNode, CommittedVNode, ComponentNode, FrameNode, HostNode, VNodeInput, VNodeParent, } from './vdom.ts' type Equal = (() => value extends left ? 1 : 2) extends () => value extends right ? 1 : 2 ? true : false type HasKey = key extends keyof value ? true : false function expectType(): void {} describe('VNode lifecycle types', () => { it('keeps node-kind resources on their owning variants', () => { expectType< Equal< VNodeInput['kind'], 'empty' | 'text' | 'fragment' | 'host' | 'component' | 'frame' | 'portal' > >() expectType>() expectType>() expectType>() expectType, false>>() expectType, false>>() expectType, false>>() expectType, false>>() expectType, false>>() expectType, false>>() expectType< Equal >() expectType>() expectType>() expectType>() expectType>() }) it('converts elements into complete unmounted variants', () => { let vnode = toVNode( jsx('section', { children: ['hello', jsx('strong', { children: 'world' })], innerHTML: undefined, }), ) assert.equal(vnode.kind, 'host') if (vnode.kind !== 'host') return assert.equal(vnode.type, 'section') assert.equal(vnode._children.length, 2) assert.equal(vnode._children[0].kind, 'text') assert.equal(vnode._children[1].kind, 'host') assert.equal('_dom' in vnode, false) }) it('validates framework props at the element boundary', () => { assert.throws( () => toVNode(jsx('div', { innerHTML: 42 })), /Invalid innerHTML prop: expected a value created with unsafeHTML\(\)/, ) assert.throws(() => toVNode(jsx('div', { mix: {} })), /Invalid mix prop/) }) it('validates nested host children before a render starts', () => { assert.throws(() => toVNode([{}]), /Unexpected RemixNode/) assert.throws( () => toVNode(jsx('div', { children: [jsx('p', {}), [{}]] })), /Invalid host children/, ) }) it('validates children even when raw HTML or a Portal defers their rendering', () => { assert.throws( () => toVNode(jsx('div', { innerHTML: unsafeHTML(''), children: {} })), /Invalid host children/, ) assert.throws(() => toVNode(jsx(Portal, { children: {} })), /Invalid host children/) }) it('rejects a null Portal destination instead of using the body', () => { assert.throws( () => toVNode(jsx(Portal, { container: null })), /Portal container must be an HTMLElement/, ) }) it('converts built-in Frame elements into frame variants', () => { let vnode = toVNode(jsx(Frame, { src: '/sidebar', name: 'sidebar' }, 'frame-key')) assert.equal(vnode.kind, 'frame') if (vnode.kind !== 'frame') return assert.equal(vnode.type, Frame) assert.equal(vnode.key, 'frame-key') assert.equal(vnode.props.src, '/sidebar') assert.equal(vnode.props.name, 'sidebar') }) it('treats other components named Frame as regular components', () => { function Frame() { return () => null } let vnode = toVNode(jsx(Frame, { src: '/sidebar' })) assert.equal(vnode.kind, 'component') assert.equal(vnode.type, Frame) }) it('treats components that inherit from Frame as regular components', () => { function DerivedFrame() { return () => null } Object.setPrototypeOf(DerivedFrame, Frame) let vnode = toVNode(jsx(DerivedFrame, { src: '/sidebar' })) assert.equal(vnode.kind, 'component') assert.equal(vnode.type, DerivedFrame) }) it('requires a src prop on built-in Frame elements', () => { assert.throws(() => toVNode(jsx(Frame, {})), / requires a src prop/) assert.throws(() => toVNode(jsx(Frame, { src: '' })), / requires a src prop/) }) })