1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-20 16:45:07 +00:00
serenity/Tests/LibWeb/Text/input/include.js
Sam Atkins 1e6cd19b28 LibWeb: Prevent calling test() twice
Calling test() multiple times in the same test file is not actually
valid, and can cause the following test to hang forever. So let's stop
doing that in the one test that did so, and also prevent the same
mistake happening again. :^)

Throwing an exception on subsequent test() calls means that we don't
hang, the test will fail with missing output, and we get a log message
explaining why.
2023-12-22 16:49:06 +01:00

42 lines
1.1 KiB
JavaScript

var __outputElement = null;
let __alreadyCalledTest = false;
function __preventMultipleTestFunctions() {
if (__alreadyCalledTest) {
throw new Error("You must only call test() or asyncTest() once per page");
}
__alreadyCalledTest = true;
}
if (globalThis.internals === undefined) {
internals = {
signalTextTestIsDone: function () {},
};
}
function println(s) {
__outputElement.appendChild(document.createTextNode(s + "\n"));
}
document.addEventListener("DOMContentLoaded", function () {
__outputElement = document.createElement("pre");
__outputElement.setAttribute("id", "out");
document.body.appendChild(__outputElement);
});
function test(f) {
__preventMultipleTestFunctions();
document.addEventListener("DOMContentLoaded", f);
window.addEventListener("load", () => {
internals.signalTextTestIsDone();
});
}
function asyncTest(f) {
const done = () => {
__preventMultipleTestFunctions();
internals.signalTextTestIsDone();
};
document.addEventListener("DOMContentLoaded", () => {
f(done);
});
}