Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import {
4 listContinuationForEnter,
5 listIndentForTab,
6 nextOrderedMarkerText,
7} from "./composer-list-continuation";
8
9function applyEdit(value: string, edit: { start: number; end: number; replacement: string }) {
10 return value.slice(0, edit.start) + edit.replacement + value.slice(edit.end);
11}
12
13describe("composer list continuation", () => {
14 it("continues bullets and keeps indentation", () => {
15 expect(applyEdit("- foo", listContinuationForEnter("- foo", 5)!)).toBe("- foo\n- ");
16 expect(applyEdit(" * foo", listContinuationForEnter(" * foo", 7)!)).toBe(" * foo\n * ");
17 });
18
19 it("continues tasks unchecked", () => {
20 expect(applyEdit("- [x] done", listContinuationForEnter("- [x] done", 10)!)).toBe(
21 "- [x] done\n- [ ] ",
22 );
23 });
24
25 it("splits mid-line items", () => {
26 expect(applyEdit("1. foobar", listContinuationForEnter("1. foobar", 5)!)).toBe(
27 "1. fo\n2. obar",
28 );
29 });
30
31 it("exits the list on an empty item", () => {
32 expect(applyEdit("1. foo\n2. ", listContinuationForEnter("1. foo\n2. ", 10)!)).toBe("1. foo\n");
33 expect(applyEdit("- ", listContinuationForEnter("- ", 2)!)).toBe("");
34 expect(applyEdit("1)\t", listContinuationForEnter("1)\t", 3)!)).toBe("");
35 expect(applyEdit("- [ ] ", listContinuationForEnter("- [ ] ", 6)!)).toBe("");
36 });
37
38 it("lifts an empty nested item one level per press before leaving the list", () => {
39 let value = "1. parent\n 1. child\n 1. ";
40 for (const expected of [
41 "1. parent\n 1. child\n 2. ",
42 "1. parent\n 1. child\n2. ",
43 "1. parent\n 1. child\n",
44 ]) {
45 value = applyEdit(value, listContinuationForEnter(value, value.length)!);
46 expect(value).toBe(expected);
47 }
48 });
49
50 it("renumbers the parent list after lifting an empty nested item", () => {
51 const value = "1. a\n 1. \n2. b";
52 const edit = listContinuationForEnter(value, value.indexOf("\n2."))!;
53 expect(applyEdit(value, edit)).toBe("1. a\n2. \n3. b");
54 expect(edit.cursorAfter).toBe("1. a\n2. ".length);
55 });
56
57 it("refuses to split a supplementary currency skill chip", () => {
58 const value = "1. 𑿝review go";
59 const cursor = value.indexOf(" go") - 1;
60 expect(listContinuationForEnter(value, cursor)).toBeNull();
61 });
62
63 it("refuses to split an inline chip", () => {
64 const value = "1. @README.md go";
65 const cursor = value.indexOf("README") + 2;
66 expect(listContinuationForEnter(value, cursor)).toBeNull();
67 });
68});
69
70describe("composer list indentation", () => {
71 // regression: #17124 Tab must nest a numbered item at its parent's content column
72 it.each([
73 ["1. parent\n2. child", "1. parent\n 1. child"],
74 ["10. parent\n11. child", "10. parent\n 1. child"],
75 ["1.\tparent\n2. child", "1.\tparent\n 1. child"],
76 ['1. @"file\n 9. name"\n2. child', '1. @"file\n 9. name"\n 1. child'],
77 ])("nests %j as a first child and keeps the caret on its text", (value, expected) => {
78 const cursor = value.lastIndexOf("child") + 2;
79 const edit = listIndentForTab(value, cursor, cursor)!;
80 expect(applyEdit(value, edit)).toBe(expected);
81 expect(edit.cursorAfter).toBe(expected.lastIndexOf("child") + 2);
82 });
83
84 it("continues the numbering of an existing nested sibling", () => {
85 const value = "1. parent\n 1. first\n2. second";
86 const edit = listIndentForTab(value, value.length, value.length)!;
87 expect(applyEdit(value, edit)).toBe("1. parent\n 1. first\n 2. second");
88 });
89});
90
91describe("nextOrderedMarkerText", () => {
92 it.each([
93 ["1.", "2."],
94 ["1)", "2)"],
95 ["09.", "10."],
96 ["001)", "002)"],
97 ["99.", "100."],
98 ["99999999999999999999.", "99999999999999999999."],
99 ])("counts %s up to %s", (marker, expected) => {
100 expect(nextOrderedMarkerText(marker)).toBe(expected);
101 });
102});
103
104describe("composer list renumbering", () => {
105 it("renumbers following items with their padding and leaves the caret after the inserted marker", () => {
106 const value = "1. @README.md go\n2. two\n03. three\n10. four";
107 const edit = listContinuationForEnter(value, value.indexOf("go"))!;
108 expect(applyEdit(value, edit)).toBe("1. @README.md \n2. go\n3. two\n04. three\n5. four");
109 expect(edit.cursorAfter).toBe("1. @README.md \n2. ".length);
110 });
111
112 it("renumbers indented paren siblings across tab-indented children and stops at a parent", () => {
113 const value = " 1) a\n\t1) child\n 2) b\n1) parent\n 3) untouched";
114 const edit = listContinuationForEnter(value, value.indexOf("\n"))!;
115 expect(applyEdit(value, edit)).toBe(
116 " 1) a\n 2) \n\t1) child\n 3) b\n1) parent\n 3) untouched",
117 );
118 expect(edit.cursorAfter).toBe(" 1) a\n 2) ".length);
119 });
120
121 // regression: #14332 continuing a numbered list must renumber following items only up to the list boundary
122 it.each([
123 ["a blank line", ""],
124 ["plain text", "text"],
125 ["a different delimiter", "1) other list"],
126 ])("stops renumbering at %s", (_label, boundary) => {
127 const value = `1. a\n2. b\n${boundary}\n3. untouched`;
128 const edit = listContinuationForEnter(value, value.indexOf("\n"))!;
129 expect(applyEdit(value, edit)).toBe(`1. a\n2. \n3. b\n${boundary}\n3. untouched`);
130 });
131
132 it("preserves multiline chip contents while renumbering surrounding items", () => {
133 const value = '1. a\n2. @"file\n2. name" \n3. b';
134 const edit = listContinuationForEnter(value, value.indexOf("\n"))!;
135 expect(applyEdit(value, edit)).toBe('1. a\n2. \n3. @"file\n2. name" \n4. b');
136 });
137});