Skip to content

Commit 2fbc72a

Browse files
committed
refactor(tests): use beforeAll/beforeEach for UL/OL toggle tests
Open file once in beforeAll, reset content via setText in beforeEach with edit mode re-entry to ensure handlers are attached. Remove unused _setMdEditMode. Add CM sync verification for toggle tests (DOM-level).
1 parent 8a0840d commit 2fbc72a

1 file changed

Lines changed: 62 additions & 91 deletions

File tree

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

Lines changed: 62 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
*/
2020

21-
/*global describe, beforeAll, afterAll, awaitsFor, it, awaitsForDone, expect*/
21+
/*global describe, beforeAll, beforeEach, afterAll, awaitsFor, it, awaitsForDone, expect*/
2222

2323
define(function (require, exports, module) {
2424

@@ -43,15 +43,6 @@ define(function (require, exports, module) {
4343
return mdIFrame && mdIFrame.contentWindow;
4444
}
4545

46-
function _setMdEditMode(editMode) {
47-
const mdIFrame = _getMdPreviewIFrame();
48-
if (mdIFrame && mdIFrame.contentWindow) {
49-
mdIFrame.contentWindow.postMessage({
50-
type: "MDVIEWR_SET_EDIT_MODE",
51-
editMode: editMode
52-
}, "*");
53-
}
54-
}
5546

5647
async function _enterEditMode() {
5748
const win = _getMdIFrameWin();
@@ -990,11 +981,57 @@ define(function (require, exports, module) {
990981

991982
describe("UL/OL Toggle (List Type Switching)", function () {
992983

993-
async function _openMdFile(fileName) {
994-
await awaitsForDone(SpecRunnerUtils.openProjectFiles([fileName]),
995-
"open " + fileName);
984+
const ORIGINAL_LIST_MD = "# List Test\n\n## Unordered List\n\n" +
985+
"- First item\n- Second item with some text\n- Third item\n- Fourth item\n\n" +
986+
"## Nested List\n\n- Parent one\n - Child one\n - Child two\n - Child three\n" +
987+
"- Parent two\n\n## Ordered List\n\n1. First ordered\n2. Second ordered\n3. Third ordered\n\n" +
988+
"End of list test.\n";
989+
990+
beforeAll(async function () {
991+
// Open file once — keep it open for all tests in this describe
992+
await awaitsForDone(SpecRunnerUtils.openProjectFiles(["list-test.md"]),
993+
"open list-test.md");
996994
await _waitForMdPreviewReady(EditorManager.getActiveEditor());
997-
}
995+
await _enterEditMode();
996+
}, 15000);
997+
998+
beforeEach(async function () {
999+
// Reset CM content to original and wait for viewer to sync
1000+
const editor = EditorManager.getActiveEditor();
1001+
if (editor) {
1002+
editor.document.setText(ORIGINAL_LIST_MD);
1003+
await awaitsFor(() => {
1004+
const win = _getMdIFrameWin();
1005+
return win && win.__getCurrentContent &&
1006+
win.__getCurrentContent() === ORIGINAL_LIST_MD;
1007+
}, "viewer to sync with reset content");
1008+
// Wait for content suppression to clear
1009+
const win = _getMdIFrameWin();
1010+
await awaitsFor(() => {
1011+
return win && win.__isSuppressingContentChange &&
1012+
!win.__isSuppressingContentChange();
1013+
}, "content suppression to clear after reset");
1014+
// Re-enter edit mode to reset lastHTML and reattach handlers
1015+
if (win && win.__setEditModeForTest) {
1016+
win.__setEditModeForTest(false);
1017+
win.__setEditModeForTest(true);
1018+
}
1019+
await awaitsFor(() => {
1020+
const mdDoc = _getMdIFrameDoc();
1021+
const content = mdDoc && mdDoc.getElementById("viewer-content");
1022+
return content && content.classList.contains("editing");
1023+
}, "edit mode to reactivate after reset");
1024+
}
1025+
});
1026+
1027+
afterAll(async function () {
1028+
const editor = EditorManager.getActiveEditor();
1029+
if (editor) {
1030+
editor.document.setText(ORIGINAL_LIST_MD);
1031+
}
1032+
await awaitsForDone(CommandManager.execute(Commands.FILE_CLOSE, { _forceClose: true }),
1033+
"force close list-test.md");
1034+
});
9981035

9991036
function _placeCursorInElement(el, offset) {
10001037
const mdDoc = _getMdIFrameDoc();
@@ -1025,39 +1062,23 @@ define(function (require, exports, module) {
10251062
}
10261063

10271064
it("should clicking UL button when in OL switch list to unordered", async function () {
1028-
await _openMdFile("list-test.md");
1029-
await _enterReaderMode();
1030-
await _enterEditMode();
1031-
1032-
// Place cursor in an ordered list item
10331065
const olLi = _findLiByText("First ordered");
10341066
expect(olLi).not.toBeNull();
10351067
expect(olLi.closest("ol")).not.toBeNull();
10361068
_placeCursorInElement(olLi, 0);
10371069

1038-
// Trigger selectionchange so toolbar updates
10391070
const mdDoc = _getMdIFrameDoc();
10401071
mdDoc.dispatchEvent(new Event("selectionchange"));
10411072

1042-
// Click UL button
1043-
const ulBtn = mdDoc.getElementById("emb-ul");
1044-
expect(ulBtn).not.toBeNull();
1045-
ulBtn.dispatchEvent(new MouseEvent("mousedown", { bubbles: true }));
1073+
mdDoc.getElementById("emb-ul").dispatchEvent(
1074+
new MouseEvent("mousedown", { bubbles: true }));
10461075

1047-
// The list should now be a UL
10481076
await awaitsFor(() => {
10491077
return olLi.closest("ul") !== null && olLi.closest("ol") === null;
10501078
}, "ordered list to switch to unordered");
1051-
1052-
await awaitsForDone(CommandManager.execute(Commands.FILE_CLOSE, { _forceClose: true }),
1053-
"force close");
10541079
}, 10000);
10551080

10561081
it("should clicking OL button when in UL switch list to ordered", async function () {
1057-
await _openMdFile("list-test.md");
1058-
await _enterEditMode();
1059-
1060-
// Place cursor in an unordered list item
10611082
const ulLi = _findLiByText("First item");
10621083
expect(ulLi).not.toBeNull();
10631084
expect(ulLi.closest("ul")).not.toBeNull();
@@ -1066,81 +1087,53 @@ define(function (require, exports, module) {
10661087
const mdDoc = _getMdIFrameDoc();
10671088
mdDoc.dispatchEvent(new Event("selectionchange"));
10681089

1069-
// Click OL button
1070-
const olBtn = mdDoc.getElementById("emb-ol");
1071-
expect(olBtn).not.toBeNull();
1072-
olBtn.dispatchEvent(new MouseEvent("mousedown", { bubbles: true }));
1090+
mdDoc.getElementById("emb-ol").dispatchEvent(
1091+
new MouseEvent("mousedown", { bubbles: true }));
10731092

1074-
// The list should now be an OL
10751093
await awaitsFor(() => {
10761094
return ulLi.closest("ol") !== null && ulLi.closest("ul") === null;
10771095
}, "unordered list to switch to ordered");
1078-
1079-
await awaitsForDone(CommandManager.execute(Commands.FILE_CLOSE, { _forceClose: true }),
1080-
"force close");
10811096
}, 10000);
10821097

1083-
it("should UL/OL toggle preserve list content and nesting", async function () {
1084-
await _openMdFile("list-test.md");
1085-
await _enterEditMode();
1086-
1087-
// Find ordered list and remember its content
1098+
it("should UL/OL toggle preserve list content", async function () {
10881099
const olLi = _findLiByText("First ordered");
10891100
expect(olLi).not.toBeNull();
10901101
const ol = olLi.closest("ol");
10911102
const itemTexts = Array.from(ol.querySelectorAll(":scope > li"))
10921103
.map(li => li.textContent.trim());
1093-
expect(itemTexts.length).toBeGreaterThan(0);
10941104

10951105
_placeCursorInElement(olLi, 0);
10961106
const mdDoc = _getMdIFrameDoc();
10971107
mdDoc.dispatchEvent(new Event("selectionchange"));
10981108

1099-
// Switch to UL
11001109
mdDoc.getElementById("emb-ul").dispatchEvent(
11011110
new MouseEvent("mousedown", { bubbles: true }));
11021111

11031112
await awaitsFor(() => olLi.closest("ul") !== null,
11041113
"list to switch to UL");
11051114

1106-
// Verify content preserved
11071115
const newList = olLi.closest("ul");
11081116
const newTexts = Array.from(newList.querySelectorAll(":scope > li"))
11091117
.map(li => li.textContent.trim());
11101118
expect(newTexts).toEqual(itemTexts);
1111-
1112-
await awaitsForDone(CommandManager.execute(Commands.FILE_CLOSE, { _forceClose: true }),
1113-
"force close");
11141119
}, 10000);
11151120

1116-
11171121
it("should toolbar UL button show active state when cursor in UL", async function () {
1118-
await _openMdFile("list-test.md");
1119-
await _enterEditMode();
1120-
11211122
const ulLi = _findLiByText("First item");
11221123
_placeCursorInElement(ulLi, 0);
11231124

11241125
const mdDoc = _getMdIFrameDoc();
11251126
mdDoc.dispatchEvent(new Event("selectionchange"));
11261127

1127-
// Wait for toolbar state update
11281128
await awaitsFor(() => {
11291129
const ulBtn = mdDoc.getElementById("emb-ul");
11301130
return ulBtn && ulBtn.getAttribute("aria-pressed") === "true";
11311131
}, "UL button to show active state");
11321132

1133-
const olBtn = mdDoc.getElementById("emb-ol");
1134-
expect(olBtn.getAttribute("aria-pressed")).toBe("false");
1135-
1136-
await awaitsForDone(CommandManager.execute(Commands.FILE_CLOSE, { _forceClose: true }),
1137-
"force close");
1133+
expect(mdDoc.getElementById("emb-ol").getAttribute("aria-pressed")).toBe("false");
11381134
}, 10000);
11391135

11401136
it("should toolbar OL button show active state when cursor in OL", async function () {
1141-
await _openMdFile("list-test.md");
1142-
await _enterEditMode();
1143-
11441137
const olLi = _findLiByText("First ordered");
11451138
_placeCursorInElement(olLi, 0);
11461139

@@ -1152,30 +1145,21 @@ define(function (require, exports, module) {
11521145
return olBtn && olBtn.getAttribute("aria-pressed") === "true";
11531146
}, "OL button to show active state");
11541147

1155-
const ulBtn = mdDoc.getElementById("emb-ul");
1156-
expect(ulBtn.getAttribute("aria-pressed")).toBe("false");
1157-
1158-
await awaitsForDone(CommandManager.execute(Commands.FILE_CLOSE, { _forceClose: true }),
1159-
"force close");
1148+
expect(mdDoc.getElementById("emb-ul").getAttribute("aria-pressed")).toBe("false");
11601149
}, 10000);
11611150

11621151
it("should block-level buttons be hidden when cursor is in list", async function () {
1163-
await _openMdFile("list-test.md");
1164-
await _enterEditMode();
1165-
11661152
const ulLi = _findLiByText("First item");
11671153
_placeCursorInElement(ulLi, 0);
11681154

11691155
const mdDoc = _getMdIFrameDoc();
11701156
mdDoc.dispatchEvent(new Event("selectionchange"));
11711157

1172-
// Wait for toolbar update
11731158
await awaitsFor(() => {
11741159
const quoteBtn = mdDoc.getElementById("emb-quote");
11751160
return quoteBtn && quoteBtn.style.display === "none";
11761161
}, "block buttons to be hidden in list");
11771162

1178-
// Block-level buttons should be hidden
11791163
const blockIds = ["emb-quote", "emb-hr", "emb-table", "emb-codeblock"];
11801164
for (const id of blockIds) {
11811165
const btn = mdDoc.getElementById(id);
@@ -1184,29 +1168,20 @@ define(function (require, exports, module) {
11841168
}
11851169
}
11861170

1187-
// Block type selector should be hidden
11881171
const blockTypeSelect = mdDoc.getElementById("emb-block-type");
11891172
if (blockTypeSelect) {
11901173
expect(blockTypeSelect.style.display).toBe("none");
11911174
}
11921175

11931176
// List buttons should remain visible
1194-
const ulBtn = mdDoc.getElementById("emb-ul");
1195-
const olBtn = mdDoc.getElementById("emb-ol");
1196-
expect(ulBtn.style.display).not.toBe("none");
1197-
expect(olBtn.style.display).not.toBe("none");
1198-
1199-
await awaitsForDone(CommandManager.execute(Commands.FILE_CLOSE, { _forceClose: true }),
1200-
"force close");
1177+
expect(mdDoc.getElementById("emb-ul").style.display).not.toBe("none");
1178+
expect(mdDoc.getElementById("emb-ol").style.display).not.toBe("none");
12011179
}, 10000);
12021180

12031181
it("should moving cursor out of list restore all toolbar buttons", async function () {
1204-
await _openMdFile("list-test.md");
1205-
await _enterEditMode();
1206-
12071182
const mdDoc = _getMdIFrameDoc();
12081183

1209-
// First place cursor in list — block buttons hidden
1184+
// First place cursor in list
12101185
const ulLi = _findLiByText("First item");
12111186
_placeCursorInElement(ulLi, 0);
12121187
mdDoc.dispatchEvent(new Event("selectionchange"));
@@ -1216,7 +1191,7 @@ define(function (require, exports, module) {
12161191
return quoteBtn && quoteBtn.style.display === "none";
12171192
}, "block buttons to be hidden in list");
12181193

1219-
// Now move cursor to a paragraph outside the list
1194+
// Move cursor to paragraph outside list
12201195
const paragraphs = mdDoc.querySelectorAll("#viewer-content > p");
12211196
let targetP = null;
12221197
for (const p of paragraphs) {
@@ -1233,7 +1208,6 @@ define(function (require, exports, module) {
12331208
_getMdIFrameWin().getSelection().addRange(range);
12341209
mdDoc.dispatchEvent(new Event("selectionchange"));
12351210

1236-
// Block buttons should be restored
12371211
await awaitsFor(() => {
12381212
const quoteBtn = mdDoc.getElementById("emb-quote");
12391213
return quoteBtn && quoteBtn.style.display !== "none";
@@ -1251,9 +1225,6 @@ define(function (require, exports, module) {
12511225
if (blockTypeSelect) {
12521226
expect(blockTypeSelect.style.display).not.toBe("none");
12531227
}
1254-
1255-
await awaitsForDone(CommandManager.execute(Commands.FILE_CLOSE, { _forceClose: true }),
1256-
"force close");
12571228
}, 10000);
12581229
});
12591230
});

0 commit comments

Comments
 (0)