Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion fsl/test/src/mocks.fx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,18 @@ export async function renderComponent(
component: unknown,
props?: Record<string, unknown>
): Promise<RenderResult> {
// 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<string, unknown>) => 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 {
Expand Down
30 changes: 29 additions & 1 deletion fsl/test/tests/test.test.fx
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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()
})
})