Skip to content

Commit 73c4b8a

Browse files
committed
fix(test): update custom server tests for mdviewer iframe id and architecture
- Check panel-md-preview-frame and query __getActiveFilePath() for md files - Add null guards to _waitForIframeURL and _forSVGLivePreview for iframe transitions between md and HTML preview - Expose __getActiveFilePath on mdviewer window for test access
1 parent 6753a2c commit 73c4b8a

2 files changed

Lines changed: 28 additions & 8 deletions

File tree

src-mdviewer/src/bridge.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ const _isMac = /Mac|iPod|iPhone|iPad/.test(navigator.platform);
105105
export function initBridge() {
106106
docCache.initDocCache();
107107

108+
// Expose active file path for test access (test iframes have no sandbox)
109+
window.__getActiveFilePath = docCache.getActiveFilePath;
110+
108111
// Listen for messages from Phoenix parent
109112
window.addEventListener("message", (event) => {
110113
const data = event.data;

test/spec/LiveDevelopmentCustomServer-test.js

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ define(function (require, exports, module) {
266266
async function _waitForIframeURL(url) {
267267
await awaitsFor(()=>{
268268
let iFrame = testWindow.document.getElementById("panel-live-preview-frame");
269-
return iFrame.src === url;
269+
return iFrame && iFrame.src === url;
270270
}, "external preview server "+ url);
271271
}
272272

@@ -494,8 +494,13 @@ define(function (require, exports, module) {
494494
await awaitsForDone(SpecRunnerUtils.openProjectFiles(["readme.md"]),
495495
"readme.md");
496496
await awaitsFor(()=>{
497-
let iFrame = testWindow.document.getElementById("panel-live-preview-frame");
498-
return iFrame.src.endsWith("readme.md");
497+
let mdIFrame = testWindow.document.getElementById("panel-md-preview-frame");
498+
if (!mdIFrame || !mdIFrame.contentWindow) { return false; }
499+
try {
500+
const activeFile = mdIFrame.contentWindow.__getActiveFilePath
501+
&& mdIFrame.contentWindow.__getActiveFilePath();
502+
return activeFile && activeFile.endsWith("readme.md");
503+
} catch (e) { return false; }
499504
}, "readme.md live preview");
500505

501506
// now do html, it should load from the custom server
@@ -663,7 +668,7 @@ define(function (require, exports, module) {
663668
testWindow.$("#toolbar-go-live").click();
664669
await awaitsFor(()=>{
665670
let iFrame = testWindow.document.getElementById("panel-live-preview-frame");
666-
if(!iFrame.src) {
671+
if(!iFrame || !iFrame.src) {
667672
return false;
668673
}
669674
const url = new URL(iFrame.src);
@@ -774,6 +779,7 @@ define(function (require, exports, module) {
774779
async function _forSVGLivePreview() {
775780
await awaitsFor(()=>{
776781
let iFrame = testWindow.document.getElementById("panel-live-preview-frame");
782+
if (!iFrame || !iFrame.src) { return false; }
777783
let srcURL = new URL(iFrame.src);
778784
return srcURL.pathname.endsWith(SVG_IMAGE_PATH);
779785
}, "For svg image to be in live preview");
@@ -803,11 +809,22 @@ define(function (require, exports, module) {
803809
}, 30000);
804810

805811
async function _waitForIframeMDFile(name) {
812+
// The mdviewer uses a persistent iframe (panel-md-preview-frame) that loads mdViewer/index.html.
813+
// Content is sent via postMessage, so we verify by checking the active file path inside the iframe.
806814
await awaitsFor(()=>{
807-
let outerIFrame = testWindow.document.getElementById("panel-live-preview-frame");
808-
let srcURL = new URL(outerIFrame.src);
809-
return srcURL.pathname.endsWith(name) === true;
810-
}, "waiting for name- " + name);
815+
let mdIFrame = testWindow.document.getElementById("panel-md-preview-frame");
816+
if (!mdIFrame || !mdIFrame.contentWindow) {
817+
return false;
818+
}
819+
try {
820+
// doc-cache.js exposes activeFilePath — check it ends with the expected name
821+
const activeFile = mdIFrame.contentWindow.__getActiveFilePath
822+
&& mdIFrame.contentWindow.__getActiveFilePath();
823+
return activeFile && activeFile.endsWith(name);
824+
} catch (e) {
825+
return false;
826+
}
827+
}, "waiting for md file: " + name);
811828
}
812829

813830
it("should pin live previews pin markdown file", async function () {

0 commit comments

Comments
 (0)