Skip to content

Commit f3d0bfa

Browse files
committed
refactor(tests): replace _codeMirror access with Editor/Document APIs
Use editor.document.getText(), editor.replaceRange(), editor.getCursorPos(), editor.setSelection(), editor.getSelectedText(), editor.lineCount(), and editor.getLine() instead of directly accessing editor._codeMirror. Also update to-create-tests.md with newly covered test items.
1 parent 3e11211 commit f3d0bfa

2 files changed

Lines changed: 43 additions & 41 deletions

File tree

src-mdviewer/to-create-tests.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
## Cursor/Scroll Sync
44
- [ ] Clicking in CM scrolls md viewer to corresponding element
5-
- [ ] Clicking in md viewer scrolls CM to corresponding line (centered)
6-
- [ ] Cursor sync toggle button disables/enables bidirectional sync
5+
- [x] Clicking in md viewer scrolls CM to corresponding line (centered)
6+
- [x] Cursor sync toggle button disables/enables bidirectional sync
77
- [ ] Cursor sync toggle state preserved across toolbar re-renders (file switch, mode toggle)
88
- [ ] Content sync still works when cursor sync is disabled
99
- [ ] Cursor sync toggle works in both reader and edit mode
1010
- [ ] Disabling cursor sync in reader mode prevents CM scroll on click
1111
- [ ] Cursor sync works on newly edited elements after edit→reader switch
1212
- [ ] Edit→reader switch re-renders from CM content (data-source-line attrs refreshed)
13-
- [ ] Switching MD files preserves current edit/reader mode
14-
- [ ] Edit mode not reset when switching between MD files
13+
- [x] Switching MD files preserves current edit/reader mode
14+
- [x] Edit mode not reset when switching between MD files
1515

1616
## Edit Mode & Entitlement Gating
1717
- [ ] Free user sees Edit button → clicking shows upsell dialog
@@ -29,7 +29,12 @@
2929
## Format Bar & Link Popover
3030
- [x] Format bar element exists in DOM with bold/italic/underline/link buttons
3131
- [x] Link popover element exists in DOM
32+
- [x] Adding link in CM shows it in md viewer
33+
- [x] Editing link URL in CM updates it in md viewer
34+
- [x] Removing link markup in CM removes link from md viewer
3235
- [ ] Format bar appears on text selection (visual — needs real mouse interaction)
36+
- [x] Link popover edit URL via popover syncs to CM
37+
- [x] Link popover remove link via popover syncs to CM
3338
- [ ] Link popover URL opens in default browser (not Electron window)
3439
- [ ] Escape in lang picker only dismisses picker, refocuses editor
3540

@@ -47,8 +52,8 @@
4752
- [ ] Slash menu works at bottom of a long scrolled document
4853

4954
## Keyboard Shortcut Focus
50-
- [ ] Ctrl+S saves file and keeps focus in md editor (not CM)
51-
- [ ] Forwarded shortcuts refocus md editor after Phoenix handles them
55+
- [x] Ctrl+S saves file and keeps focus in md editor (not CM)
56+
- [x] Forwarded shortcuts refocus md editor after Phoenix handles them
5257

5358
## Code Block Editing
5459
- [ ] ArrowDown on last line of code block exits to paragraph below
@@ -147,8 +152,8 @@
147152
- [ ] Backspace in middle of heading works normally (deletes character)
148153

149154
## Undo/Redo
150-
- [ ] Ctrl+Z undoes change in both md editor and CM (single undo stack)
151-
- [ ] Ctrl+Shift+Z / Ctrl+Y redoes change in both
155+
- [x] Ctrl+Z undoes change in both md editor and CM (single undo stack)
156+
- [x] Ctrl+Shift+Z / Ctrl+Y redoes change in both
152157
- [ ] Cursor restored to correct block element (source-line) after undo
153158
- [ ] Cursor restored to correct offset within block after undo
154159
- [ ] Undo/redo cursor works when editing at different positions in document

test/spec/md-editor-integ-test.js

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ define(function (require, exports, module) {
199199
* @param {Object} editor - The active Editor instance whose content should be synced to the viewer.
200200
*/
201201
async function _waitForMdPreviewReady(editor) {
202-
const expectedSrc = editor ? editor._codeMirror.getValue() : null;
202+
const expectedSrc = editor ? editor.document.getText() : null;
203203
await awaitsFor(() => {
204204
const mdIFrame = _getMdPreviewIFrame();
205205
if (!mdIFrame || mdIFrame.style.display === "none") { return false; }
@@ -294,8 +294,7 @@ define(function (require, exports, module) {
294294
async function _resetFileContent() {
295295
const editor = EditorManager.getActiveEditor();
296296
if (editor && editor.document) {
297-
const cm = editor._codeMirror;
298-
cm.setValue(ORIGINAL_MD_CONTENT);
297+
editor.document.setText(ORIGINAL_MD_CONTENT);
299298
await awaitsForDone(CommandManager.execute(Commands.FILE_SAVE), "save after reset");
300299
await awaitsFor(() => !editor.document.isDirty, "document to be clean after reset save");
301300
await awaitsFor(() => {
@@ -333,16 +332,14 @@ define(function (require, exports, module) {
333332

334333
// Make a small edit in CM to dirty the document
335334
const editor = EditorManager.getActiveEditor();
336-
const cm = editor._codeMirror;
337-
const doc = editor.document;
338-
cm.replaceRange(" ", { line: 0, ch: 0 });
335+
editor.replaceRange(" ", { line: 0, ch: 0 });
339336

340-
await awaitsFor(() => doc.isDirty, "document to become dirty");
337+
await awaitsFor(() => editor.document.isDirty, "document to become dirty");
341338

342339
// Dispatch Ctrl+S in the md iframe — should trigger save
343340
_dispatchKeyInMdIframe("s");
344341

345-
await awaitsFor(() => !doc.isDirty, "document to be saved (dirty flag cleared)");
342+
await awaitsFor(() => !editor.document.isDirty, "document to be saved (dirty flag cleared)");
346343
}, 10000);
347344

348345
it("should Ctrl+Shift+F in edit mode open Find in Files", async function () {
@@ -942,15 +939,15 @@ define(function (require, exports, module) {
942939
// Ensure the CM editor is created by focusing it
943940
await awaitsFor(() => {
944941
const ed = EditorManager.getActiveEditor();
945-
return ed && ed._codeMirror;
946-
}, "CM editor for long.md to be created");
942+
return ed && ed.document;
943+
}, "editor for long.md to be created");
947944
await _enterReaderMode();
948945
}
949946
}, 30000);
950947

951948
function _getCMCursorLine() {
952949
const editor = EditorManager.getActiveEditor();
953-
return editor ? editor._codeMirror.getCursor().line : -1;
950+
return editor ? editor.getCursorPos().line : -1;
954951
}
955952

956953
function _hasViewerHighlight() {
@@ -963,7 +960,7 @@ define(function (require, exports, module) {
963960
// Wait for editor to be fully ready (masterEditor established)
964961
await awaitsFor(() => {
965962
const ed = EditorManager.getActiveEditor();
966-
return ed && ed._codeMirror && ed.document && ed.document._masterEditor;
963+
return ed && ed.document && ed.document._masterEditor;
967964
}, "editor with masterEditor to be ready");
968965

969966
// Clear any existing highlights
@@ -975,16 +972,15 @@ define(function (require, exports, module) {
975972
// Select text in CM and dispatch highlight to iframe.
976973
// MarkdownSync sends postMessage as thers some race where the cursor isnt syncing it seems
977974
const editor = EditorManager.getActiveEditor();
978-
const cm = editor._codeMirror;
979-
cm.setSelection({ line: 4, ch: 0 }, { line: 6, ch: 0 });
980-
expect(cm.getSelection().length).toBeGreaterThan(0);
975+
editor.setSelection({ line: 4, ch: 0 }, { line: 6, ch: 0 });
976+
expect(editor.getSelectedText().length).toBeGreaterThan(0);
981977

982978
const win = _getMdIFrameWin();
983979
win.dispatchEvent(new MessageEvent("message", {
984980
data: {
985981
type: "MDVIEWR_HIGHLIGHT_SELECTION",
986982
fromLine: 5, toLine: 7,
987-
selectedText: cm.getSelection()
983+
selectedText: editor.getSelectedText()
988984
}
989985
}));
990986

@@ -1281,10 +1277,10 @@ define(function (require, exports, module) {
12811277
await _openMdFile("doc2.md");
12821278
await _enterEditMode();
12831279

1284-
const cm = EditorManager.getActiveEditor()._codeMirror;
1285-
const lastLine = cm.lastLine();
1286-
cm.replaceRange("\n\n[CM Link](https://cm-link-test.example.com)\n",
1287-
{ line: lastLine, ch: cm.getLine(lastLine).length });
1280+
const editor = EditorManager.getActiveEditor();
1281+
const lastLine = editor.lineCount() - 1;
1282+
editor.replaceRange("\n\n[CM Link](https://cm-link-test.example.com)\n",
1283+
{ line: lastLine, ch: editor.getLine(lastLine).length });
12881284

12891285
const mdDoc = _getMdIFrameDoc();
12901286
await awaitsFor(() => {
@@ -1297,21 +1293,21 @@ define(function (require, exports, module) {
12971293
await _openMdFile("doc2.md");
12981294
await _enterEditMode();
12991295

1300-
const cm = EditorManager.getActiveEditor()._codeMirror;
1301-
const val = cm.getValue();
1296+
const editor = EditorManager.getActiveEditor();
13021297

13031298
// Add a link
1304-
cm.replaceRange("\n[Old Link](https://old-url.example.com)\n",
1305-
{ line: cm.lastLine(), ch: cm.getLine(cm.lastLine()).length });
1299+
const lastLine = editor.lineCount() - 1;
1300+
editor.replaceRange("\n[Old Link](https://old-url.example.com)\n",
1301+
{ line: lastLine, ch: editor.getLine(lastLine).length });
13061302

13071303
const mdDoc = _getMdIFrameDoc();
13081304
await awaitsFor(() =>
13091305
mdDoc.querySelector('#viewer-content a[href="https://old-url.example.com"]') !== null,
13101306
"old link to appear in viewer");
13111307

13121308
// Change the URL in CM
1313-
const cmVal = cm.getValue();
1314-
cm.setValue(cmVal.replace("https://old-url.example.com", "https://new-url.example.com"));
1309+
const cmVal = editor.document.getText();
1310+
editor.document.setText(cmVal.replace("https://old-url.example.com", "https://new-url.example.com"));
13151311

13161312
await awaitsFor(() =>
13171313
mdDoc.querySelector('#viewer-content a[href="https://new-url.example.com"]') !== null,
@@ -1325,20 +1321,21 @@ define(function (require, exports, module) {
13251321
await _openMdFile("doc3.md");
13261322
await _enterEditMode();
13271323

1328-
const cm = EditorManager.getActiveEditor()._codeMirror;
1324+
const editor = EditorManager.getActiveEditor();
13291325

13301326
// Add a link
1331-
cm.replaceRange("\n[Remove Me](https://remove-cm.example.com)\n",
1332-
{ line: cm.lastLine(), ch: cm.getLine(cm.lastLine()).length });
1327+
const lastLine = editor.lineCount() - 1;
1328+
editor.replaceRange("\n[Remove Me](https://remove-cm.example.com)\n",
1329+
{ line: lastLine, ch: editor.getLine(lastLine).length });
13331330

13341331
const mdDoc = _getMdIFrameDoc();
13351332
await awaitsFor(() =>
13361333
mdDoc.querySelector('#viewer-content a[href="https://remove-cm.example.com"]') !== null,
13371334
"link to appear");
13381335

13391336
// Remove the link markup — replace [text](url) with just text
1340-
const cmVal = cm.getValue();
1341-
cm.setValue(cmVal.replace("[Remove Me](https://remove-cm.example.com)", "Remove Me"));
1337+
const cmVal = editor.document.getText();
1338+
editor.document.setText(cmVal.replace("[Remove Me](https://remove-cm.example.com)", "Remove Me"));
13421339

13431340
await awaitsFor(() =>
13441341
mdDoc.querySelector('#viewer-content a[href="https://remove-cm.example.com"]') === null,
@@ -1390,7 +1387,7 @@ define(function (require, exports, module) {
13901387
// Verify CM source has the edited URL
13911388
const editor = EditorManager.getActiveEditor();
13921389
await awaitsFor(() => {
1393-
const cmVal = editor._codeMirror.getValue();
1390+
const cmVal = editor.document.getText();
13941391
return cmVal.includes("https://edited-popover.example.com") &&
13951392
!cmVal.includes("test-link-doc2.example.com");
13961393
}, "CM source to contain edited URL and not old URL");
@@ -1434,7 +1431,7 @@ define(function (require, exports, module) {
14341431
// Verify CM source has link text but no markdown link syntax
14351432
const editor = EditorManager.getActiveEditor();
14361433
await awaitsFor(() => {
1437-
const cmVal = editor._codeMirror.getValue();
1434+
const cmVal = editor.document.getText();
14381435
return cmVal.includes("Remove Link") &&
14391436
!cmVal.includes("[Remove Link](https://remove-link-doc3.example.com)");
14401437
}, "CM source to have plain text without link markdown");

0 commit comments

Comments
 (0)