grist-widget-sdk 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +29 -0
- package/dist/actions.cjs +96 -0
- package/dist/actions.cjs.map +1 -0
- package/dist/actions.d.cts +36 -0
- package/dist/actions.d.ts +36 -0
- package/dist/actions.js +29 -0
- package/dist/actions.js.map +1 -0
- package/dist/app.cjs +49 -8
- package/dist/app.cjs.map +1 -1
- package/dist/app.d.cts +3 -2
- package/dist/app.d.ts +3 -2
- package/dist/app.js +4 -3
- package/dist/{chunk-4ZGVKGDF.js → chunk-A4KFKOEQ.js} +2 -58
- package/dist/chunk-A4KFKOEQ.js.map +1 -0
- package/dist/{chunk-OXA2VITC.js → chunk-CMN4M4S3.js} +6 -4
- package/dist/{chunk-OXA2VITC.js.map → chunk-CMN4M4S3.js.map} +1 -1
- package/dist/{chunk-MAPAWVQC.js → chunk-DKCRFNAW.js} +104 -17
- package/dist/chunk-DKCRFNAW.js.map +1 -0
- package/dist/{chunk-LSALXIGM.js → chunk-TUPCTWE5.js} +7 -3
- package/dist/chunk-TUPCTWE5.js.map +1 -0
- package/dist/chunk-YGQXH2P5.js +81 -0
- package/dist/chunk-YGQXH2P5.js.map +1 -0
- package/dist/{define-widget-B1FIvI-h.d.cts → define-widget-CYYdqRmB.d.cts} +39 -29
- package/dist/{define-widget-B9gEtJP0.d.ts → define-widget-D-NmSglS.d.ts} +39 -29
- package/dist/emulator/testing.cjs +49 -8
- package/dist/emulator/testing.cjs.map +1 -1
- package/dist/emulator/testing.d.cts +1 -0
- package/dist/emulator/testing.d.ts +1 -0
- package/dist/emulator/testing.js +8 -6
- package/dist/emulator/testing.js.map +1 -1
- package/dist/emulator.cjs.map +1 -1
- package/dist/emulator.js +3 -2
- package/dist/index.cjs +128 -23
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +30 -3
- package/dist/index.d.ts +30 -3
- package/dist/index.js +12 -11
- package/dist/{mount-grist-widget-Dtelqao_.d.cts → mount-grist-widget-D0mr__ga.d.cts} +1 -1
- package/dist/{mount-grist-widget-CkB-3iG_.d.ts → mount-grist-widget-DVcFl5Ys.d.ts} +1 -1
- package/package.json +11 -1
- package/dist/chunk-4ZGVKGDF.js.map +0 -1
- package/dist/chunk-LSALXIGM.js.map +0 -1
- package/dist/chunk-MAPAWVQC.js.map +0 -1
|
@@ -6,6 +6,7 @@ import { CreateGristEmulatorOptions, GristEmulator, EmulatorEvent } from '../emu
|
|
|
6
6
|
export { p as presets } from '../templates-CGmRIQBV.cjs';
|
|
7
7
|
import { GristWidgetEntry } from '../app.cjs';
|
|
8
8
|
import { GristReplicaDocument } from '../emulator.cjs';
|
|
9
|
+
import '../actions.cjs';
|
|
9
10
|
|
|
10
11
|
/**
|
|
11
12
|
* `renderWithGrist(ui, options)` — drop-in replacement for
|
|
@@ -6,6 +6,7 @@ import { CreateGristEmulatorOptions, GristEmulator, EmulatorEvent } from '../emu
|
|
|
6
6
|
export { p as presets } from '../templates-Cq94qOcf.js';
|
|
7
7
|
import { GristWidgetEntry } from '../app.js';
|
|
8
8
|
import { GristReplicaDocument } from '../emulator.js';
|
|
9
|
+
import '../actions.js';
|
|
9
10
|
|
|
10
11
|
/**
|
|
11
12
|
* `renderWithGrist(ui, options)` — drop-in replacement for
|
package/dist/emulator/testing.js
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
import {
|
|
2
2
|
GristWidgetApp,
|
|
3
|
-
entryProps
|
|
4
|
-
|
|
3
|
+
entryProps,
|
|
4
|
+
isGristWidgetEntry
|
|
5
|
+
} from "../chunk-TUPCTWE5.js";
|
|
5
6
|
import {
|
|
6
7
|
encodeGristCell,
|
|
7
8
|
isGristWidget,
|
|
8
9
|
resetGristForTests
|
|
9
|
-
} from "../chunk-
|
|
10
|
+
} from "../chunk-DKCRFNAW.js";
|
|
10
11
|
import {
|
|
11
12
|
createGristEmulator,
|
|
12
13
|
presets
|
|
13
|
-
} from "../chunk-
|
|
14
|
-
import "../chunk-
|
|
14
|
+
} from "../chunk-CMN4M4S3.js";
|
|
15
|
+
import "../chunk-A4KFKOEQ.js";
|
|
16
|
+
import "../chunk-YGQXH2P5.js";
|
|
15
17
|
|
|
16
18
|
// src/emulator/testing/render-with-grist.tsx
|
|
17
19
|
import { render } from "@testing-library/react";
|
|
@@ -131,7 +133,7 @@ function installJsdomStubs() {
|
|
|
131
133
|
// src/emulator/testing/smoke-test-widget.tsx
|
|
132
134
|
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
133
135
|
async function smokeTestWidget(input, extra = {}) {
|
|
134
|
-
const { App, options: given, widget, document, timeout = 1e4 } =
|
|
136
|
+
const { App, options: given, widget, document, timeout = 1e4 } = isGristWidgetEntry(input) ? { ...entryProps(input), ...extra } : input;
|
|
135
137
|
installJsdomStubs();
|
|
136
138
|
const options = given ?? widget?.options;
|
|
137
139
|
if (!options) throw new Error("smokeTestWidget: pass the widget's `widget` (defineGristWidget) or its `options` (GRIST_OPTIONS).");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/emulator/testing/render-with-grist.tsx","../../src/emulator/testing/declared-document.ts","../../src/emulator/testing/smoke-test-widget.tsx","../../src/emulator/testing/jsdom-stubs.ts","../../src/emulator/testing/matchers.ts","../../src/emulator/testing/index.ts"],"sourcesContent":["/**\n * `renderWithGrist(ui, options)` — drop-in replacement for\n * `@testing-library/react`'s `render(ui)` that boots a Grist emulator\n * (inline transport) and exposes it to the test.\n *\n * The emulator is mounted as `window.grist`. The SDK hooks (`useGrist`,\n * `useGristReady`, etc.) detect it via the same path they use in production.\n *\n * Cleanup is automatic when the test's container unmounts.\n */\n\nimport type { ReactElement } from \"react\"\n\nimport { render, type RenderOptions, type RenderResult } from \"@testing-library/react\"\n\nimport { GristWidgetApp } from \"../../app/mount-grist-widget\"\nimport type { GristWidget, UseGristOptions } from \"../../sdk\"\nimport { isGristWidget } from \"../../sdk/define-widget\"\nimport { resetGristForTests } from \"../../sdk/use-grist\"\nimport { declaredColumns, declaredDocument, mapEveryColumn } from \"./declared-document\"\nimport { createGristEmulator } from \"../create-grist-emulator\"\nimport type { CreateGristEmulatorOptions, GristEmulator } from \"../create-grist-emulator\"\nimport { presets } from \"../templates\"\n\nexport type RenderWithGristOptions = RenderOptions & {\n emulator?: CreateGristEmulatorOptions\n /** Skip mounting the emulator; you bring your own. Useful for shared fixtures. */\n existing?: GristEmulator\n /**\n * The widget's declaration (`defineGristWidget()`) or its `GRIST_OPTIONS`.\n * Given, the test runs the widget as `mountGristWidget()` does inside\n * Grist (provider, boundary, alerts), against a table with a column per\n * declared column, every one mapped, and the access it asks for granted.\n */\n widget?: GristWidget | UseGristOptions\n /**\n * With `widget`: the table's rows, under the declared names, as typed\n * reads give them (`{ Title: \"Ship\", Due: new Date(...) }`). The first is\n * selected. Defaults to one row of sample values.\n */\n rows?: ReadonlyArray<Record<string, unknown>>\n}\n\nexport type RenderWithGristResult = RenderResult & {\n emulator: GristEmulator\n}\n\nexport function renderWithGrist(\n ui: ReactElement,\n options: RenderWithGristOptions = {}\n): RenderWithGristResult {\n // Make `window.self !== window.top` evaluate to `true` so\n // `useGristAvailability` reports the widget as embedded. We only do this in\n // jsdom; real browsers already enforce this distinction for iframes.\n fakeIframeContext()\n\n resetGristForTests()\n\n const widgetOptions = options.widget ? (isGristWidget(options.widget) ? options.widget.options : options.widget) : undefined\n const columns = widgetOptions ? declaredColumns(widgetOptions) : []\n const emulatorOptions: CreateGristEmulatorOptions =\n widgetOptions && !options.emulator?.document\n ? {\n ...options.emulator,\n document: declaredDocument(columns, options.rows),\n widgetInteraction: options.emulator?.widgetInteraction ?? { access_level: widgetOptions.requiredAccess ?? \"read table\" },\n }\n : (options.emulator ?? { document: presets.blank() })\n const emulator = options.existing ?? createGristEmulator(emulatorOptions)\n if (widgetOptions && columns.length > 0) emulator.setColumnMappings(mapEveryColumn(columns))\n\n const Widget = () => ui\n const result = render(widgetOptions ? <GristWidgetApp App={Widget} options={widgetOptions} /> : ui, options)\n const originalUnmount = result.unmount\n result.unmount = () => {\n originalUnmount()\n if (!options.existing) emulator.dispose()\n resetGristForTests()\n }\n return Object.assign(result, { emulator })\n}\n\nfunction fakeIframeContext() {\n if (typeof window === \"undefined\") return\n if (window.self !== window.top) return // already iframe-like\n try {\n Object.defineProperty(window, \"top\", {\n configurable: true,\n get: () => ({}),\n })\n } catch {\n // Some jsdom versions block redefinition; fail silently — tests using\n // `useGristAvailability` will need to bypass it explicitly in that case.\n }\n}\n","/**\n * A test document from a widget's declared columns: one table, `Table1`,\n * with a column per declared column (typed after the first type it accepts)\n * and the given rows, each value stored as Grist stores it; and the mappings\n * that map every declared column to its namesake. Shared by\n * `smokeTestWidget` and `renderWithGrist({ widget })`.\n */\n\nimport { encodeGristCell } from \"../../sdk/runtime/cells\"\nimport type { GristColumnToMap, GristReplicaDocument, GristWidgetColumnMap, UseGristOptions } from \"../../sdk\"\n\nexport type Column = { name: string; type: string; declared: string | undefined; allowMultiple: boolean }\n\nexport function declaredColumns(options: UseGristOptions): Column[] {\n return (options.columns ?? []).map((column: string | GristColumnToMap) =>\n typeof column === \"string\"\n ? { name: column, type: \"Any\", declared: undefined, allowMultiple: false }\n : { name: column.name, type: firstType(column.type), declared: column.type, allowMultiple: column.allowMultiple === true }\n )\n}\n\n/** The first Grist type a column accepts: `\"Text,Ref,Choice\"` gives `\"Text\"`. */\nfunction firstType(type: string | undefined): string {\n return (type ?? \"Any\").split(\",\")[0].trim() || \"Any\"\n}\n\nexport function mapEveryColumn(columns: Column[]): GristWidgetColumnMap {\n return Object.fromEntries(columns.map((c) => [c.name, c.allowMultiple ? [c.name] : c.name]))\n}\n\n/**\n * One table with a column per declared column, and `rows` (under the declared\n * names, as typed reads give them: a `Date`, a `string[]`…), or one row of\n * sample values.\n */\nexport function declaredDocument(columns: Column[], rows?: ReadonlyArray<Record<string, unknown>>): GristReplicaDocument {\n const given = rows ?? [Object.fromEntries(columns.map((c) => [c.name, sampleValue(c.type)]))]\n return {\n generatedAt: \"1970-01-01T00:00:00.000Z\",\n docName: \"Test document\",\n mode: \"schema+data\",\n tables: {\n Table1: {\n columns: Object.fromEntries(columns.map((c) => [c.name, { type: c.type }])),\n rows: given.map((row, i) => ({\n id: typeof row.id === \"number\" ? row.id : i + 1,\n ...Object.fromEntries(columns.map((c) => [c.name, encodeGristCell(c.declared, row[c.name] ?? null)])),\n })),\n },\n },\n }\n}\n\nfunction sampleValue(type: string): unknown {\n switch (type) {\n case \"Numeric\":\n case \"Int\":\n return 42\n case \"Bool\":\n return true\n case \"Date\":\n case \"DateTime\":\n return 1767225600 // 2026-01-01, as Grist stores it: epoch seconds\n default:\n return \"Sample\"\n }\n}\n","import { Component, type ComponentType, type ReactNode } from \"react\"\nimport { waitFor } from \"@testing-library/react\"\n\nimport { entryProps, GristWidgetApp, type GristWidgetEntry } from \"../../app/mount-grist-widget\"\nimport type { GristReplicaDocument, GristWidget, UseGristOptions } from \"../../sdk\"\nimport { declaredColumns, declaredDocument, mapEveryColumn } from \"./declared-document\"\nimport { installJsdomStubs } from \"./jsdom-stubs\"\nimport { renderWithGrist } from \"./render-with-grist\"\n\nexport type SmokeTestWidgetOptions = {\n /** The widget, as `src/main.tsx` passes it to `mountGristWidget()`. */\n App: ComponentType\n /** The widget's `GRIST_OPTIONS` (or pass `widget`). */\n options?: UseGristOptions\n /** The widget's declaration, from `defineGristWidget()` (or pass `options`). */\n widget?: GristWidget\n /**\n * The Grist document to run against. Defaults to one table with a column\n * per column the widget declares, typed after the first type it accepts,\n * and one row of sample values.\n */\n document?: GristReplicaDocument\n /** How long to wait for the widget to render, in ms. Defaults to 10 000. */\n timeout?: number\n}\n\n/**\n * Mounts the widget the way `mountGristWidget()` does inside Grist, against\n * an emulated Grist document with every column it declares mapped and the\n * access it requests granted, and waits until it renders past\n * `GristBoundary`. Resolves once it has,\n * rejects with the widget's own error if it throws, or with what the page\n * showed instead if it never gets there. Then unmounts.\n *\n * A widget's whole smoke test, given its entry module as `mountGristWidget()`\n * takes it (the widget, and its `widget` declaration or `GRIST_OPTIONS`):\n *\n * ```tsx\n * import { it } from \"vitest\"\n * import { smokeTestWidget } from \"grist-widget-sdk/emulator/testing\"\n * import * as entry from \"./App\"\n *\n * it(\"renders inside Grist\", () => smokeTestWidget(entry))\n * ```\n *\n * It only reads the declaration, so it stays valid as the widget changes,\n * and it improves with each SDK release. `smokeTestWidget({ App, options })`\n * and `smokeTestWidget({ App, widget })` work too.\n */\nexport async function smokeTestWidget(\n input: SmokeTestWidgetOptions | GristWidgetEntry,\n extra: Pick<SmokeTestWidgetOptions, \"document\" | \"timeout\"> = {}\n): Promise<void> {\n const { App, options: given, widget, document, timeout = 10_000 } =\n \"App\" in input ? input : { ...entryProps(input), ...extra }\n installJsdomStubs()\n const options = given ?? widget?.options\n if (!options) throw new Error(\"smokeTestWidget: pass the widget's `widget` (defineGristWidget) or its `options` (GRIST_OPTIONS).\")\n\n const crash: { error: unknown } = { error: undefined }\n function Probe() {\n return (\n <CatchRenderError crash={crash}>\n <div data-grist-smoke-test=\"\">\n <App />\n </div>\n </CatchRenderError>\n )\n }\n\n const columns = declaredColumns(options)\n const gristDocument = document ?? declaredDocument(columns)\n const result = renderWithGrist(<GristWidgetApp App={Probe} options={options} />, {\n emulator: {\n document: gristDocument,\n // Granted as the user would in Grist's widget panel: the access the\n // widget asks for, unless the given document says otherwise.\n widgetInteraction: gristDocument.widgetInteraction ?? { access_level: options.requiredAccess ?? \"read table\" },\n },\n })\n try {\n if (columns.length > 0) result.emulator.setColumnMappings(mapEveryColumn(columns))\n\n try {\n await waitFor(\n () => {\n if (crash.error !== undefined) return\n const probe = result.container.querySelector(\"[data-grist-smoke-test]\")\n if (!probe || !probe.hasChildNodes()) throw new Error(\"not rendered yet\")\n },\n { timeout }\n )\n } catch {\n const shown = result.container.textContent?.trim().replace(/\\s+/g, \" \") || \"nothing\"\n throw new Error(\n `smokeTestWidget: the widget didn't render past GristBoundary within ${timeout} ms. ` +\n `The page showed: \"${shown.slice(0, 300)}\".`\n )\n }\n if (crash.error !== undefined) throw crash.error\n } finally {\n result.unmount()\n }\n}\n\n/** Records the widget's render error, so the test fails with it rather than a timeout. */\nclass CatchRenderError extends Component<{ crash: { error: unknown }; children: ReactNode }, { failed: boolean }> {\n state = { failed: false }\n\n static getDerivedStateFromError() {\n return { failed: true }\n }\n\n componentDidCatch(error: unknown) {\n this.props.crash.error = error\n }\n\n render() {\n return this.state.failed ? null : this.props.children\n }\n}\n","/**\n * Browser APIs jsdom lacks but widget UI commonly uses, as inert stand-ins:\n * `ResizeObserver` (charts, virtualized lists, popovers) and\n * `window.matchMedia` (the theme, responsive hooks; it reports a light,\n * wide screen). Only installs what's missing, so a test's own mock wins.\n *\n * {@link smokeTestWidget} calls it. For a widget's other tests, call it at\n * the top of the test file or in a Vitest setup file.\n */\nexport function installJsdomStubs(): void {\n if (typeof window === \"undefined\") return\n\n globalThis.ResizeObserver ??= class ResizeObserver {\n observe() {}\n unobserve() {}\n disconnect() {}\n }\n\n window.matchMedia ??= (query: string) =>\n ({\n matches: false,\n media: query,\n onchange: null,\n addEventListener() {},\n removeEventListener() {},\n addListener() {},\n removeListener() {},\n dispatchEvent: () => false,\n }) as MediaQueryList\n}\n","/**\n * Vitest helpers to assert what the widget did to the document.\n *\n * Usage:\n *\n * const { emulator } = renderWithGrist(<MyWidget />)\n * await userEvent.click(screen.getByText(\"Add\"))\n * expect(actionsOf(emulator)).toContainEqual([\"AddRecord\", \"Tasks\", null, { title: \"X\" }])\n */\n\nimport type { GristEmulator } from \"../create-grist-emulator\"\nimport type { EmulatorEvent } from \"../core/event-bus\"\n\nexport function actionsOf(emulator: GristEmulator): unknown[][] {\n return emulator.bus\n .history()\n .filter((e: EmulatorEvent): e is Extract<EmulatorEvent, { type: \"user-action\" }> => e.type === \"user-action\")\n .map((e) => e.action as unknown[])\n}\n\nexport function eventsOf(emulator: GristEmulator): readonly EmulatorEvent[] {\n return emulator.bus.history()\n}\n\n/** Bus event kinds aligned with grist host callbacks (see inline transport). */\nexport type WaitForEventKind = \"ready\" | \"record\" | \"records\" | \"options\" | \"theme\" | \"cursor\"\n\nexport type WaitForEventOptions = {\n timeoutMs?: number\n}\n\nfunction predicateForKind(kind: WaitForEventKind): (event: EmulatorEvent) => boolean {\n switch (kind) {\n case \"ready\":\n return (e) => e.type === \"ready\"\n case \"record\":\n case \"cursor\":\n return (e) => e.type === \"cursor-change\"\n case \"records\":\n return (e) => e.type === \"data-change\"\n case \"options\":\n return (e) => e.type === \"options-change\"\n case \"theme\":\n return (e) => e.type === \"theme-change\"\n }\n}\n\nfunction waitForNextEvent(\n emulator: GristEmulator,\n predicate: (event: EmulatorEvent) => boolean,\n timeoutMs: number,\n label?: string\n): Promise<EmulatorEvent> {\n return new Promise((resolve, reject) => {\n const timer = setTimeout(() => {\n off()\n const hint = label ? ` (kind: ${label})` : \"\"\n const recent = emulator.bus\n .history()\n .slice(-5)\n .map((e) => e.type)\n .join(\", \")\n reject(\n new Error(\n `waitForEvent timed out after ${timeoutMs}ms${hint}.` +\n (recent ? ` Recent bus events: ${recent}.` : \" No bus events yet.\")\n )\n )\n }, timeoutMs)\n const off = emulator.bus.on((event) => {\n if (predicate(event)) {\n clearTimeout(timer)\n off()\n resolve(event)\n }\n })\n })\n}\n\nexport function waitForEvent(\n emulator: GristEmulator,\n kind: WaitForEventKind,\n options?: WaitForEventOptions\n): Promise<EmulatorEvent>\nexport function waitForEvent(\n emulator: GristEmulator,\n predicate: (event: EmulatorEvent) => boolean,\n timeoutMs?: number\n): Promise<EmulatorEvent>\nexport function waitForEvent(\n emulator: GristEmulator,\n kindOrPredicate: WaitForEventKind | ((event: EmulatorEvent) => boolean),\n timeoutOrOptions: number | WaitForEventOptions = 1000\n): Promise<EmulatorEvent> {\n if (typeof kindOrPredicate === \"string\") {\n const opts = typeof timeoutOrOptions === \"object\" ? timeoutOrOptions : {}\n const timeoutMs = opts.timeoutMs ?? 1000\n return waitForNextEvent(emulator, predicateForKind(kindOrPredicate), timeoutMs, kindOrPredicate)\n }\n const timeoutMs = typeof timeoutOrOptions === \"number\" ? timeoutOrOptions : 1000\n return waitForNextEvent(emulator, kindOrPredicate, timeoutMs)\n}\n","/**\n * Test entry point — `grist-widget-sdk/emulator/testing`.\n *\n * Bundles `renderWithGrist`, matchers, document presets, and the most-used\n * @testing-library/react primitives so a typical test file only needs one\n * import line:\n *\n * import {\n * renderWithGrist, presets, screen, fireEvent, waitFor, actionsOf,\n * } from \"grist-widget-sdk/emulator/testing\"\n */\n\nexport { renderWithGrist } from \"./render-with-grist\"\nexport type {\n RenderWithGristOptions,\n RenderWithGristResult,\n} from \"./render-with-grist\"\n\nexport { smokeTestWidget, type SmokeTestWidgetOptions } from \"./smoke-test-widget\"\nexport { installJsdomStubs } from \"./jsdom-stubs\"\n\nexport {\n actionsOf,\n eventsOf,\n waitForEvent,\n type WaitForEventKind,\n type WaitForEventOptions,\n} from \"./matchers\"\n\n// Document fixtures (re-export from the emulator entry for convenience).\nexport { presets } from \"../templates\"\n\n// @testing-library/react convenience re-exports — see AGENTS.md\nexport {\n act,\n cleanup,\n fireEvent,\n render,\n screen,\n waitFor,\n within,\n} from \"@testing-library/react\"\nexport type { RenderOptions, RenderResult } from \"@testing-library/react\"\n"],"mappings":";;;;;;;;;;;;;;;;AAaA,SAAS,cAAqD;;;ACAvD,SAAS,gBAAgB,SAAoC;AAClE,UAAQ,QAAQ,WAAW,CAAC,GAAG;AAAA,IAAI,CAAC,WAClC,OAAO,WAAW,WACd,EAAE,MAAM,QAAQ,MAAM,OAAO,UAAU,QAAW,eAAe,MAAM,IACvE,EAAE,MAAM,OAAO,MAAM,MAAM,UAAU,OAAO,IAAI,GAAG,UAAU,OAAO,MAAM,eAAe,OAAO,kBAAkB,KAAK;AAAA,EAC7H;AACF;AAGA,SAAS,UAAU,MAAkC;AACnD,UAAQ,QAAQ,OAAO,MAAM,GAAG,EAAE,CAAC,EAAE,KAAK,KAAK;AACjD;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C,MAAM,EAAE,SAAS;AAAA,EAC7B;AACF;AAEA,SAAS,iBACP,UACA,WACA,WACA,OACwB;AACxB,SAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,UAAM,QAAQ,WAAW,MAAM;AAC7B,UAAI;AACJ,YAAM,OAAO,QAAQ,WAAW,KAAK,MAAM;AAC3C,YAAM,SAAS,SAAS,IACrB,QAAQ,EACR,MAAM,EAAE,EACR,IAAI,CAAC,MAAM,EAAE,IAAI,EACjB,KAAK,IAAI;AACZ;AAAA,QACE,IAAI;AAAA,UACF,gCAAgC,SAAS,KAAK,IAAI,OAC/C,SAAS,uBAAuB,MAAM,MAAM;AAAA,QACjD;AAAA,MACF;AAAA,IACF,GAAG,SAAS;AACZ,UAAM,MAAM,SAAS,IAAI,GAAG,CAAC,UAAU;AACrC,UAAI,UAAU,KAAK,GAAG;AACpB,qBAAa,KAAK;AAClB,YAAI;AACJ,gBAAQ,KAAK;AAAA,MACf;AAAA,IACF,CAAC;AAAA,EACH,CAAC;AACH;AAYO,SAAS,aACd,UACA,iBACA,mBAAiD,KACzB;AACxB,MAAI,OAAO,oBAAoB,UAAU;AACvC,UAAM,OAAO,OAAO,qBAAqB,WAAW,mBAAmB,CAAC;AACxE,UAAMC,aAAY,KAAK,aAAa;AACpC,WAAO,iBAAiB,UAAU,iBAAiB,eAAe,GAAGA,YAAW,eAAe;AAAA,EACjG;AACA,QAAM,YAAY,OAAO,qBAAqB,WAAW,mBAAmB;AAC5E,SAAO,iBAAiB,UAAU,iBAAiB,SAAS;AAC9D;;;ACpEA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA,UAAAC;AAAA,EACA;AAAA,EACA,WAAAC;AAAA,EACA;AAAA,OACK;","names":["jsx","timeoutMs","render","waitFor"]}
|
|
1
|
+
{"version":3,"sources":["../../src/emulator/testing/render-with-grist.tsx","../../src/emulator/testing/declared-document.ts","../../src/emulator/testing/smoke-test-widget.tsx","../../src/emulator/testing/jsdom-stubs.ts","../../src/emulator/testing/matchers.ts","../../src/emulator/testing/index.ts"],"sourcesContent":["/**\n * `renderWithGrist(ui, options)` — drop-in replacement for\n * `@testing-library/react`'s `render(ui)` that boots a Grist emulator\n * (inline transport) and exposes it to the test.\n *\n * The emulator is mounted as `window.grist`. The SDK hooks (`useGrist`,\n * `useGristReady`, etc.) detect it via the same path they use in production.\n *\n * Cleanup is automatic when the test's container unmounts.\n */\n\nimport type { ReactElement } from \"react\"\n\nimport { render, type RenderOptions, type RenderResult } from \"@testing-library/react\"\n\nimport { GristWidgetApp } from \"../../app/mount-grist-widget\"\nimport type { GristWidget, UseGristOptions } from \"../../sdk\"\nimport { isGristWidget } from \"../../sdk/define-widget\"\nimport { resetGristForTests } from \"../../sdk/use-grist\"\nimport { declaredColumns, declaredDocument, mapEveryColumn } from \"./declared-document\"\nimport { createGristEmulator } from \"../create-grist-emulator\"\nimport type { CreateGristEmulatorOptions, GristEmulator } from \"../create-grist-emulator\"\nimport { presets } from \"../templates\"\n\nexport type RenderWithGristOptions = RenderOptions & {\n emulator?: CreateGristEmulatorOptions\n /** Skip mounting the emulator; you bring your own. Useful for shared fixtures. */\n existing?: GristEmulator\n /**\n * The widget's declaration (`defineGristWidget()`) or its `GRIST_OPTIONS`.\n * Given, the test runs the widget as `mountGristWidget()` does inside\n * Grist (provider, boundary, alerts), against a table with a column per\n * declared column, every one mapped, and the access it asks for granted.\n */\n widget?: GristWidget | UseGristOptions\n /**\n * With `widget`: the table's rows, under the declared names, as typed\n * reads give them (`{ Title: \"Ship\", Due: new Date(...) }`). The first is\n * selected. Defaults to one row of sample values.\n */\n rows?: ReadonlyArray<Record<string, unknown>>\n}\n\nexport type RenderWithGristResult = RenderResult & {\n emulator: GristEmulator\n}\n\nexport function renderWithGrist(\n ui: ReactElement,\n options: RenderWithGristOptions = {}\n): RenderWithGristResult {\n // Make `window.self !== window.top` evaluate to `true` so\n // `useGristAvailability` reports the widget as embedded. We only do this in\n // jsdom; real browsers already enforce this distinction for iframes.\n fakeIframeContext()\n\n resetGristForTests()\n\n const widgetOptions = options.widget ? (isGristWidget(options.widget) ? options.widget.options : options.widget) : undefined\n const columns = widgetOptions ? declaredColumns(widgetOptions) : []\n const emulatorOptions: CreateGristEmulatorOptions =\n widgetOptions && !options.emulator?.document\n ? {\n ...options.emulator,\n document: declaredDocument(columns, options.rows),\n widgetInteraction: options.emulator?.widgetInteraction ?? { access_level: widgetOptions.requiredAccess ?? \"read table\" },\n }\n : (options.emulator ?? { document: presets.blank() })\n const emulator = options.existing ?? createGristEmulator(emulatorOptions)\n if (widgetOptions && columns.length > 0) emulator.setColumnMappings(mapEveryColumn(columns))\n\n const Widget = () => ui\n const result = render(widgetOptions ? <GristWidgetApp App={Widget} options={widgetOptions} /> : ui, options)\n const originalUnmount = result.unmount\n result.unmount = () => {\n originalUnmount()\n if (!options.existing) emulator.dispose()\n resetGristForTests()\n }\n return Object.assign(result, { emulator })\n}\n\nfunction fakeIframeContext() {\n if (typeof window === \"undefined\") return\n if (window.self !== window.top) return // already iframe-like\n try {\n Object.defineProperty(window, \"top\", {\n configurable: true,\n get: () => ({}),\n })\n } catch {\n // Some jsdom versions block redefinition; fail silently — tests using\n // `useGristAvailability` will need to bypass it explicitly in that case.\n }\n}\n","/**\n * A test document from a widget's declared columns: one table, `Table1`,\n * with a column per declared column (typed after the first type it accepts)\n * and the given rows, each value stored as Grist stores it; and the mappings\n * that map every declared column to its namesake. Shared by\n * `smokeTestWidget` and `renderWithGrist({ widget })`.\n */\n\nimport { encodeGristCell } from \"../../sdk/runtime/cells\"\nimport type { GristColumnToMap, GristReplicaDocument, GristWidgetColumnMap, UseGristOptions } from \"../../sdk\"\n\nexport type Column = { name: string; type: string; declared: string | undefined; allowMultiple: boolean }\n\nexport function declaredColumns(options: UseGristOptions): Column[] {\n return (options.columns ?? []).map((column: string | GristColumnToMap) =>\n typeof column === \"string\"\n ? { name: column, type: \"Any\", declared: undefined, allowMultiple: false }\n : { name: column.name, type: firstType(column.type), declared: column.type, allowMultiple: column.allowMultiple === true }\n )\n}\n\n/** The first Grist type a column accepts: `\"Text,Ref,Choice\"` gives `\"Text\"`. */\nfunction firstType(type: string | undefined): string {\n return (type ?? \"Any\").split(\",\")[0].trim() || \"Any\"\n}\n\nexport function mapEveryColumn(columns: Column[]): GristWidgetColumnMap {\n return Object.fromEntries(columns.map((c) => [c.name, c.allowMultiple ? [c.name] : c.name]))\n}\n\n/**\n * One table with a column per declared column, and `rows` (under the declared\n * names, as typed reads give them: a `Date`, a `string[]`…), or one row of\n * sample values.\n */\nexport function declaredDocument(columns: Column[], rows?: ReadonlyArray<Record<string, unknown>>): GristReplicaDocument {\n const given = rows ?? [Object.fromEntries(columns.map((c) => [c.name, sampleValue(c.type)]))]\n return {\n generatedAt: \"1970-01-01T00:00:00.000Z\",\n docName: \"Test document\",\n mode: \"schema+data\",\n tables: {\n Table1: {\n columns: Object.fromEntries(columns.map((c) => [c.name, { type: c.type }])),\n rows: given.map((row, i) => ({\n id: typeof row.id === \"number\" ? row.id : i + 1,\n ...Object.fromEntries(columns.map((c) => [c.name, encodeGristCell(c.declared, row[c.name] ?? null)])),\n })),\n },\n },\n }\n}\n\nfunction sampleValue(type: string): unknown {\n switch (type) {\n case \"Numeric\":\n case \"Int\":\n return 42\n case \"Bool\":\n return true\n case \"Date\":\n case \"DateTime\":\n return 1767225600 // 2026-01-01, as Grist stores it: epoch seconds\n default:\n return \"Sample\"\n }\n}\n","import { Component, type ComponentType, type ReactNode } from \"react\"\nimport { waitFor } from \"@testing-library/react\"\n\nimport { entryProps, GristWidgetApp, isGristWidgetEntry, type GristWidgetEntry } from \"../../app/mount-grist-widget\"\nimport type { GristReplicaDocument, GristWidget, UseGristOptions } from \"../../sdk\"\nimport { declaredColumns, declaredDocument, mapEveryColumn } from \"./declared-document\"\nimport { installJsdomStubs } from \"./jsdom-stubs\"\nimport { renderWithGrist } from \"./render-with-grist\"\n\nexport type SmokeTestWidgetOptions = {\n /** The widget, as `src/main.tsx` passes it to `mountGristWidget()`. */\n App: ComponentType\n /** The widget's `GRIST_OPTIONS` (or pass `widget`). */\n options?: UseGristOptions\n /** The widget's declaration, from `defineGristWidget()` (or pass `options`). */\n widget?: GristWidget\n /**\n * The Grist document to run against. Defaults to one table with a column\n * per column the widget declares, typed after the first type it accepts,\n * and one row of sample values.\n */\n document?: GristReplicaDocument\n /** How long to wait for the widget to render, in ms. Defaults to 10 000. */\n timeout?: number\n}\n\n/**\n * Mounts the widget the way `mountGristWidget()` does inside Grist, against\n * an emulated Grist document with every column it declares mapped and the\n * access it requests granted, and waits until it renders past\n * `GristBoundary`. Resolves once it has,\n * rejects with the widget's own error if it throws, or with what the page\n * showed instead if it never gets there. Then unmounts.\n *\n * A widget's whole smoke test, given its entry module as `mountGristWidget()`\n * takes it (the widget, and its `widget` declaration or `GRIST_OPTIONS`):\n *\n * ```tsx\n * import { it } from \"vitest\"\n * import { smokeTestWidget } from \"grist-widget-sdk/emulator/testing\"\n * import * as entry from \"./App\"\n *\n * it(\"renders inside Grist\", () => smokeTestWidget(entry))\n * ```\n *\n * It only reads the declaration, so it stays valid as the widget changes,\n * and it improves with each SDK release. `smokeTestWidget({ App, options })`\n * and `smokeTestWidget({ App, widget })` work too.\n */\nexport async function smokeTestWidget(\n input: SmokeTestWidgetOptions | GristWidgetEntry,\n extra: Pick<SmokeTestWidgetOptions, \"document\" | \"timeout\"> = {}\n): Promise<void> {\n const { App, options: given, widget, document, timeout = 10_000 } =\n isGristWidgetEntry(input) ? { ...entryProps(input), ...extra } : input\n installJsdomStubs()\n const options = given ?? widget?.options\n if (!options) throw new Error(\"smokeTestWidget: pass the widget's `widget` (defineGristWidget) or its `options` (GRIST_OPTIONS).\")\n\n const crash: { error: unknown } = { error: undefined }\n function Probe() {\n return (\n <CatchRenderError crash={crash}>\n <div data-grist-smoke-test=\"\">\n <App />\n </div>\n </CatchRenderError>\n )\n }\n\n const columns = declaredColumns(options)\n const gristDocument = document ?? declaredDocument(columns)\n const result = renderWithGrist(<GristWidgetApp App={Probe} options={options} />, {\n emulator: {\n document: gristDocument,\n // Granted as the user would in Grist's widget panel: the access the\n // widget asks for, unless the given document says otherwise.\n widgetInteraction: gristDocument.widgetInteraction ?? { access_level: options.requiredAccess ?? \"read table\" },\n },\n })\n try {\n if (columns.length > 0) result.emulator.setColumnMappings(mapEveryColumn(columns))\n\n try {\n await waitFor(\n () => {\n if (crash.error !== undefined) return\n const probe = result.container.querySelector(\"[data-grist-smoke-test]\")\n if (!probe || !probe.hasChildNodes()) throw new Error(\"not rendered yet\")\n },\n { timeout }\n )\n } catch {\n const shown = result.container.textContent?.trim().replace(/\\s+/g, \" \") || \"nothing\"\n throw new Error(\n `smokeTestWidget: the widget didn't render past GristBoundary within ${timeout} ms. ` +\n `The page showed: \"${shown.slice(0, 300)}\".`\n )\n }\n if (crash.error !== undefined) throw crash.error\n } finally {\n result.unmount()\n }\n}\n\n/** Records the widget's render error, so the test fails with it rather than a timeout. */\nclass CatchRenderError extends Component<{ crash: { error: unknown }; children: ReactNode }, { failed: boolean }> {\n state = { failed: false }\n\n static getDerivedStateFromError() {\n return { failed: true }\n }\n\n componentDidCatch(error: unknown) {\n this.props.crash.error = error\n }\n\n render() {\n return this.state.failed ? null : this.props.children\n }\n}\n","/**\n * Browser APIs jsdom lacks but widget UI commonly uses, as inert stand-ins:\n * `ResizeObserver` (charts, virtualized lists, popovers) and\n * `window.matchMedia` (the theme, responsive hooks; it reports a light,\n * wide screen). Only installs what's missing, so a test's own mock wins.\n *\n * {@link smokeTestWidget} calls it. For a widget's other tests, call it at\n * the top of the test file or in a Vitest setup file.\n */\nexport function installJsdomStubs(): void {\n if (typeof window === \"undefined\") return\n\n globalThis.ResizeObserver ??= class ResizeObserver {\n observe() {}\n unobserve() {}\n disconnect() {}\n }\n\n window.matchMedia ??= (query: string) =>\n ({\n matches: false,\n media: query,\n onchange: null,\n addEventListener() {},\n removeEventListener() {},\n addListener() {},\n removeListener() {},\n dispatchEvent: () => false,\n }) as MediaQueryList\n}\n","/**\n * Vitest helpers to assert what the widget did to the document.\n *\n * Usage:\n *\n * const { emulator } = renderWithGrist(<MyWidget />)\n * await userEvent.click(screen.getByText(\"Add\"))\n * expect(actionsOf(emulator)).toContainEqual([\"AddRecord\", \"Tasks\", null, { title: \"X\" }])\n */\n\nimport type { GristEmulator } from \"../create-grist-emulator\"\nimport type { EmulatorEvent } from \"../core/event-bus\"\n\nexport function actionsOf(emulator: GristEmulator): unknown[][] {\n return emulator.bus\n .history()\n .filter((e: EmulatorEvent): e is Extract<EmulatorEvent, { type: \"user-action\" }> => e.type === \"user-action\")\n .map((e) => e.action as unknown[])\n}\n\nexport function eventsOf(emulator: GristEmulator): readonly EmulatorEvent[] {\n return emulator.bus.history()\n}\n\n/** Bus event kinds aligned with grist host callbacks (see inline transport). */\nexport type WaitForEventKind = \"ready\" | \"record\" | \"records\" | \"options\" | \"theme\" | \"cursor\"\n\nexport type WaitForEventOptions = {\n timeoutMs?: number\n}\n\nfunction predicateForKind(kind: WaitForEventKind): (event: EmulatorEvent) => boolean {\n switch (kind) {\n case \"ready\":\n return (e) => e.type === \"ready\"\n case \"record\":\n case \"cursor\":\n return (e) => e.type === \"cursor-change\"\n case \"records\":\n return (e) => e.type === \"data-change\"\n case \"options\":\n return (e) => e.type === \"options-change\"\n case \"theme\":\n return (e) => e.type === \"theme-change\"\n }\n}\n\nfunction waitForNextEvent(\n emulator: GristEmulator,\n predicate: (event: EmulatorEvent) => boolean,\n timeoutMs: number,\n label?: string\n): Promise<EmulatorEvent> {\n return new Promise((resolve, reject) => {\n const timer = setTimeout(() => {\n off()\n const hint = label ? ` (kind: ${label})` : \"\"\n const recent = emulator.bus\n .history()\n .slice(-5)\n .map((e) => e.type)\n .join(\", \")\n reject(\n new Error(\n `waitForEvent timed out after ${timeoutMs}ms${hint}.` +\n (recent ? ` Recent bus events: ${recent}.` : \" No bus events yet.\")\n )\n )\n }, timeoutMs)\n const off = emulator.bus.on((event) => {\n if (predicate(event)) {\n clearTimeout(timer)\n off()\n resolve(event)\n }\n })\n })\n}\n\nexport function waitForEvent(\n emulator: GristEmulator,\n kind: WaitForEventKind,\n options?: WaitForEventOptions\n): Promise<EmulatorEvent>\nexport function waitForEvent(\n emulator: GristEmulator,\n predicate: (event: EmulatorEvent) => boolean,\n timeoutMs?: number\n): Promise<EmulatorEvent>\nexport function waitForEvent(\n emulator: GristEmulator,\n kindOrPredicate: WaitForEventKind | ((event: EmulatorEvent) => boolean),\n timeoutOrOptions: number | WaitForEventOptions = 1000\n): Promise<EmulatorEvent> {\n if (typeof kindOrPredicate === \"string\") {\n const opts = typeof timeoutOrOptions === \"object\" ? timeoutOrOptions : {}\n const timeoutMs = opts.timeoutMs ?? 1000\n return waitForNextEvent(emulator, predicateForKind(kindOrPredicate), timeoutMs, kindOrPredicate)\n }\n const timeoutMs = typeof timeoutOrOptions === \"number\" ? timeoutOrOptions : 1000\n return waitForNextEvent(emulator, kindOrPredicate, timeoutMs)\n}\n","/**\n * Test entry point — `grist-widget-sdk/emulator/testing`.\n *\n * Bundles `renderWithGrist`, matchers, document presets, and the most-used\n * @testing-library/react primitives so a typical test file only needs one\n * import line:\n *\n * import {\n * renderWithGrist, presets, screen, fireEvent, waitFor, actionsOf,\n * } from \"grist-widget-sdk/emulator/testing\"\n */\n\nexport { renderWithGrist } from \"./render-with-grist\"\nexport type {\n RenderWithGristOptions,\n RenderWithGristResult,\n} from \"./render-with-grist\"\n\nexport { smokeTestWidget, type SmokeTestWidgetOptions } from \"./smoke-test-widget\"\nexport { installJsdomStubs } from \"./jsdom-stubs\"\n\nexport {\n actionsOf,\n eventsOf,\n waitForEvent,\n type WaitForEventKind,\n type WaitForEventOptions,\n} from \"./matchers\"\n\n// Document fixtures (re-export from the emulator entry for convenience).\nexport { presets } from \"../templates\"\n\n// @testing-library/react convenience re-exports — see AGENTS.md\nexport {\n act,\n cleanup,\n fireEvent,\n render,\n screen,\n waitFor,\n within,\n} from \"@testing-library/react\"\nexport type { RenderOptions, Render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