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

composer-list-continuation.test.ts

93 lines3.7 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { listContinuationForEnter, nextOrderedMarkerText } from "./composer-list-continuation";
4​
5function applyEdit(value: string, edit: { start: number; end: number; replacement: string }) {
6 return value.slice(0, edit.start) + edit.replacement + value.slice(edit.end);
7}
8​
9describe("composer list continuation", () => {
10 it("continues bullets and keeps indentation", () => {
11 expect(applyEdit("- foo", listContinuationForEnter("- foo", 5)!)).toBe("- foo\n- ");
12 expect(applyEdit(" * foo", listContinuationForEnter(" * foo", 7)!)).toBe(" * foo\n * ");
13 });
14​
15 it("continues tasks unchecked", () => {
16 expect(applyEdit("- [x] done", listContinuationForEnter("- [x] done", 10)!)).toBe(
17 "- [x] done\n- [ ] ",
18 );
19 });
20​
21 it("splits mid-line items", () => {
22 expect(applyEdit("1. foobar", listContinuationForEnter("1. foobar", 5)!)).toBe(
23 "1. fo\n2. obar",
24 );
25 });
26​
27 it("exits the list on an empty item", () => {
28 expect(applyEdit("1. foo\n2. ", listContinuationForEnter("1. foo\n2. ", 10)!)).toBe("1. foo\n");
29 expect(applyEdit("- ", listContinuationForEnter("- ", 2)!)).toBe("");
30 expect(applyEdit("1)\t", listContinuationForEnter("1)\t", 3)!)).toBe("");
31 expect(applyEdit("- [ ] ", listContinuationForEnter("- [ ] ", 6)!)).toBe("");
32 });
33​
34 it("refuses to split a supplementary currency skill chip", () => {
35 const value = "1. 𑿝review go";
36 const cursor = value.indexOf(" go") - 1;
37 expect(listContinuationForEnter(value, cursor)).toBeNull();
38 });
39​
40 it("refuses to split an inline chip", () => {
41 const value = "1. @README.md go";
42 const cursor = value.indexOf("README") + 2;
43 expect(listContinuationForEnter(value, cursor)).toBeNull();
44 });
45});
46​
47describe("nextOrderedMarkerText", () => {
48 it.each([
49 ["1.", "2."],
50 ["1)", "2)"],
51 ["09.", "10."],
52 ["001)", "002)"],
53 ["99.", "100."],
54 ["99999999999999999999.", "99999999999999999999."],
55 ])("counts %s up to %s", (marker, expected) => {
56 expect(nextOrderedMarkerText(marker)).toBe(expected);
57 });
58});
59​
60describe("composer list renumbering", () => {
61 it("renumbers following items with their padding and leaves the caret after the inserted marker", () => {
62 const value = "1. @README.md go\n2. two\n03. three\n10. four";
63 const edit = listContinuationForEnter(value, value.indexOf("go"))!;
64 expect(applyEdit(value, edit)).toBe("1. @README.md \n2. go\n3. two\n04. three\n5. four");
65 expect(edit.cursorAfter).toBe("1. @README.md \n2. ".length);
66 });
67​
68 it("renumbers indented paren siblings across tab-indented children and stops at a parent", () => {
69 const value = " 1) a\n\t1) child\n 2) b\n1) parent\n 3) untouched";
70 const edit = listContinuationForEnter(value, value.indexOf("\n"))!;
71 expect(applyEdit(value, edit)).toBe(
72 " 1) a\n 2) \n\t1) child\n 3) b\n1) parent\n 3) untouched",
73 );
74 expect(edit.cursorAfter).toBe(" 1) a\n 2) ".length);
75 });
76​
77 // regression: #14332 continuing a numbered list must renumber following items only up to the list boundary
78 it.each([
79 ["a blank line", ""],
80 ["plain text", "text"],
81 ["a different delimiter", "1) other list"],
82 ])("stops renumbering at %s", (_label, boundary) => {
83 const value = `1. a\n2. b\n${boundary}\n3. untouched`;
84 const edit = listContinuationForEnter(value, value.indexOf("\n"))!;
85 expect(applyEdit(value, edit)).toBe(`1. a\n2. \n3. b\n${boundary}\n3. untouched`);
86 });
87​
88 it("preserves multiline chip contents while renumbering surrounding items", () => {
89 const value = '1. a\n2. @"file\n2. name" \n3. b';
90 const edit = listContinuationForEnter(value, value.indexOf("\n"))!;
91 expect(applyEdit(value, edit)).toBe('1. a\n2. \n3. @"file\n2. name" \n4. b');
92 });
93});