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

composer-list-continuation.test.ts

128 lines5.0 KB

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);
12​
13describe("composer list continuation", () => {
14 it("continues ordered lists with the next number", () => {
15 const value = "1. foo";
16 const edit = listContinuationForEnter(value, value.length);
17 expect(edit).not.toBeNull();
18 expect(applyEdit(value, edit!)).toBe("1. foo\n2. ");
19 });
20​
21 it("increments multi-digit and paren markers", () => {
22 expect(applyEdit("12. foo", listContinuationForEnter("12. foo", 7)!)).toBe("12. foo\n13. ");
23 expect(applyEdit("3) foo", listContinuationForEnter("3) foo", 6)!)).toBe("3) foo\n4) ");
24 });
25​
26 it("continues bullets and keeps indentation", () => {
27 expect(applyEdit("- foo", listContinuationForEnter("- foo", 5)!)).toBe("- foo\n- ");
28 expect(applyEdit(" * foo", listContinuationForEnter(" * foo", 7)!)).toBe(" * foo\n * ");
29 });
30​
31 it("continues tasks unchecked", () => {
32 expect(applyEdit("- [x] done", listContinuationForEnter("- [x] done", 10)!)).toBe(
33 "- [x] done\n- [ ] ",
34 );
35 });
36​
37 it("splits mid-line items", () => {
38 expect(applyEdit("1. foobar", listContinuationForEnter("1. foobar", 5)!)).toBe(
39 "1. fo\n2. obar",
40 );
41 });
42​
43 it("exits the list on an empty item", () => {
44 expect(applyEdit("1. foo\n2. ", listContinuationForEnter("1. foo\n2. ", 10)!)).toBe("1. foo\n");
45 expect(applyEdit("- ", listContinuationForEnter("- ", 2)!)).toBe("");
46 expect(applyEdit("1)\t", listContinuationForEnter("1)\t", 3)!)).toBe("");
47 expect(applyEdit("- [ ] ", listContinuationForEnter("- [ ] ", 6)!)).toBe("");
48 });
49​
50 it("keeps a marker typed without a trailing space", () => {
51 expect(listContinuationForEnter("asd\n2.", 6)).toBeNull();
52 expect(listContinuationForEnter("1)", 2)).toBeNull();
53 expect(listContinuationForEnter("-", 1)).toBeNull();
54 expect(listContinuationForEnter("- [ ]", 5)).toBeNull();
55 });
56​
57 it("ignores non-list lines and carets inside the marker", () => {
58 expect(listContinuationForEnter("plain text", 5)).toBeNull();
59 expect(listContinuationForEnter("1.foo no space", 3)).toBeNull();
60 expect(listContinuationForEnter("-foo", 2)).toBeNull();
61 expect(listContinuationForEnter("1. foo", 1)).toBeNull();
62 });
63​
64 it("refuses to split a supplementary currency skill chip", () => {
65 const value = "1. 𑿝review go";
66 const cursor = value.indexOf(" go") - 1;
67 expect(listContinuationForEnter(value, cursor)).toBeNull();
68 });
69​
70 it("refuses to split an inline chip", () => {
71 const value = "1. @README.md go";
72 const cursor = value.indexOf("README") + 2;
73 expect(listContinuationForEnter(value, cursor)).toBeNull();
74 });
75​
76 it("indents list items on Tab", () => {
77 expect(applyEdit("- foo", listIndentForTab("- foo", 2, 2)!)).toBe(" - foo");
78 expect(listIndentForTab("plain", 2, 2)).toBeNull();
79 expect(listIndentForTab("- foo", 1, 3)).toBeNull();
80 });
81});
82​
83describe("nextOrderedMarkerText", () => {
84 it.each([
85 ["1.", "2."],
86 ["1)", "2)"],
87 ["09.", "10."],
88 ["001)", "002)"],
89 ["99.", "100."],
90 ["99999999999999999999.", "99999999999999999999."],
91 ])("counts %s up to %s", (marker, expected) => {
92 expect(nextOrderedMarkerText(marker)).toBe(expected);
93 });
94});
95​
96describe("composer list renumbering", () => {
97 it("renumbers following items with their padding and leaves the caret after the inserted marker", () => {
98 const value = "1. @README.md go\n2. two\n03. three\n10. four";
99 const edit = listContinuationForEnter(value, value.indexOf("go"))!;
100 expect(applyEdit(value, edit)).toBe("1. @README.md \n2. go\n3. two\n04. three\n5. four");
101 expect(edit.cursorAfter).toBe("1. @README.md \n2. ".length);
102 });
103​
104 it("renumbers indented paren siblings across tab-indented children and stops at a parent", () => {
105 const value = " 1) a\n\t1) child\n 2) b\n1) parent\n 3) untouched";
106 const edit = listContinuationForEnter(value, value.indexOf("\n"))!;
107 expect(applyEdit(value, edit)).toBe(
108 " 1) a\n 2) \n\t1) child\n 3) b\n1) parent\n 3) untouched",
109 );
110 expect(edit.cursorAfter).toBe(" 1) a\n 2) ".length);
111 });
112​
113 it.each([
114 ["a blank line", ""],
115 ["plain text", "text"],
116 ["a different delimiter", "1) other list"],
117 ])("stops renumbering at %s", (_label, boundary) => {
118 const value = `1. a\n2. b\n${boundary}\n3. untouched`;
119 const edit = listContinuationForEnter(value, value.indexOf("\n"))!;
120 expect(applyEdit(value, edit)).toBe(`1. a\n2. \n3. b\n${boundary}\n3. untouched`);
121 });
122​
123 it("preserves multiline chip contents while renumbering surrounding items", () => {
124 const value = '1. a\n2. @"file\n2. name" \n3. b';
125 const edit = listContinuationForEnter(value, value.indexOf("\n"))!;
126 expect(applyEdit(value, edit)).toBe('1. a\n2. \n3. @"file\n2. name" \n4. b');
127 });
128});