gridland
API Reference

@gridland/testing

Testing utilities API reference

@gridland/testing mounts Gridland components into an in-memory buffer for unit tests. It exposes one entry point — renderTui — plus the Screen, KeySender, and waitFor primitives. See the Testing guide for end-to-end examples.

renderTui

import { renderTui } from "@gridland/testing"

const tui = renderTui(node, options?)

Mounts a React tree into an in-memory buffer and returns a TuiInstance. The call is synchronous — the initial render pass completes before renderTui returns.

Parameters:

ParameterTypeDescription
nodeReactNodeThe Gridland tree to render
optionsRenderTuiOptions?Buffer dimensions

RenderTuiOptions:

OptionTypeDefaultDescription
colsnumber80Number of columns
rowsnumber24Number of rows

Returns: TuiInstance

TuiInstance

PropertyTypeDescription
screenScreenQuery helpers for reading buffer content
keysKeySenderKeyboard input simulation
waitFor(condition, options?) => Promise<void>Poll until condition holds (bound to this instance's screen)
flush() => voidForce a synchronous render pass and capture a new frame
rerender(node: ReactNode) => voidReplace the rendered tree with a new one
unmount() => voidUnmount the tree and release the renderer

Screen

The Screen class provides read-only access to the cell buffer.

MethodDescription
text()Full screen text with trailing spaces trimmed per line
rawText()Full screen text preserving all spaces
contains(text: string)true if the trimmed text includes text
matches(pattern: RegExp)true if the trimmed text matches pattern
line(n: number)The nth line (0-indexed) or "" if out of range
lines()All non-empty lines as an array
frames()Captured frame snapshots — one per render pass
captureFrame()Manually push the current frame onto the snapshot list
attributeAt(row, col)Raw u32 text attributes at a cell
fgAt(row, col)Foreground RGBA tuple [r, g, b, a] at a cell
widthBuffer width in cells
heightBuffer height in cells

KeySender

Dispatches synthetic keypress events through the focus system.

MethodDescription
type(text: string)Type a string character by character
press(char: string)Press a single character key
raw(data: string)Send a raw sequence (e.g. an escape code)
enter()Press Enter
escape()Press Escape
tab()Press Tab
backspace()Press Backspace
delete()Press Delete
space()Press Space
up() / down() / left() / right()Arrow keys
home() / end()Home / End
pageUp() / pageDown()Page Up / Page Down

All key helpers are methods, not properties — call them with ().

waitFor

Standalone version of tui.waitFor. Polls a condition until it holds or times out.

import { renderTui, waitFor } from "@gridland/testing"

const tui = renderTui(<AsyncComponent />)

// Wait for a literal string on the screen
await waitFor(tui.screen, "Loaded")

// Or wait for an assertion to stop throwing
await waitFor(tui.screen, () => {
  expect(tui.screen.contains("Ready")).toBe(true)
})

Parameters:

ParameterTypeDescription
screenScreenThe screen to poll
conditionstring | (() => void)Literal substring, or an assertion that throws until satisfied
optionsWaitForOptions?Timeout and poll interval

WaitForOptions:

OptionTypeDefaultDescription
timeoutnumber3000Max wait time in ms
intervalnumber50Poll interval in ms

On timeout, waitFor throws an error that includes the current screen contents.

cleanup

import { cleanup } from "@gridland/testing"
import { afterEach } from "bun:test"

afterEach(() => {
  cleanup()
})

Unmounts every active TuiInstance created by renderTui. Wire this into afterEach once and you can skip calling tui.unmount() in each test.