Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

composer-list-split.test.ts

248 lines8.8 KB

1// @vitest-environment jsdom
2​
3import { Editor } from "@tiptap/core";
4import { TaskList } from "@tiptap/extension-task-list";
5import { TextSelection } from "@tiptap/pm/state";
6import StarterKit from "@tiptap/starter-kit";
7import { afterEach, describe, expect, it } from "vite-plus/test";
8​
9import {
10 backspaceAcrossList,
11 buildDocJson,
12 buildTiptapContent,
13 ComposerBlockExtensions,
14 ComposerCodeBlockExtension,
15 ComposerListExtensions,
16 ComposerTaskItemExtension,
17 convertBulletItemToTask,
18 deleteAcrossList,
19 serializeEditorDoc,
20 splitOrLiftListItem,
21} from "./composer-rich-text-doc";
22​
23// ProseMirror's DOM observer can flush on a timer after a test ends, which
24// throws once jsdom is torn down, so every editor is destroyed after its test.
25const editors: Editor[] = [];
26afterEach(() => {
27 for (const editor of editors.splice(0)) editor.destroy();
28});
29​
30function makeEditor(value: string) {
31 const editor = new Editor({
32 extensions: [
33 StarterKit.configure({
34 bulletList: false,
35 orderedList: false,
36 listItem: false,
37 codeBlock: false,
38 blockquote: false,
39 heading: false,
40 horizontalRule: false,
41 trailingNode: false,
42 listKeymap: false,
43 }),
44 ...ComposerListExtensions,
45 TaskList,
46 ComposerTaskItemExtension,
47 ComposerCodeBlockExtension,
48 ...ComposerBlockExtensions,
49 ],
50 content: buildDocJson(value, (name) => ({ label: name, description: null })),
51 });
52 editors.push(editor);
53 return editor;
55​
56function splitLastItem(value: string, type: "listItem" | "taskItem", overrides: object) {
57 const editor = makeEditor(value);
58 editor.view.dispatch(editor.state.tr.setSelection(TextSelection.atEnd(editor.state.doc)));
59 editor.commands.splitListItem(type, overrides);
60 editor.view.dispatch(editor.state.tr.insertText("b"));
61 return serializeEditorDoc(editor.state.doc).value;
63​
64describe("splitting a list item", () => {
65 it.each([
66 ["* a", { space: " " }, "* a\n* b"],
67 ["+ a", { space: " " }, "+ a\n+ b"],
68 ["- p\n - a", { space: " " }, "- p\n - a\n - b"],
69 ["3) a", { marker: "4)", space: " " }, "3) a\n4) b"],
70 ])("keeps the source marker and indent of %s", (value, overrides, expected) => {
71 expect(splitLastItem(value, "listItem", overrides)).toBe(expected);
72 });
73​
74 it("keeps the indent of a nested task", () => {
75 expect(splitLastItem("- [ ] p\n - [ ] a", "taskItem", { checked: false })).toBe(
76 "- [ ] p\n - [ ] a\n - [ ] b",
77 );
78 });
79});
80​
81function placeCaret(editor: Editor, text: string, offset = 0) {
82 let caret = -1;
83 editor.state.doc.descendants((node, pos) => {
84 if (caret < 0 && node.isText && node.text!.includes(text)) {
85 caret = pos + node.text!.indexOf(text) + offset;
86 }
87 });
88 editor.view.dispatch(editor.state.tr.setSelection(TextSelection.create(editor.state.doc, caret)));
90​
91function storedDraft(editor: Editor) {
92 const stored = serializeEditorDoc(editor.state.doc).value;
93 expect(makeEditor(stored).state.doc.toString()).toBe(editor.state.doc.toString());
94 return stored;
96​
97describe("Shift+Enter twice on an item", () => {
98 it.each([
99 ["- a\n - x", "- a\n - x\n- b"],
100 ["* a\n * x", "* a\n * x\n* b"],
101 ["1. a\n 1. x", "1. a\n 1. x\n2. b"],
102 ["- a\n - x\n- c", "- a\n - x\n- b\n- c"],
103 ["- a\n - x\n - y", "- a\n - x\n- b\n - y"],
104 ["- [ ] a\n - [ ] x", "- [ ] a\n - [ ] x\n- [ ] b"],
105 ["- [x] a\n - [ ] x\n- [ ] c", "- [x] a\n - [ ] x\n- [ ] b\n- [ ] c"],
106 ])("after x in %j writes %j", (value, expected) => {
107 const editor = makeEditor(value);
108 let caret = -1;
109 editor.state.doc.descendants((node, pos) => {
110 if (node.isText && node.text === "x") caret = pos + 1;
111 });
112 editor.view.dispatch(
113 editor.state.tr.setSelection(TextSelection.create(editor.state.doc, caret)),
114 );
115 expect(splitOrLiftListItem(editor)).toBe(true);
116 expect(splitOrLiftListItem(editor)).toBe(true);
117 editor.view.dispatch(editor.state.tr.insertText("b"));
118 const stored = serializeEditorDoc(editor.state.doc).value;
119 expect(stored).toBe(expected);
120 expect(makeEditor(stored).state.doc.toString()).toBe(editor.state.doc.toString());
121 });
122});
123​
124describe("Shift+Enter in the middle of an ordered list", () => {
125 it.each([
126 ["1. a\n2. x\n3. c\n4. d", "1. a\n2. x\n3. b\n4. c\n5. d"],
127 ["1) a\n2) x\n3) c", "1) a\n2) x\n3) b\n4) c"],
128 ["1. a\n 1. x\n 2. c\n2. d", "1. a\n 1. x\n 2. b\n 3. c\n2. d"],
129 ])("after x in %j writes %j", (value, expected) => {
130 const editor = makeEditor(value);
131 placeCaret(editor, "x", 1);
132 expect(splitOrLiftListItem(editor)).toBe(true);
133 editor.view.dispatch(editor.state.tr.insertText("b"));
134 expect(storedDraft(editor)).toBe(expected);
135 });
136});
137​
138describe("Shift+Enter on an empty task under a bullet", () => {
139 it.each(["- a\n - [ ] x", "- [ ] a\n - x"])("in %j keeps the draft honest", (value) => {
140 const editor = makeEditor(value);
141 placeCaret(editor, "x", 1);
142 splitOrLiftListItem(editor);
143 splitOrLiftListItem(editor);
144 storedDraft(editor);
145 });
146});
147​
148describe("Backspace at the start of a line", () => {
149 it.each([
150 ["- a\n - x", "x", "- a\n- x"],
151 ["- a\n - x\n - y", "x", "- a\n- x\n - y"],
152 ["- [ ] a\n - [ ] x", "x", "- [ ] a\n- [ ] x"],
153 ["- a\n- b\n- c", "b", "- a\nb\n- c"],
154 ["- a\n - [ ] x", "x", "- ax"],
155 ["- a\n - x\nb", "b", "- a\n - xb"],
156 ["1. a\nb", "b", "1. ab"],
157 ])("in %j at %s writes %j", (value, at, expected) => {
158 const editor = makeEditor(value);
159 placeCaret(editor, at);
160 expect(backspaceAcrossList(editor)).toBe(true);
161 expect(storedDraft(editor)).toBe(expected);
162 });
163​
164 it.each([
165 ["- a\n```\nl1\nl2\n```", "- a\nl1\nl2"],
166 ["p\n```ts\nl1\n```", "p\nl1"],
167 ])("removes the fence of %j rather than joining its code up", (value, expected) => {
168 const editor = makeEditor(value);
169 placeCaret(editor, "l1");
170 editor.commands.keyboardShortcut("Backspace");
171 expect(storedDraft(editor)).toBe(expected);
172 });
173});
174​
175describe("Delete at the end of a line", () => {
176 it.each([
177 ["- a\n- b", "- ab"],
178 ["- a\n - x\n- c", "- ax\n- c"],
179 ["- a\n\nb", "- a\nb"],
180 ])("in %j writes %j", (value, expected) => {
181 const editor = makeEditor(value);
182 placeCaret(editor, "a", 1);
183 expect(deleteAcrossList(editor)).toBe(true);
184 expect(storedDraft(editor)).toBe(expected);
185 });
186​
187 it.each(["- a\n```\nl1\n```", "a\n```\nl1\n```"])("leaves the fence in %j alone", (value) => {
188 const editor = makeEditor(value);
189 placeCaret(editor, "a", 1);
190 editor.commands.keyboardShortcut("Delete");
191 expect(storedDraft(editor)).toBe(value);
192 });
193});
194​
195describe("blocks that a list item or quote cannot hold", () => {
196 it("puts lines pasted at the end of an item after the list", () => {
197 const editor = makeEditor("- a");
198 editor.view.dispatch(editor.state.tr.setSelection(TextSelection.atEnd(editor.state.doc)));
199 const lines = buildTiptapContent("one\ntwo", (name) => ({ label: name, description: null }));
200 editor.commands.insertContent(lines);
201 expect(storedDraft(editor)).toBe("- a\none\ntwo");
202 });
203​
204 it("will not make a task list inside a quote", () => {
205 const editor = makeEditor("> q");
206 placeCaret(editor, "q");
207 expect(editor.commands.wrapInList("taskList")).toBe(false);
208 expect(storedDraft(editor)).toBe("> q");
209 });
210});
211​
212describe("[ ] typed at the start of a bullet item", () => {
213 it.each([
214 ["* [ ]x", "- [ ] x"],
215 ["* a\n* [ ]x\n* c", "* a\n- [ ] x\n* c"],
216 ["- a\n * [ ]x", "- a\n - [ ] x"],
217 ["- a\n - [ ]x\n - y", "- a\n - [ ] x\n - y"],
218 ["- [ ]x\n - y", "- [ ] x\n - y"],
219 ["- [ ]x", "- [ ] x"],
220 ])("makes the item in %j a task where it stands", (value, expected) => {
221 const editor = makeEditor(value);
222 placeCaret(editor, "[ ]x");
223 const from = editor.state.selection.from;
224 editor.view.dispatch(
225 editor.state.tr.setSelection(TextSelection.create(editor.state.doc, from + 3)),
226 );
227 const { tr } = editor.state;
228 convertBulletItemToTask(tr, from, from + 3, false);
229 editor.view.dispatch(tr);
230 expect(editor.state.selection.$from.parent.textContent).toBe("x");
231 expect(editor.state.selection.$from.parentOffset).toBe(0);
232 expect(storedDraft(editor)).toBe(expected);
233 });
234});
235​
236describe("a rule at the end of the draft", () => {
237 it("keeps an empty line after it to type on", () => {
238 const editor = makeEditor("a\n---");
239 editor.view.dispatch(
240 editor.state.tr.delete(editor.state.doc.content.size - 2, editor.state.doc.content.size),
241 );
242 expect(editor.state.doc.lastChild?.type.name).toBe("paragraph");
243 expect(storedDraft(editor)).toBe("a\n---");
244 editor.view.dispatch(editor.state.tr.setSelection(TextSelection.atEnd(editor.state.doc)));
245 editor.view.dispatch(editor.state.tr.insertText("b"));
246 expect(storedDraft(editor)).toBe("a\n---\nb");
247 });
248});