testing wait alternatives

This commit is contained in:
Karsten Hassel 2022-01-13 00:13:29 +01:00
parent 65fada7ef1
commit 42b80b18f8
4 changed files with 52 additions and 27 deletions

View File

@ -23,10 +23,12 @@ exports.stopApplication = function () {
}; };
exports.getDocument = function (callback, ms) { exports.getDocument = function (callback, ms) {
if (!ms || ms < 1000) ms = 1000;
const url = "http://" + (config.address || "localhost") + ":" + (config.port || "8080"); const url = "http://" + (config.address || "localhost") + ":" + (config.port || "8080");
jsdom.JSDOM.fromURL(url, { resources: "usable", runScripts: "dangerously" }).then((dom) => { jsdom.JSDOM.fromURL(url, { resources: "usable", runScripts: "dangerously" }).then((dom) => {
dom.window.name = "jsdom"; dom.window.name = "jsdom";
dom.window.onload = function () { dom.window.onload = function () {
global.MutationObserver = dom.window.MutationObserver;
global.document = dom.window.document; global.document = dom.window.document;
setTimeout(() => { setTimeout(() => {
callback(); callback();
@ -34,3 +36,23 @@ exports.getDocument = function (callback, ms) {
}; };
}); });
}; };
exports.waitForElement = function(selector) {
return new Promise(resolve => {
if (document.querySelector(selector)) {
return resolve(document.querySelector(selector));
}
const observer = new MutationObserver(() => {
if (document.querySelector(selector)) {
resolve(document.querySelector(selector));
observer.disconnect();
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
});
};

View File

@ -9,13 +9,20 @@ describe("Calendar module", function () {
* @param {string} not reverse result * @param {string} not reverse result
*/ */
function testElementLength(element, result, not) { function testElementLength(element, result, not) {
const elem = document.querySelectorAll(element); helpers.waitForElement(element).then((elem) => {
expect(elem).not.toBe(null);
if (not === "not") {
expect(elem.length).not.toBe(result);
} else {
expect(elem.length).toBe(result);
}
})
}
const testTextContain = function (element, text) {
const elem = document.querySelector(".calendar");
expect(elem).not.toBe(null); expect(elem).not.toBe(null);
if (not === "not") { expect(elem.textContent).toContain(text);
expect(elem.length).not.toBe(result);
} else {
expect(elem.length).toBe(result);
}
} }
afterAll(function () { afterAll(function () {
@ -25,7 +32,7 @@ describe("Calendar module", function () {
describe("Default configuration", function () { describe("Default configuration", function () {
beforeAll(function (done) { beforeAll(function (done) {
helpers.startApplication("tests/configs/modules/calendar/default.js"); helpers.startApplication("tests/configs/modules/calendar/default.js");
helpers.getDocument(done, testDelay); helpers.getDocument(done);
}); });
it("should show the default maximumEntries of 10", () => { it("should show the default maximumEntries of 10", () => {
@ -40,7 +47,7 @@ describe("Calendar module", function () {
describe("Custom configuration", function () { describe("Custom configuration", function () {
beforeAll(function (done) { beforeAll(function (done) {
helpers.startApplication("tests/configs/modules/calendar/custom.js"); helpers.startApplication("tests/configs/modules/calendar/custom.js");
helpers.getDocument(done, testDelay); helpers.getDocument(done);
}); });
it("should show the custom maximumEntries of 4", () => { it("should show the custom maximumEntries of 4", () => {
@ -63,7 +70,7 @@ describe("Calendar module", function () {
describe("Recurring event", function () { describe("Recurring event", function () {
beforeAll(function (done) { beforeAll(function (done) {
helpers.startApplication("tests/configs/modules/calendar/recurring.js"); helpers.startApplication("tests/configs/modules/calendar/recurring.js");
helpers.getDocument(done, testDelay); helpers.getDocument(done);
}); });
it("should show the recurring birthday event 6 times", () => { it("should show the recurring birthday event 6 times", () => {
@ -83,9 +90,7 @@ describe("Calendar module", function () {
}); });
it('should contain text "Mar 25th" in timezone UTC ' + -i, () => { it('should contain text "Mar 25th" in timezone UTC ' + -i, () => {
const elem = document.querySelector(".calendar"); testTextContain(".calendar", "Mar 25th");
expect(elem).not.toBe(null);
expect(elem.textContent).toContain("Mar 25th");
}); });
}); });
} }
@ -94,7 +99,7 @@ describe("Calendar module", function () {
beforeAll(function (done) { beforeAll(function (done) {
helpers.startApplication("tests/configs/modules/calendar/changed-port.js"); helpers.startApplication("tests/configs/modules/calendar/changed-port.js");
serverBasicAuth.listen(8010); serverBasicAuth.listen(8010);
helpers.getDocument(done, testDelay); helpers.getDocument(done);
}); });
afterAll(function (done) { afterAll(function (done) {
@ -109,7 +114,7 @@ describe("Calendar module", function () {
describe("Basic auth", function () { describe("Basic auth", function () {
beforeAll(function (done) { beforeAll(function (done) {
helpers.startApplication("tests/configs/modules/calendar/basic-auth.js"); helpers.startApplication("tests/configs/modules/calendar/basic-auth.js");
helpers.getDocument(done, testDelay); helpers.getDocument(done);
}); });
it("should return TestEvents", function () { it("should return TestEvents", function () {
@ -120,7 +125,7 @@ describe("Calendar module", function () {
describe("Basic auth by default", function () { describe("Basic auth by default", function () {
beforeAll(function (done) { beforeAll(function (done) {
helpers.startApplication("tests/configs/modules/calendar/auth-default.js"); helpers.startApplication("tests/configs/modules/calendar/auth-default.js");
helpers.getDocument(done, testDelay); helpers.getDocument(done);
}); });
it("should return TestEvents", function () { it("should return TestEvents", function () {
@ -131,7 +136,7 @@ describe("Calendar module", function () {
describe("Basic auth backward compatibility configuration: DEPRECATED", function () { describe("Basic auth backward compatibility configuration: DEPRECATED", function () {
beforeAll(function (done) { beforeAll(function (done) {
helpers.startApplication("tests/configs/modules/calendar/old-basic-auth.js"); helpers.startApplication("tests/configs/modules/calendar/old-basic-auth.js");
helpers.getDocument(done, testDelay); helpers.getDocument(done);
}); });
it("should return TestEvents", function () { it("should return TestEvents", function () {
@ -151,9 +156,7 @@ describe("Calendar module", function () {
}); });
it("should show Unauthorized error", function () { it("should show Unauthorized error", function () {
const elem = document.querySelector(".calendar"); testTextContain(".calendar", "Error in the calendar module. Authorization failed");
expect(elem).not.toBe(null);
expect(elem.textContent).toContain("Error in the calendar module. Authorization failed");
}); });
}); });
}); });

View File

@ -15,7 +15,7 @@ describe("Clock module", function () {
describe("with default 24hr clock config", function () { describe("with default 24hr clock config", function () {
beforeAll(function (done) { beforeAll(function (done) {
helpers.startApplication("tests/configs/modules/clock/clock_24hr.js"); helpers.startApplication("tests/configs/modules/clock/clock_24hr.js");
helpers.getDocument(done, 1000); helpers.getDocument(done);
}); });
it("should show the date in the correct format", function () { it("should show the date in the correct format", function () {
@ -32,7 +32,7 @@ describe("Clock module", function () {
describe("with default 12hr clock config", function () { describe("with default 12hr clock config", function () {
beforeAll(function (done) { beforeAll(function (done) {
helpers.startApplication("tests/configs/modules/clock/clock_12hr.js"); helpers.startApplication("tests/configs/modules/clock/clock_12hr.js");
helpers.getDocument(done, 1000); helpers.getDocument(done);
}); });
it("should show the date in the correct format", function () { it("should show the date in the correct format", function () {
@ -49,7 +49,7 @@ describe("Clock module", function () {
describe("with showPeriodUpper config enabled", function () { describe("with showPeriodUpper config enabled", function () {
beforeAll(function (done) { beforeAll(function (done) {
helpers.startApplication("tests/configs/modules/clock/clock_showPeriodUpper.js"); helpers.startApplication("tests/configs/modules/clock/clock_showPeriodUpper.js");
helpers.getDocument(done, 1000); helpers.getDocument(done);
}); });
it("should show 12hr time with upper case AM/PM", function () { it("should show 12hr time with upper case AM/PM", function () {
@ -61,7 +61,7 @@ describe("Clock module", function () {
describe("with displaySeconds config disabled", function () { describe("with displaySeconds config disabled", function () {
beforeAll(function (done) { beforeAll(function (done) {
helpers.startApplication("tests/configs/modules/clock/clock_displaySeconds_false.js"); helpers.startApplication("tests/configs/modules/clock/clock_displaySeconds_false.js");
helpers.getDocument(done, 1000); helpers.getDocument(done);
}); });
it("should show 12hr time without seconds am/pm", function () { it("should show 12hr time without seconds am/pm", function () {
@ -73,7 +73,7 @@ describe("Clock module", function () {
describe("with showTime config disabled", function () { describe("with showTime config disabled", function () {
beforeAll(function (done) { beforeAll(function (done) {
helpers.startApplication("tests/configs/modules/clock/clock_showTime.js"); helpers.startApplication("tests/configs/modules/clock/clock_showTime.js");
helpers.getDocument(done, 1000); helpers.getDocument(done);
}); });
it("should show not show the time when digital clock is shown", function () { it("should show not show the time when digital clock is shown", function () {
@ -85,7 +85,7 @@ describe("Clock module", function () {
describe("with showWeek config enabled", function () { describe("with showWeek config enabled", function () {
beforeAll(function (done) { beforeAll(function (done) {
helpers.startApplication("tests/configs/modules/clock/clock_showWeek.js"); helpers.startApplication("tests/configs/modules/clock/clock_showWeek.js");
helpers.getDocument(done, 1000); helpers.getDocument(done);
}); });
it("should show the week in the correct format", function () { it("should show the week in the correct format", function () {
@ -105,7 +105,7 @@ describe("Clock module", function () {
describe("with analog clock face enabled", function () { describe("with analog clock face enabled", function () {
beforeAll(function (done) { beforeAll(function (done) {
helpers.startApplication("tests/configs/modules/clock/clock_analog.js"); helpers.startApplication("tests/configs/modules/clock/clock_analog.js");
helpers.getDocument(done, 1000); helpers.getDocument(done);
}); });
it("should show the analog clock face", () => { it("should show the analog clock face", () => {

View File

@ -3,7 +3,7 @@ const helpers = require("./global-setup");
describe("Check configuration without modules", function () { describe("Check configuration without modules", function () {
beforeAll(function (done) { beforeAll(function (done) {
helpers.startApplication("tests/configs/without_modules.js"); helpers.startApplication("tests/configs/without_modules.js");
helpers.getDocument(done, 1000); helpers.getDocument(done);
}); });
afterAll(function () { afterAll(function () {
helpers.stopApplication(); helpers.stopApplication();