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

composer-list-continuation.test.ts

85 lines3.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 });
47​
48 it("ignores non-list lines and carets inside the marker", () => {
49 expect(listContinuationForEnter("plain text", 5)).toBeNull();
50 expect(listContinuationForEnter("1.foo no space", 3)).toBeNull();
51 expect(listContinuationForEnter("-foo", 2)).toBeNull();
52 expect(listContinuationForEnter("1. foo", 1)).toBeNull();
53 });
54​
55 it("refuses to split a supplementary currency skill chip", () => {
56 const value = "1. 𑿝review go";
57 const cursor = value.indexOf(" go") - 1;
58 expect(listContinuationForEnter(value, cursor)).toBeNull();
59 });
60​
61 it("refuses to split an inline chip", () => {
62 const value = "1. @README.md go";
63 const cursor = value.indexOf("README") + 2;
64 expect(listContinuationForEnter(value, cursor)).toBeNull();
65 });
66​
67 it("indents list items on Tab", () => {
68 expect(applyEdit("- foo", listIndentForTab("- foo", 2, 2)!)).toBe(" - foo");
69 expect(listIndentForTab("plain", 2, 2)).toBeNull();
70 expect(listIndentForTab("- foo", 1, 3)).toBeNull();
71 });
72});
73​
74describe("nextOrderedMarkerText", () => {
75 it.each([
76 ["1.", "2."],
77 ["1)", "2)"],
78 ["09.", "10."],
79 ["001)", "002)"],
80 ["99.", "100."],
81 ["99999999999999999999.", "99999999999999999999."],
82 ])("counts %s up to %s", (marker, expected) => {
83 expect(nextOrderedMarkerText(marker)).toBe(expected);
84 });
85});