-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor.test.ts
More file actions
233 lines (189 loc) · 6.14 KB
/
editor.test.ts
File metadata and controls
233 lines (189 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import { describe, expect, it, vi } from "vitest";
import { DOCUMENT_SURFACE_CLASS } from "./src/core/document-surface-classes";
import { mountEditor } from "./editor";
describe("mountEditor", () => {
it("mounts with an empty document by default", () => {
const parent = document.createElement("div");
const editor = mountEditor({ parent });
expect(editor.getDoc()).toBe("");
expect(editor.getMode()).toBe("rich");
const editorElement = parent.querySelector(".cm-editor");
expect(editorElement).toBeTruthy();
expect(editorElement?.classList.contains(DOCUMENT_SURFACE_CLASS.surface)).toBe(true);
const contentElement = parent.querySelector(".cm-content");
expect(contentElement?.classList.contains(DOCUMENT_SURFACE_CLASS.flow)).toBe(true);
editor.unmount();
});
it("applies the requested initial source mode without firing mode callbacks", () => {
const onModeChange = vi.fn();
const parent = document.createElement("div");
const editor = mountEditor({
parent,
doc: "# Title",
mode: "source",
onModeChange,
});
expect(editor.getMode()).toBe("source");
expect(onModeChange).not.toHaveBeenCalled();
editor.unmount();
});
it("setDoc replaces fenced content without triggering onChange", () => {
const onChange = vi.fn();
const parent = document.createElement("div");
const editor = mountEditor({
parent,
doc: "::: {.theorem}\nBody\n:::",
onChange,
});
editor.setDoc("# Replaced");
expect(editor.getDoc()).toBe("# Replaced");
expect(onChange).not.toHaveBeenCalled();
editor.unmount();
});
it("reports programmatic mode changes through onModeChange", () => {
const onModeChange = vi.fn();
const parent = document.createElement("div");
const editor = mountEditor({
parent,
onModeChange,
});
editor.setMode("source");
editor.setMode("rich");
expect(onModeChange.mock.calls).toEqual([["source"], ["rich"]]);
editor.unmount();
});
it("exposes synchronous headless outline data after mount", () => {
const parent = document.createElement("div");
const editor = mountEditor({
parent,
doc: "# Intro {#sec:intro}\n\nBody\n\n## Lemma\nText",
});
expect(editor.outline.get()).toEqual([
{
level: 1,
text: "Intro",
line: 1,
from: 0,
key: "0",
id: "sec:intro",
number: "1",
},
{
level: 2,
text: "Lemma",
line: 5,
from: 28,
key: "28",
id: undefined,
number: "1.1",
},
]);
editor.unmount();
});
it("publishes headless outline updates without rendering panel DOM", () => {
const parent = document.createElement("div");
const editor = mountEditor({
parent,
doc: "# Intro",
});
const onOutline = vi.fn();
const unsubscribe = editor.outline.subscribe(onOutline);
editor.setDoc("# Intro\n\n## Details");
expect(onOutline).toHaveBeenCalledTimes(1);
expect(onOutline.mock.calls[0][0].map((entry) => entry.text)).toEqual([
"Intro",
"Details",
]);
expect(parent.querySelector(".cf-outline")).toBeNull();
unsubscribe();
editor.setDoc("# Intro\n\n## Details\n\n### Tail");
expect(onOutline).toHaveBeenCalledTimes(1);
editor.unmount();
});
it("exposes counts for the markdown body", () => {
const parent = document.createElement("div");
const editor = mountEditor({
parent,
doc: "---\ntitle: Draft\n---\nOne two.\n\nThree.",
});
expect(editor.counts.get()).toEqual({
words: 3,
chars: "One two.\n\nThree.".length,
paragraphs: 2,
});
editor.unmount();
});
it("updates cursor context on line-oriented navigation", () => {
const parent = document.createElement("div");
const editor = mountEditor({
parent,
doc: "# Chapter\n\n## Section\nBody text",
});
const onCursorContext = vi.fn();
const unsubscribe = editor.cursorContext.subscribe(onCursorContext);
editor.scrollToLine(4, { column: 6 });
expect(editor.cursorContext.get()).toEqual({
line: 4,
column: 6,
from: 27,
currentHeadingPath: ["Chapter", "Section"],
});
expect(onCursorContext).toHaveBeenCalledWith(editor.cursorContext.get());
unsubscribe();
editor.unmount();
});
it("clears headless subscribers on unmount", () => {
const parent = document.createElement("div");
const editor = mountEditor({
parent,
doc: "# Intro",
});
const onCounts = vi.fn();
editor.counts.subscribe(onCounts);
editor.unmount();
editor.setDoc("# Changed");
expect(onCounts).not.toHaveBeenCalled();
});
it("updates DocumentContext after mount without remounting document state", () => {
const parent = document.createElement("div");
const editor = mountEditor({
parent,
doc: "See [@host-page].",
mode: "rich",
});
expect(editor.getDoc()).toBe("See [@host-page].");
expect(parent.textContent).not.toContain("Resolved Page");
editor.setContext({
refResolver: {
resolve: (key) => ({ content: `<strong>Resolved ${key}</strong>` }),
},
});
expect(editor.getDoc()).toBe("See [@host-page].");
expect(editor.getMode()).toBe("rich");
expect(parent.textContent).toContain("Resolved host-page");
editor.unmount();
});
it("includes hover preview support for standalone mounts", () => {
vi.useFakeTimers();
const parent = document.createElement("div");
const editor = mountEditor({
parent,
doc: "$$x$$ {#eq:one}\n\nSee [@eq:one].",
mode: "rich",
});
try {
const reference = parent.querySelector<HTMLElement>("[data-reference-widget]");
expect(reference?.textContent).toBe("Eq. (1)");
reference?.dispatchEvent(new MouseEvent("mouseover", {
bubbles: true,
relatedTarget: null,
}));
vi.advanceTimersByTime(350);
expect(document.querySelector(".cf-hover-preview-tooltip")).toBeTruthy();
} finally {
editor.unmount();
document.querySelector(".cf-hover-preview-tooltip")?.remove();
vi.useRealTimers();
}
});
});