Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import { listContinuationForEnter, listIndentForTab } 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 ordered lists with the next number", () => {
11 const value = "1. foo";
12 const edit = listContinuationForEnter(value, value.length);
13 expect(edit).not.toBeNull();
14 expect(applyEdit(value, edit!)).toBe("1. foo\n2. ");
15 });
16
17 it("increments multi-digit and paren markers", () => {
18 expect(applyEdit("12. foo", listContinuationForEnter("12. foo", 7)!)).toBe("12. foo\n13. ");
19 expect(applyEdit("3) foo", listContinuationForEnter("3) foo", 6)!)).toBe("3) foo\n4) ");
20 });
21
22 it("continues bullets and keeps indentation", () => {
23 expect(applyEdit("- foo", listContinuationForEnter("- foo", 5)!)).toBe("- foo\n- ");
24 expect(applyEdit(" * foo", listContinuationForEnter(" * foo", 7)!)).toBe(" * foo\n * ");
25 });
26
27 it("continues tasks unchecked", () => {
28 expect(applyEdit("- [x] done", listContinuationForEnter("- [x] done", 10)!)).toBe(
29 "- [x] done\n- [ ] ",
30 );
31 });
32
33 it("splits mid-line items", () => {
34 expect(applyEdit("1. foobar", listContinuationForEnter("1. foobar", 5)!)).toBe(
35 "1. fo\n2. obar",
36 );
37 });
38
39 it("exits the list on an empty item", () => {
40 expect(applyEdit("1. foo\n2. ", listContinuationForEnter("1. foo\n2. ", 10)!)).toBe("1. foo\n");
41 expect(applyEdit("- ", listContinuationForEnter("- ", 2)!)).toBe("");
42 });
43
44 it("ignores non-list lines and carets inside the marker", () => {
45 expect(listContinuationForEnter("plain text", 5)).toBeNull();
46 expect(listContinuationForEnter("1.foo no space", 3)).toBeNull();
47 expect(listContinuationForEnter("-foo", 2)).toBeNull();
48 expect(listContinuationForEnter("1. foo", 1)).toBeNull();
49 });
50
51 it("refuses to split a supplementary currency skill chip", () => {
52 const value = "1. 𑿝review go";
53 const cursor = value.indexOf(" go") - 1;
54 expect(listContinuationForEnter(value, cursor)).toBeNull();
55 });
56
57 it("refuses to split an inline chip", () => {
58 const value = "1. @README.md go";
59 const cursor = value.indexOf("README") + 2;
60 expect(listContinuationForEnter(value, cursor)).toBeNull();
61 });
62
63 it("indents list items on Tab", () => {
64 expect(applyEdit("- foo", listIndentForTab("- foo", 2, 2)!)).toBe(" - foo");
65 expect(listIndentForTab("plain", 2, 2)).toBeNull();
66 expect(listIndentForTab("- foo", 1, 3)).toBeNull();
67 });
68});