-
-
Notifications
You must be signed in to change notification settings - Fork 215
test(markdownEditorUtils): add unit tests for pure utility functions #604
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Shubh-Raj
wants to merge
12
commits into
accordproject:main
Choose a base branch
from
Shubh-Raj:shubhraj/tests/markdowneditorutils-unit-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+226
−1
Open
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ef1f461
test(markdownEditorUtils): add unit tests for pure utility functions
Shubh-Raj 75d20a5
Merge branch 'main' into shubhraj/tests/markdowneditorutils-unit-tests
Shubh-Raj 536f927
Merge branch 'main' into shubhraj/tests/markdowneditorutils-unit-tests
Shubh-Raj b6fa035
Merge branch 'main' into shubhraj/tests/markdowneditorutils-unit-tests
Shubh-Raj d48a19f
Merge branch 'main' into shubhraj/tests/markdowneditorutils-unit-tests
Shubh-Raj fb38b39
Merge branch 'main' into shubhraj/tests/markdowneditorutils-unit-tests
Shubh-Raj 1650059
Merge branch 'main' into shubhraj/tests/markdowneditorutils-unit-tests
Shubh-Raj a41615a
fix: use fileURLToPath for cross-platform path resolution in Vitest a…
Shubh-Raj 26ec385
Merge branch 'main' into shubhraj/tests/markdowneditorutils-unit-tests
Shubh-Raj 4d3fd87
Merge branch 'main' into shubhraj/tests/markdowneditorutils-unit-tests
Shubh-Raj 266f8ec
Merge branch 'main' into shubhraj/tests/markdowneditorutils-unit-tests
Shubh-Raj 0030eea
Merge branch 'main' into shubhraj/tests/markdowneditorutils-unit-tests
Shubh-Raj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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, | ||
| }); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /** | ||
| * Mock for monaco-editor module used in tests | ||
| * This allows testing utilities that import Monaco types without loading the full editor | ||
| */ | ||
|
|
||
| export class Selection { | ||
| constructor( | ||
| public startLineNumber: number, | ||
| public startColumn: number, | ||
| public endLineNumber: number, | ||
| public endColumn: number | ||
| ) {} | ||
| } | ||
|
|
||
| export const editor = {}; | ||
|
|
||
| export interface IRange { | ||
| startLineNumber: number; | ||
| startColumn: number; | ||
| endLineNumber: number; | ||
| endColumn: number; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This mock currently exports only an empty
editorobject and aSelectionclass. Because the Vitest config aliasesmonaco-editorglobally, any test that imports code using Monaco runtime APIs (e.g.,monaco.languages.*,monaco.editor.*) will throw at runtime with this mock. Either expand the mock to include the minimal runtime shape used by the app (stublanguages,editor, etc.), or avoid applying the alias globally and mock Monaco only in the tests that need it.