diff --git a/fsl/test/src/mocks.fx b/fsl/test/src/mocks.fx index a0c7323..be0a42f 100644 --- a/fsl/test/src/mocks.fx +++ b/fsl/test/src/mocks.fx @@ -21,8 +21,18 @@ export async function renderComponent( component: unknown, props?: Record ): Promise { - // TODO: Mount the component using the Forge runtime in test mode const container = document.createElement('div') + + let element: Element + if (component instanceof Element) { + element = component + } else if (typeof component === 'function') { + element = (component as (props: Record) => Element)(props ?? {}) + } else { + throw new TypeError(`renderComponent: expected a component function or an Element, got ${typeof component}`) + } + container.appendChild(element) + document.body.appendChild(container) return { diff --git a/fsl/test/tests/test.test.fx b/fsl/test/tests/test.test.fx index ffab7b5..b638133 100644 --- a/fsl/test/tests/test.test.fx +++ b/fsl/test/tests/test.test.fx @@ -1,6 +1,6 @@ // forge:test self-tests // These tests verify the test runner's own assertion API. -import { describe, it, expect } from '../src/index.fx' +import { describe, it, expect, renderComponent } from '../src/index.fx' describe('expect().toBe', () => { it('passes for identical primitives', () => { @@ -28,3 +28,31 @@ describe('expect().toThrow', () => { expect(() => { throw new Error('boom') }).toThrow('boom') }) }) + +describe('renderComponent', () => { + it('mounts a component and renders its output', async () => { + // Dummy component function mimicking compiled Forge output + const MyComponent = (props: { title?: string } = {}) => { + const el = document.createElement('div') + el.textContent = props.title ?? 'Hello World' + return el + } + + const { getByText, unmount } = await renderComponent(MyComponent, { title: 'Test Component' }) + + expect(getByText('Test Component')).toBeDefined() + + unmount() + }) + + it('mounts a pre-constructed Element directly', async () => { + const element = document.createElement('p') + element.textContent = 'Pre-built element' + + const { getByText, unmount } = await renderComponent(element) + + expect(getByText('Pre-built element')).toBeDefined() + + unmount() + }) +})