Convert done callbacks to async/await in test/unit/ui_utils_spec.js

This commit is contained in:
Tim van der Meij 2021-04-11 19:51:21 +02:00
parent a2811e925d
commit a56ffb92be
No known key found for this signature in database
GPG Key ID: 8C3FD2925A5F2762

View File

@ -178,9 +178,9 @@ describe("ui_utils", function () {
expect(onceCount).toEqual(1); expect(onceCount).toEqual(1);
}); });
it("should not re-dispatch to DOM", function (done) { it("should not re-dispatch to DOM", async function () {
if (isNodeJS) { if (isNodeJS) {
pending("Document in not supported in Node.js."); pending("Document is not supported in Node.js.");
} }
const eventBus = new EventBus(); const eventBus = new EventBus();
let count = 0; let count = 0;
@ -189,18 +189,17 @@ describe("ui_utils", function () {
count++; count++;
}); });
function domEventListener() { function domEventListener() {
done.fail("shall not dispatch DOM event."); // Shouldn't get here.
expect(false).toEqual(true);
} }
document.addEventListener("test", domEventListener); document.addEventListener("test", domEventListener);
eventBus.dispatch("test"); eventBus.dispatch("test");
Promise.resolve().then(() => { await Promise.resolve();
expect(count).toEqual(1); expect(count).toEqual(1);
document.removeEventListener("test", domEventListener); document.removeEventListener("test", domEventListener);
done();
});
}); });
}); });
@ -265,13 +264,14 @@ describe("ui_utils", function () {
eventBus = null; eventBus = null;
}); });
it("should reject invalid parameters", function (done) { it("should reject invalid parameters", async function () {
const invalidTarget = waitOnEventOrTimeout({ const invalidTarget = waitOnEventOrTimeout({
target: "window", target: "window",
name: "DOMContentLoaded", name: "DOMContentLoaded",
}).then( }).then(
function () { function () {
throw new Error("Should reject invalid parameters."); // Shouldn't get here.
expect(false).toEqual(true);
}, },
function (reason) { function (reason) {
expect(reason instanceof Error).toEqual(true); expect(reason instanceof Error).toEqual(true);
@ -283,7 +283,8 @@ describe("ui_utils", function () {
name: "", name: "",
}).then( }).then(
function () { function () {
throw new Error("Should reject invalid parameters."); // Shouldn't get here.
expect(false).toEqual(true);
}, },
function (reason) { function (reason) {
expect(reason instanceof Error).toEqual(true); expect(reason instanceof Error).toEqual(true);
@ -296,22 +297,20 @@ describe("ui_utils", function () {
delay: -1000, delay: -1000,
}).then( }).then(
function () { function () {
throw new Error("Should reject invalid parameters."); // Shouldn't get here.
expect(false).toEqual(true);
}, },
function (reason) { function (reason) {
expect(reason instanceof Error).toEqual(true); expect(reason instanceof Error).toEqual(true);
} }
); );
Promise.all([invalidTarget, invalidName, invalidDelay]).then( await Promise.all([invalidTarget, invalidName, invalidDelay]);
done,
done.fail
);
}); });
it("should resolve on event, using the DOM", function (done) { it("should resolve on event, using the DOM", async function () {
if (isNodeJS) { if (isNodeJS) {
pending("Document in not supported in Node.js."); pending("Document is not supported in Node.js.");
} }
const button = document.createElement("button"); const button = document.createElement("button");
@ -323,15 +322,13 @@ describe("ui_utils", function () {
// Immediately dispatch the expected event. // Immediately dispatch the expected event.
button.click(); button.click();
buttonClicked.then(function (type) { const type = await buttonClicked;
expect(type).toEqual(WaitOnType.EVENT); expect(type).toEqual(WaitOnType.EVENT);
done();
}, done.fail);
}); });
it("should resolve on timeout, using the DOM", function (done) { it("should resolve on timeout, using the DOM", async function () {
if (isNodeJS) { if (isNodeJS) {
pending("Document in not supported in Node.js."); pending("Document is not supported in Node.js.");
} }
const button = document.createElement("button"); const button = document.createElement("button");
@ -342,13 +339,11 @@ describe("ui_utils", function () {
}); });
// Do *not* dispatch the event, and wait for the timeout. // Do *not* dispatch the event, and wait for the timeout.
buttonClicked.then(function (type) { const type = await buttonClicked;
expect(type).toEqual(WaitOnType.TIMEOUT); expect(type).toEqual(WaitOnType.TIMEOUT);
done();
}, done.fail);
}); });
it("should resolve on event, using the EventBus", function (done) { it("should resolve on event, using the EventBus", async function () {
const pageRendered = waitOnEventOrTimeout({ const pageRendered = waitOnEventOrTimeout({
target: eventBus, target: eventBus,
name: "pagerendered", name: "pagerendered",
@ -357,13 +352,11 @@ describe("ui_utils", function () {
// Immediately dispatch the expected event. // Immediately dispatch the expected event.
eventBus.dispatch("pagerendered"); eventBus.dispatch("pagerendered");
pageRendered.then(function (type) { const type = await pageRendered;
expect(type).toEqual(WaitOnType.EVENT); expect(type).toEqual(WaitOnType.EVENT);
done();
}, done.fail);
}); });
it("should resolve on timeout, using the EventBus", function (done) { it("should resolve on timeout, using the EventBus", async function () {
const pageRendered = waitOnEventOrTimeout({ const pageRendered = waitOnEventOrTimeout({
target: eventBus, target: eventBus,
name: "pagerendered", name: "pagerendered",
@ -371,10 +364,8 @@ describe("ui_utils", function () {
}); });
// Do *not* dispatch the event, and wait for the timeout. // Do *not* dispatch the event, and wait for the timeout.
pageRendered.then(function (type) { const type = await pageRendered;
expect(type).toEqual(WaitOnType.TIMEOUT); expect(type).toEqual(WaitOnType.TIMEOUT);
done();
}, done.fail);
}); });
}); });