@thescaffold/editor-testing
v0.2.0
Published
Test utilities for X Editor addon authors
Readme
@thescaffold/editor-testing
Test utilities for X Editor addon authors. Provides createTestEditor() which spins up a real EditorContext in Node (via happy-dom) so you can unit-test addons with vitest or jest without a browser.
Installation
npm install --save-dev @thescaffold/editor-testingUsage
import {
createTestEditor,
simulateKeydown,
getDocJSON,
} from "@thescaffold/editor-testing";
import {
SchemaAddon,
BoldAddon,
CommandsAddon,
KeymapAddon,
} from "@thescaffold/editor-addons";
import { expect, it, describe } from "vitest";
describe("BoldAddon", () => {
it("toggles bold on Mod-b", async () => {
const editor = await createTestEditor({
addons: [SchemaAddon(), CommandsAddon(), BoldAddon(), KeymapAddon()],
});
simulateKeydown(editor, "Mod-b");
expect(editor.selection.hasMark("bold")).toBe(true);
editor.destroy();
});
});API
createTestEditor(options)
Returns a TestEditorHandle (same as EditorHandle + container + destroy()).
const editor = await createTestEditor({
addons: [SchemaAddon(), BoldAddon()],
mode: "doc",
content: "# Hello",
});Simulate helpers
| Function | Description |
| ------------------------------ | ----------------------------------- |
| simulateKeydown(editor, key) | Fire a key binding (e.g. 'Mod-b') |
| simulatePaste(editor, text) | Fire a paste event |
| simulateInput(editor, text) | Fire an input-rules event |
| simulateFocus(editor) | Fire focus event |
| simulateBlur(editor) | Fire blur event |
Document fixtures
| Helper | Description |
| -------------------------- | ------------------------------- |
| getDocJSON(editor) | Snapshot current doc as JSON |
| setDocJSON(editor, json) | Load a doc JSON fixture |
| FIXTURE_DOCS.empty | Empty doc |
| FIXTURE_DOCS.paragraph | Single paragraph |
| FIXTURE_DOCS.richText | Heading + bold paragraph + list |
License
MIT
