Skip to content

test(markdownEditorUtils): add unit tests for pure utility functions#604

Open
Shubh-Raj wants to merge 10 commits intoaccordproject:mainfrom
Shubh-Raj:shubhraj/tests/markdowneditorutils-unit-tests
Open

test(markdownEditorUtils): add unit tests for pure utility functions#604
Shubh-Raj wants to merge 10 commits intoaccordproject:mainfrom
Shubh-Raj:shubhraj/tests/markdowneditorutils-unit-tests

Conversation

@Shubh-Raj
Copy link
Contributor

Closes #603

Add comprehensive unit tests for the pure utility functions in markdownEditorUtils.ts to improve test coverage for the new Markdown Toolbar feature.

Changes

  • Add src/tests/utils/markdownEditorUtils.test.ts with 24 unit tests covering:
    • parseLine: 6 tests for whitespace extraction
    • detectPrefix: 9 tests for markdown prefix detection
    • allLinesHavePrefix: 6 tests for prefix validation
    • createLineEditRange: 3 tests for range creation
  • Add Monaco editor mock at src/utils/testing/mocks/monaco-editor.ts to enable testing utilities that import Monaco types
  • Update vite.config.ts with deps.inline and alias config to use the Monaco mock in tests

Flags

  • This PR only tests pure utility functions that don't require Monaco editor mocking beyond types
  • The Monaco mock infrastructure added here can be reused for future editor utility tests
  • All existing 34 tests continue to pass; total test count increases to 58

Screenshots or Video

N/A - This is a test-only change with no UI modifications.

Related Issues

Author Checklist

  • Ensure you provide a DCO sign-off for your commits using the --signoff option of git commit.
  • Vital features and changes captured in unit and/or integration tests
  • Commits messages follow AP format
  • Extend the documentation, if necessary
  • Merging to main from Shubh-Raj:shubhraj/tests/markdowneditorutils-unit-tests

Add comprehensive unit tests for markdownEditorUtils.ts pure functions:
- parseLine: 6 tests for whitespace extraction
- detectPrefix: 9 tests for markdown prefix detection
- allLinesHavePrefix: 6 tests for prefix validation
- createLineEditRange: 3 tests for range creation

Also adds Monaco editor mock to enable testing utilities that
import Monaco types without loading the full editor.

Total new tests: 24 (repo total: 58)

Signed-off-by: Shubh-Raj <shubhraj625@gmail.com>
@Shubh-Raj Shubh-Raj requested a review from a team as a code owner January 19, 2026 18:55
@netlify
Copy link

netlify bot commented Jan 19, 2026

Deploy Preview for ap-template-playground ready!

Name Link
🔨 Latest commit 4d3fd87
🔍 Latest deploy log https://app.netlify.com/projects/ap-template-playground/deploys/69ac613b14484000081f126b
😎 Deploy Preview https://deploy-preview-604--ap-template-playground.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copilot AI review requested due to automatic review settings February 24, 2026 14:22
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds Vitest unit coverage for the pure helper functions in src/utils/markdownEditorUtils.ts (used by the Markdown Toolbar feature) and introduces a lightweight monaco-editor mock so these utilities can be imported in tests without pulling in the full Monaco runtime.

Changes:

  • Adds 24 unit tests covering createLineEditRange, parseLine, detectPrefix, and allLinesHavePrefix.
  • Introduces a monaco-editor module mock for the unit-test environment.
  • Updates Vitest config to inline and alias monaco-editor to the mock during tests.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
vite.config.ts Configures Vitest to alias monaco-editor to a test mock (and inline the dependency).
src/utils/testing/mocks/monaco-editor.ts Provides a minimal Monaco mock (Selection + basic exports) to support importing editor utilities in unit tests.
src/tests/utils/markdownEditorUtils.test.ts Adds unit tests for the pure utility functions powering markdown prefix/whitespace logic.

Copilot AI review requested due to automatic review settings March 3, 2026 06:36
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

Comment on lines +30 to +32
alias: {
"monaco-editor": fileURLToPath(new URL("./src/utils/testing/__mocks__/monaco-editor.ts", import.meta.url)),
},
Copy link

Copilot AI Mar 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Vitest config aliases the entire monaco-editor module to a local mock for all unit tests. This makes it hard to add future tests that rely on real Monaco behavior (or a richer mock) and can cause unrelated test failures when a component imports Monaco at runtime. Consider scoping the Monaco mocking to only the specific test(s) that need it (e.g., vi.mock('monaco-editor', ...) in the test file or in setup.ts with conditional logic), or alternatively remove the runtime Monaco import need by switching the util to import type (so no module-level Monaco load occurs during tests).

Suggested change
alias: {
"monaco-editor": fileURLToPath(new URL("./src/utils/testing/__mocks__/monaco-editor.ts", import.meta.url)),
},

Copilot uses AI. Check for mistakes.
Comment on lines +15 to +16
export const editor = {};

Copy link

Copilot AI Mar 3, 2026

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 editor object and a Selection class. Because the Vitest config aliases monaco-editor globally, 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 (stub languages, editor, etc.), or avoid applying the alias globally and mock Monaco only in the tests that need it.

Suggested change
export const editor = {};
/**
* Minimal stub for the monaco.editor namespace used in tests.
* Functions are no-ops that return simple disposable-like objects
* to prevent runtime errors when test code calls Monaco APIs.
*/
export const editor = {
create: (_domElement?: unknown, _options?: unknown) => {
const model = {
getValue: (): string => '',
setValue: (_value: string): void => {
// no-op
},
dispose: (): void => {
// no-op
},
};
return {
onDidChangeModelContent: (
_listener: () => void
): { dispose: () => void } => ({
dispose: (): void => {
// no-op
},
}),
getValue: (): string => '',
setValue: (_value: string): void => {
// no-op
},
getModel: () => model,
updateOptions: (_options: unknown): void => {
// no-op
},
dispose: (): void => {
// no-op
},
};
},
createModel: (value: string, _language?: string) => ({
getValue: (): string => value,
setValue: (_newValue: string): void => {
// no-op
},
dispose: (): void => {
// no-op
},
}),
setModelLanguage: (_model: unknown, _languageId: string): void => {
// no-op
},
};
/**
* Minimal stub for the monaco.languages namespace used in tests.
*/
export const languages = {
register: (_language: unknown): void => {
// no-op
},
setMonarchTokensProvider: (_languageId: string, _tokensProvider: unknown): void => {
// no-op
},
registerCompletionItemProvider: (
_languageId: string,
_provider: unknown
): { dispose: () => void } => ({
dispose: (): void => {
// no-op
},
}),
};

Copilot uses AI. Check for mistakes.
Copilot AI review requested due to automatic review settings March 7, 2026 17:32
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: Add unit tests for markdownEditorUtils.ts pure utility functions

2 participants