Skip to content

Commit 34440c5

Browse files
fix: prevent form navigation (#427)
1 parent 97af586 commit 34440c5

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

packages/dom-evaluator/src/dom-test-evaluator.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ const removeTestScripts = () => {
5050
hooksScript?.remove();
5151
};
5252

53+
// Prevent form submissions from navigating the page. If we don't do this, the
54+
// iframe's browsing context could be replaced destroying the test runner.
55+
document.addEventListener("submit", (e) => {
56+
e.preventDefault();
57+
});
58+
5359
export class DOMTestEvaluator implements TestEvaluator {
5460
#runTest?: TestEvaluator["runTest"];
5561
#proxyConsole: ProxyConsole;

packages/tests/integration-tests/index.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,37 @@ assert(mocked.find('.greeting').length === 1);
11151115
});
11161116
},
11171117
);
1118+
1119+
it("should prevent forms from triggering navigation", async () => {
1120+
const source = `<!DOCTYPE html>
1121+
<form>
1122+
<button id="check-btn">Check</button>
1123+
</form>
1124+
`;
1125+
1126+
const result = await page.evaluate(async (source) => {
1127+
const runner = await window.FCCTestRunner.createTestRunner({
1128+
source,
1129+
type: "dom",
1130+
});
1131+
1132+
await runner.runTest(`
1133+
const checkBtn = document.getElementById('check-btn');
1134+
checkBtn.click();`);
1135+
1136+
// Small delay to allow the page to navigate (if it does)
1137+
await new Promise((resolve) => {
1138+
setTimeout(resolve, 100);
1139+
});
1140+
1141+
return runner.runTest(`
1142+
const checkBtn = document.getElementById('check-btn');
1143+
checkBtn.click();
1144+
`);
1145+
}, source);
1146+
1147+
expect(result).toEqual({ pass: true });
1148+
});
11181149
});
11191150

11201151
describe("Javascript evaluator", () => {

0 commit comments

Comments
 (0)