-
-
Notifications
You must be signed in to change notification settings - Fork 215
Expand file tree
/
Copy pathmarkdownEditorUtils.test.ts
More file actions
202 lines (178 loc) · 5.58 KB
/
markdownEditorUtils.test.ts
File metadata and controls
202 lines (178 loc) · 5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import {
parseLine,
detectPrefix,
allLinesHavePrefix,
createLineEditRange,
} from "../../utils/markdownEditorUtils";
describe("markdownEditorUtils", () => {
describe("parseLine", () => {
it("should return empty whitespace for line with no leading spaces", () => {
const result = parseLine("Hello World");
expect(result).toEqual({
leadingWhitespace: "",
content: "Hello World",
});
});
it("should extract leading spaces", () => {
const result = parseLine(" Hello");
expect(result).toEqual({
leadingWhitespace: " ",
content: "Hello",
});
});
it("should extract leading tabs", () => {
const result = parseLine("\t\tHello");
expect(result).toEqual({
leadingWhitespace: "\t\t",
content: "Hello",
});
});
it("should handle mixed whitespace", () => {
const result = parseLine(" \t Hello");
expect(result).toEqual({
leadingWhitespace: " \t ",
content: "Hello",
});
});
it("should handle empty line", () => {
const result = parseLine("");
expect(result).toEqual({
leadingWhitespace: "",
content: "",
});
});
it("should handle line with only whitespace", () => {
const result = parseLine(" ");
expect(result).toEqual({
leadingWhitespace: " ",
content: "",
});
});
});
describe("detectPrefix", () => {
it("should return undefined matched when no prefix found", () => {
const result = detectPrefix("Hello World", ["#", "##", "###"]);
expect(result).toEqual({
matched: undefined,
stripped: "Hello World",
});
});
it("should detect single hash heading prefix", () => {
const result = detectPrefix("# Hello", ["#", "##", "###"]);
expect(result).toEqual({
matched: "#",
stripped: "Hello",
});
});
it("should detect double hash heading prefix", () => {
const result = detectPrefix("## Hello", ["#", "##", "###"]);
expect(result).toEqual({
matched: "##",
stripped: "Hello",
});
});
it("should detect triple hash heading prefix", () => {
const result = detectPrefix("### Hello", ["#", "##", "###"]);
expect(result).toEqual({
matched: "###",
stripped: "Hello",
});
});
it("should detect unordered list prefix", () => {
const result = detectPrefix("- Item", ["-"]);
expect(result).toEqual({
matched: "-",
stripped: "Item",
});
});
it("should handle prefix with no content after it", () => {
const result = detectPrefix("#", ["#", "##"]);
expect(result).toEqual({
matched: "#",
stripped: "",
});
});
it("should not match prefix that is not in the list", () => {
const result = detectPrefix("* Item", ["-"]);
expect(result).toEqual({
matched: undefined,
stripped: "* Item",
});
});
it("should handle content with leading spaces before prefix", () => {
const result = detectPrefix(" # Hello", ["#", "##"]);
expect(result).toEqual({
matched: "#",
stripped: "Hello",
});
});
it("should not match when prefix is part of word", () => {
const result = detectPrefix("#hashtag", ["#"]);
expect(result).toEqual({
matched: undefined,
stripped: "#hashtag",
});
});
});
describe("allLinesHavePrefix", () => {
it("should return true when all lines have the same prefix", () => {
const lines = ["# Hello", "# World", "# Test"];
const result = allLinesHavePrefix(lines, "#", ["#", "##", "###"]);
expect(result).toBe(true);
});
it("should return false when lines have different prefixes", () => {
const lines = ["# Hello", "## World", "# Test"];
const result = allLinesHavePrefix(lines, "#", ["#", "##", "###"]);
expect(result).toBe(false);
});
it("should return false when some lines have no prefix", () => {
const lines = ["# Hello", "World", "# Test"];
const result = allLinesHavePrefix(lines, "#", ["#", "##", "###"]);
expect(result).toBe(false);
});
it("should handle single line", () => {
const lines = ["# Hello"];
const result = allLinesHavePrefix(lines, "#", ["#", "##", "###"]);
expect(result).toBe(true);
});
it("should handle lines with leading whitespace", () => {
const lines = [" # Hello", " # World"];
const result = allLinesHavePrefix(lines, "#", ["#", "##", "###"]);
expect(result).toBe(true);
});
it("should return true for empty array", () => {
const lines: string[] = [];
const result = allLinesHavePrefix(lines, "#", ["#", "##", "###"]);
expect(result).toBe(true);
});
});
describe("createLineEditRange", () => {
it("should create correct range for line 1", () => {
const result = createLineEditRange(1, 10);
expect(result).toEqual({
startLineNumber: 1,
startColumn: 1,
endLineNumber: 1,
endColumn: 11,
});
});
it("should create correct range for arbitrary line", () => {
const result = createLineEditRange(5, 20);
expect(result).toEqual({
startLineNumber: 5,
startColumn: 1,
endLineNumber: 5,
endColumn: 21,
});
});
it("should handle zero-length line", () => {
const result = createLineEditRange(3, 0);
expect(result).toEqual({
startLineNumber: 3,
startColumn: 1,
endLineNumber: 3,
endColumn: 1,
});
});
});
});