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

markdownTaskList.test.ts

97 lines4.6 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { markdownTaskMarkerOffset } from "./markdownTaskList.ts";
4import { setMarkdownTaskChecked, taskListMarkerOffset } from "./markdownTaskEdits.ts";
5​
6describe("taskListMarkerOffset", () => {
7 it("finds the marker on the line after a bare bullet", () => {
8 expect(taskListMarkerOffset("-\n [ ] Task", 0)).toBe(4);
9 expect(taskListMarkerOffset("-\r\n [ ] Task", 0)).toBe(5);
10 expect(taskListMarkerOffset("-\r [ ] Task", 0)).toBe(4);
11 expect(taskListMarkerOffset("> > 1.\n> > [ ] Task", 4)).toBe(14);
12 expect(taskListMarkerOffset("- First\n- Second", 0)).toBeNull();
13 });
14});
15​
16describe("setMarkdownTaskChecked", () => {
17 const markdown = "- [ ] First\n- [x] Second\n";
18​
19 it("checks and unchecks the task marker at the supplied offset", () => {
20 expect(setMarkdownTaskChecked(markdown, 2, true)).toBe("- [x] First\n- [x] Second\n");
21 expect(setMarkdownTaskChecked(markdown, 14, false)).toBe("- [ ] First\n- [ ] Second\n");
22 expect(setMarkdownTaskChecked("1. [X] Ordered\n", 3, false)).toBe("1. [ ] Ordered\n");
23 });
24​
25 it("leaves the document unchanged for a stale or invalid marker offset", () => {
26 expect(setMarkdownTaskChecked(markdown, 0, true)).toBe(markdown);
27 expect(setMarkdownTaskChecked(markdown, 200, true)).toBe(markdown);
28 });
29});
30​
31describe("markdownTaskMarkerOffset", () => {
32 const markdown = [
33 "## Test plan",
34 "- [ ] `vp lint` passes",
35 " - [x] Nested **step**",
36 "```md",
37 "- [ ] Inside a fence",
38 "```",
39 "> 1. [X] Quoted [link](https://example.com)",
40 "- [ ] Wrapped",
41 " onto two lines",
42 ].join("\n");
43 const marker = (offset: number | null) =>
44 offset === null ? null : markdown.slice(offset, offset + 3);
45​
46 it("finds the tapped checkbox, skipping look-alikes in code", () => {
47 expect(marker(markdownTaskMarkerOffset(markdown, 0, "vp lint passes", true))).toBe("[ ]");
48 expect(marker(markdownTaskMarkerOffset(markdown, 1, "Nested step", false))).toBe("[x]");
49 expect(marker(markdownTaskMarkerOffset(markdown, 2, "Quoted link", false))).toBe("[X]");
50 // Android cuts the text at a line break, a soft one too with hard line breaks; iOS doesn't.
51 for (const text of ["Wrapped", "Wrapped onto two lines"]) {
52 expect(markdownTaskMarkerOffset(markdown, 3, text, true)).toBe(markdown.lastIndexOf("[ ]"));
53 }
54 expect(markdownTaskMarkerOffset("- [ ]\n Task", 0, "Task", true)).toBe(2);
55 });
56​
57 it("skips next-line markers that the native renderer does not render as checkboxes", () => {
58 const body = "-\n [ ] Same\n\n```txt\nseparator\n```\n\n- [ ] Same";
59 expect(markdownTaskMarkerOffset(body, 0, "Same", true)).toBe(body.lastIndexOf("[ ]"));
60 expect(markdownTaskMarkerOffset("> -\n> [ ] Task", 0, "Task", true)).toBeNull();
61 });
62​
63 it("counts empty task items before the tapped checkbox", () => {
64 for (const empty of ["- [ ]", "- [x]", "1. [X]\r"]) {
65 const body = `${empty}\n\n- [ ] Same`;
66 const offset = markdownTaskMarkerOffset(body, 1, "Same", true);
67 expect(offset).toBe(body.lastIndexOf("[ ]"));
68 expect(setMarkdownTaskChecked(body, offset ?? -1, true)).toBe(`${empty}\n\n- [x] Same`);
69 expect(markdownTaskMarkerOffset(body, 1, "Other", true)).toBeNull();
70 expect(markdownTaskMarkerOffset(body, 1, "Same", false)).toBeNull();
71 }
72 });
73​
74 it("tells identical labels apart by document index, across code blocks", () => {
75 const separated = "- [ ] Same\n\n```txt\nx\n```\n\n- [ ] Same";
76 expect([0, 1].map((index) => markdownTaskMarkerOffset(separated, index, "Same", true))).toEqual(
77 [2, 28],
78 );
79 const same = "- [ ] Same\n- [ ] Same\n\n```\nx\n```\n\n- [x] Same\n";
80 expect(markdownTaskMarkerOffset(same, 0, "Same", true)).toBe(2);
81 expect(markdownTaskMarkerOffset(same, 1, "Same", true)).toBe(13);
82 expect(markdownTaskMarkerOffset(same, 2, "Same", false)).toBe(same.lastIndexOf("[x]"));
83 });
84​
85 it("refuses a tap whose index, state or text does not match", () => {
86 expect(markdownTaskMarkerOffset(markdown, 1, "vp lint passes", true)).toBeNull();
87 expect(markdownTaskMarkerOffset(markdown, 0, "vp lint passes", false)).toBeNull();
88 expect(markdownTaskMarkerOffset(markdown, 0, "Wrapped onto", true)).toBeNull();
89 expect(markdownTaskMarkerOffset(markdown, 4, "", true)).toBeNull();
90​
91 const similar = "- [ ] Similar\n\n```text\nseparator\n```\n\n- [ ] Similar<br>different\n";
92 for (const label of ["Similar [docs](https://github.com/pingdotgg/t3code)", "Similar $x$"]) {
93 const attachment = similar.replace("Similar<br>different", label);
94 expect(markdownTaskMarkerOffset(attachment, 1, "Similar \uFFFC", true)).toBeNull();
95 }
96 });
97});