Skip to content
File

Blob: tests/client/util/editor-mocks.ts

typescript57 lines
1import type { Editor } from "@tiptap/core";
2import type { Node as PMNode, Schema } from "@tiptap/pm/model";
3import { EditorState, type Transaction } from "@tiptap/pm/state";
4import type { EditorView } from "@tiptap/pm/view";
5import { vi } from "vitest";
6import type { FormattingToolbarEditor } from "@/client/components/editor/controllers/formatting-toolbar-state";
7 
8export function createDispatchingTestEditor(schema: Schema, doc: PMNode) {
9 let state = EditorState.create({ schema, doc });
10 const focus = vi.fn();
11 const updateState = vi.fn((nextState: EditorState) => {
12 state = nextState;
13 view.state = nextState;
14 });
15 
16 const view = {
17 state,
18 isDestroyed: false,
19 dispatch(tr: Transaction) {
20 state = state.apply(tr);
21 view.state = state;
22 },
23 focus,
24 updateState,
25 } as Pick<EditorView, "dispatch" | "focus" | "updateState"> & {
26 state: EditorState;
27 isDestroyed: boolean;
28 focus: ReturnType<typeof vi.fn>;
29 updateState: ReturnType<typeof vi.fn>;
30 };
31 
32 const editor = {
33 get state() {
34 return state;
35 },
36 view,
37 } as unknown as Editor;
38 
39 return { editor, view };
40}
41 
42interface FormattingToolbarEditorOverrides {
43 dragging?: FormattingToolbarEditor["view"]["dragging"];
44 isActive?: FormattingToolbarEditor["isActive"];
45}
46 
47export function createFormattingToolbarEditor(
48 state: EditorState,
49 overrides: FormattingToolbarEditorOverrides = {},
50): FormattingToolbarEditor {
51 return {
52 state,
53 view: { dragging: overrides.dragging ?? null },
54 isActive: overrides.isActive ?? (() => false),
55 } as FormattingToolbarEditor;
56}