Skip to content
Source
Sign inSign up
fuko/trytanstackdb.com
Open History to see the last commit

note-editing.test.ts

117 lines4.1 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { parseEditableBody, serializeBody } from "./note-body";
4import {
5 joinWithPrevious,
6 setBlockText,
7 setTitle,
8 splitBlock,
9 splitTitle,
10 toggleChecked,
11 toggleChecklist,
12} from "./note-editing";
13import type { NoteText } from "./note-editing";
14​
15const note = (title: string, body: string): NoteText => ({
16 title,
17 blocks: parseEditableBody(body),
18});
19const body = (edit: NoteText) => serializeBody(edit.blocks);
20​
21describe("editable body", () => {
22 it("round-trips the stored text, blank lines included", () => {
23 const text = "Intro\n\n# Goals\n- one\n[ ] todo\n[x] done\n";
24 expect(serializeBody(parseEditableBody(text))).toBe(text);
25 expect(parseEditableBody("")).toEqual([{ type: "paragraph", text: "" }]);
26 });
27});
28​
29describe("Enter", () => {
30 it("in the title moves the rest of it into the body", () => {
31 const edit = splitTitle(note("Groceries list", "Milk"), 9, 9);
32 expect(edit.title).toBe("Groceries");
33 expect(body(edit)).toBe(" list\nMilk");
34 expect(edit.caret).toEqual({ field: 0, offset: 0 });
35 });
36​
37 it("at the end of a new note's title starts the body", () => {
38 const edit = splitTitle(note("Hello", ""), 5, 5);
39 expect(edit.title).toBe("Hello");
40 expect(body(edit)).toBe("");
41 expect(edit.caret).toEqual({ field: 0, offset: 0 });
42 });
43​
44 it("continues checklists and bullets with an empty item", () => {
45 expect(body(splitBlock(note("", "[x] Eggs"), 0, 4, 4))).toBe("[x] Eggs\n[ ] ");
46 expect(body(splitBlock(note("", "- ab"), 0, 1, 1))).toBe("- a\n- b");
47 });
48​
49 it("on an empty list item ends the list", () => {
50 const edit = splitBlock(note("", "[ ] Eggs\n[ ] "), 1, 0, 0);
51 expect(body(edit)).toBe("[ ] Eggs\n");
52 expect(edit.caret).toEqual({ field: 1, offset: 0 });
53 });
54​
55 it("after a heading starts a paragraph", () => {
56 expect(body(splitBlock(note("", "# Goals"), 0, 5, 5))).toBe("# Goals\n");
57 });
58});
59​
60describe("Backspace at the start of a line", () => {
61 it("turns a list item into a paragraph first", () => {
62 expect(body(joinWithPrevious(note("", "Milk\n[ ] Eggs"), 1))).toBe("Milk\nEggs");
63 });
64​
65 it("joins a paragraph to the line above", () => {
66 const edit = joinWithPrevious(note("", "[ ] Milk\nshake"), 1);
67 expect(body(edit)).toBe("[ ] Milkshake");
68 expect(edit.caret).toEqual({ field: 0, offset: 4 });
69 });
70​
71 it("joins the first line to the title", () => {
72 const edit = joinWithPrevious(note("Shopping", " list\nMilk"), 0);
73 expect(edit.title).toBe("Shopping list");
74 expect(body(edit)).toBe("Milk");
75 expect(edit.caret).toEqual({ field: -1, offset: 8 });
76 });
77});
78​
79describe("typing and pasting", () => {
80 it("keeps the caret where the browser put it", () => {
81 const edit = setBlockText(note("", "Mlk"), 0, "Milk", 2);
82 expect(body(edit)).toBe("Milk");
83 expect(edit.caret).toEqual({ field: 0, offset: 2 });
84 });
85​
86 it("turns markdown shortcuts into blocks", () => {
87 expect(setBlockText(note("", ""), 0, "- ", 2).blocks[0]).toEqual({ type: "bullet", text: "" });
88 expect(setBlockText(note("", ""), 0, "[ ] ", 4).blocks[0]).toMatchObject({
89 type: "checklist",
90 });
91 });
92​
93 it("splits pasted lines into blocks", () => {
94 const edit = setBlockText(note("", "Intro\nEnd"), 0, "Intro\n- a\n[ ] b", 15);
95 expect(body(edit)).toBe("Intro\n- a\n[ ] b\nEnd");
96 expect(edit.caret).toEqual({ field: 2, offset: 1 });
97 });
98​
99 it("moves extra lines pasted into the title into the body", () => {
100 const edit = setTitle(note("", ""), "Title\nfirst line", 16);
101 expect(edit.title).toBe("Title");
102 expect(body(edit)).toBe("first line");
103 });
104});
105​
106describe("checklists", () => {
107 it("toggle a line in and out of a checklist", () => {
108 const once = toggleChecklist(note("", "Eggs"), 0);
109 expect(body(once)).toBe("[ ] Eggs");
110 expect(body(toggleChecklist(once, 0))).toBe("Eggs");
111 });
112​
113 it("tick and untick items", () => {
114 expect(serializeBody(toggleChecked(parseEditableBody("[ ] Eggs"), 0))).toBe("[x] Eggs");
115 expect(serializeBody(toggleChecked(parseEditableBody("Eggs"), 0))).toBe("Eggs");
116 });
117});