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

composer-list-split.browser.test.ts

123 lines4.5 KB

1import { Editor } from "@tiptap/core";
2import { TaskList } from "@tiptap/extension-task-list";
3import { TextSelection } from "@tiptap/pm/state";
4import StarterKit from "@tiptap/starter-kit";
5import { describe, expect, it } from "vite-plus/test";
6​
7import {
8 buildDocJson,
9 buildTiptapContent,
10 ComposerBlockExtensions,
11 ComposerCodeBlockExtension,
12 ComposerListExtensions,
13 ComposerTaskItemExtension,
14 serializeEditorDoc,
15 splitOrLiftListItem,
16} from "./composer-rich-text-doc";
17​
18// ProseMirror's DOM observer can flush on a timer after a test ends, which
19// throws once its DOM is gone, so every editor is acquired with `using`.
20function makeEditor(value: string): Editor & Disposable {
21 const editor = new Editor({
22 extensions: [
23 StarterKit.configure({
24 bulletList: false,
25 orderedList: false,
26 listItem: false,
27 codeBlock: false,
28 blockquote: false,
29 heading: false,
30 horizontalRule: false,
31 trailingNode: false,
32 listKeymap: false,
33 }),
34 ...ComposerListExtensions,
35 TaskList,
36 ComposerTaskItemExtension,
37 ComposerCodeBlockExtension,
38 ...ComposerBlockExtensions,
39 ],
40 content: buildDocJson(value, (name) => ({ label: name, description: null })),
41 });
42 return Object.assign(editor, { [Symbol.dispose]: () => editor.destroy() });
44​
45function placeCaret(editor: Editor, text: string, offset = 0) {
46 let caret = -1;
47 editor.state.doc.descendants((node, pos) => {
48 if (caret < 0 && node.isText && node.text!.includes(text)) {
49 caret = pos + node.text!.indexOf(text) + offset;
50 }
51 });
52 editor.view.dispatch(editor.state.tr.setSelection(TextSelection.create(editor.state.doc, caret)));
54​
55function storedDraft(editor: Editor) {
56 const stored = serializeEditorDoc(editor.state.doc).value;
57 using reloaded = makeEditor(stored);
58 expect(reloaded.state.doc.toString()).toBe(editor.state.doc.toString());
59 return stored;
61​
62describe("Backspace at the start of a line", () => {
63 it.each([
64 ["- a\n```\nl1\nl2\n```", "- a\n\nl1\nl2"],
65 ["p\n```ts\nl1\n```", "p\nl1"],
66 ])("removes the fence of %j rather than joining its code up", (value, expected) => {
67 using editor = makeEditor(value);
68 placeCaret(editor, "l1");
69 editor.commands.keyboardShortcut("Backspace");
70 expect(storedDraft(editor)).toBe(expected);
71 });
72​
73 // Neither item has a list of its own kind one level up to lift into.
74 it.each([
75 ["- a\n - [ ] x\n - [ ] y", "- a\n - [ ] xy"],
76 ["- [ ] a\n - x\n - y", "- [ ] a\n - xy"],
77 ["- a\n - [ ] y\n - [ ] q", "- ay\n - [ ] q"],
78 ])("joins y in %j onto the line above", (value, expected) => {
79 using editor = makeEditor(value);
80 placeCaret(editor, "y");
81 editor.commands.keyboardShortcut("Backspace");
82 expect(storedDraft(editor)).toBe(expected);
83 });
84​
85 it("removes an empty checkbox under a bullet", () => {
86 using editor = makeEditor("- a\n - [ ] x\n - [ ]");
87 editor.view.dispatch(editor.state.tr.setSelection(TextSelection.atEnd(editor.state.doc)));
88 editor.commands.keyboardShortcut("Backspace");
89 expect(storedDraft(editor)).toBe("- a\n - [ ] x");
90 });
91});
92​
93describe("blocks that a list item or quote cannot hold", () => {
94 it("puts lines pasted at the end of an item after the list", () => {
95 using editor = makeEditor("- a");
96 editor.view.dispatch(editor.state.tr.setSelection(TextSelection.atEnd(editor.state.doc)));
97 const lines = buildTiptapContent("one\ntwo", (name) => ({ label: name, description: null }));
98 editor.commands.insertContent(lines);
99 expect(storedDraft(editor)).toBe("- a\n\none\ntwo");
100 });
101});
102​
103describe("Enter in a list item", () => {
104 it.each([
105 ["* [x] done", "* [x] done\n* [ ] "],
106 ["1. [x] done", "1. [x] done\n2. [ ] "],
107 ["* [ ] ", ""],
108 ["1. [x] a\n2. [ ] ", "1. [x] a\n"],
109 ])("continues or leaves the task list from %j", (value, expected) => {
110 using editor = makeEditor(value);
111 editor.view.dispatch(editor.state.tr.setSelection(TextSelection.atEnd(editor.state.doc)));
112 expect(splitOrLiftListItem(editor, () => true)).toBe(true);
113 expect(storedDraft(editor)).toBe(expected);
114 });
115​
116 it("keeps the new item's marker within Markdown's nine digits", () => {
117 using editor = makeEditor("999999999. first");
118 editor.view.dispatch(editor.state.tr.setSelection(TextSelection.atEnd(editor.state.doc)));
119 expect(splitOrLiftListItem(editor, () => true)).toBe(true);
120 editor.commands.insertContent("second");
121 expect(storedDraft(editor)).toBe("999999999. first\n999999999. second");
122 });
123});