Emulator & testing
Reference for the emulator's public and testing subpaths, used to run widgets without a real Grist document.
The emulator is a self-contained reimplementation of the parts of the Grist plugin API the SDK uses, for tests. It's available under two subpaths:
| Subpath | Purpose |
|---|---|
grist-widget-sdk/emulator | Core emulator (createGristEmulator, document validators). |
grist-widget-sdk/emulator/testing | React Testing Library integration (renderWithGrist, presets, matchers). |
createGristEmulator(options)
Builds an emulator from a document and installs it as grist on the page
(or on transport.target). renderWithGrist does this for you; call it
yourself to share one emulator between renders.
import { createGristEmulator, presets } from "grist-widget-sdk/emulator"
const emulator = createGristEmulator({
document: presets.todoList(), // required, a replica document
widgetOptions: { color: "red" }, // what grist.getOptions() returns
widgetInteraction: { access_level: "read table" }, // the access Grist grants; default: what the widget asks for
initialTheme: "dark", // replayed once on grist.ready()
})
emulator.dispose()document must be a complete GristReplicaDocument (see
Replica document). Validate untrusted
input first with parseDocument(json) (throws) or safeParseDocument(json).
An action on a row that doesn't exist, or one the emulator doesn't support,
throws an EmulatorActionError carrying the action. An unknown table or
malformed arguments throw a plain Error.
import { createGristEmulator, EmulatorActionError, parseDocument, presets, safeParseDocument } from "grist-widget-sdk/emulator"
const checked = safeParseDocument(JSON.parse("{}"))
if (!checked.success) console.log(checked.error.issues.length, "problems")
const document = parseDocument(JSON.parse(JSON.stringify(presets.todoList()))) // throws on a bad one
const emulator = createGristEmulator({ document })
try {
emulator.apply([["UpdateRecord", "Tasks", 99, { done: true }]])
} catch (err) {
if (err instanceof EmulatorActionError) console.log(err.message, err.action)
else throw err
}
emulator.dispose()Driving it
Every change goes through the same event streams Grist uses (onRecord,
onRecords, onOptions, theme messages): the SDK takes its real path.
emulator.mutate.addRow("Tasks", { title: "New" }) // returns the row id
emulator.mutate.updateRow("Tasks", 1, { done: true })
emulator.mutate.removeRow("Tasks", 1)
emulator.apply([["AddRecord", "Tasks", null, { title: "x" }]]) // any user actions
emulator.selectRow(2) // or "new" (the add-row), or null
emulator.selectTable("Contacts") // the section's table
emulator.setColumnMappings({ Label: "title" }) // as a user mapping columns (see below)
await emulator.setOptions({ color: "blue" }) // widget options
emulator.setInteraction({ access_level: "read table" }) // access Grist grants
emulator.theme.set("light")Once the widget has declared its columns, the mappings it receives are
Grist's: every declared column, null (or []) when unmapped, and a
column the table doesn't have, or whose base type the declaration doesn't
accept, left out, as in Grist's column panel.
emulator.state is the live document (tables, selection, options), and
emulator.bus the event bus every change goes through.
Inspecting events and actions
import { actionsOf, eventsOf } from "grist-widget-sdk/emulator/testing"
actionsOf(emulator) // every user action applied so far, e.g. ["AddRecord", "Tasks", null, {...}]
eventsOf(emulator) // every bus event: cursor-change, data-change, options-change, ...Testing entry
import {
renderWithGrist,
presets,
actionsOf,
eventsOf,
waitForEvent,
} from "grist-widget-sdk/emulator/testing"renderWithGrist(ui, options?)
A thin wrapper over @testing-library/react's render. Adds:
type RenderWithGristOptions = RenderOptions & {
/** The widget's declaration: rendered as mountGristWidget() does, every declared column mapped. */
widget?: GristWidget
/** With widget: the rows, under the declared names, as typed reads give them. The first is selected. */
rows?: ReadonlyArray<Record<string, unknown>>
/** The document open read-only, as for a viewer: writes fail as in Grist. */
readOnly?: boolean
emulator?: CreateGristEmulatorOptions
existing?: GristEmulator
}A widget's tests pass widget and rows: renderWithGrist(<App />, { widget, rows })
(Testing). If neither emulator nor existing is
given, a blank emulator is created and torn down on unmount.
Returns a RenderResult with the emulator:
type RenderWithGristResult = RenderResult & {
emulator: GristEmulator
}presets
presets.blank()
presets.todoList()
presets.contacts()Returns ready-to-pass GristReplicaDocument objects. Useful for quick starts; build your own for thorough tests.
waitForEvent(emulator, kind, opts?)
Waits for the next matching bus event (ignores events that were already in history when the call started).
await waitForEvent(emulator, "options", { timeoutMs: 2000 })
await waitForEvent(emulator, (e) => e.type === "user-action" && e.action[0] === "AddRecord")Kinds (map to emulator bus events):
| Kind | Bus event |
|---|---|
ready | ready |
record | cursor-change (drives onRecord) |
records | data-change (drives onRecords) |
options | options-change |
theme | theme-change |
cursor | cursor-change |
Rejects with waitForEvent timed out after …ms (kind: …) and a short list of recent bus event types. Useful for async write flows.