From 42b80b18f8e9358f332d11353df1bf27829a23de Mon Sep 17 00:00:00 2001 From: Karsten Hassel Date: Thu, 13 Jan 2022 00:13:29 +0100 Subject: [PATCH] testing wait alternatives --- tests/e2e/global-setup.js | 22 ++++++++++++++++ tests/e2e/modules/calendar_spec.js | 41 ++++++++++++++++-------------- tests/e2e/modules/clock_spec.js | 14 +++++----- tests/e2e/without_modules.js | 2 +- 4 files changed, 52 insertions(+), 27 deletions(-) diff --git a/tests/e2e/global-setup.js b/tests/e2e/global-setup.js index bb9b1a2e..19a1c6c1 100644 --- a/tests/e2e/global-setup.js +++ b/tests/e2e/global-setup.js @@ -23,10 +23,12 @@ exports.stopApplication = function () { }; exports.getDocument = function (callback, ms) { + if (!ms || ms < 1000) ms = 1000; const url = "http://" + (config.address || "localhost") + ":" + (config.port || "8080"); jsdom.JSDOM.fromURL(url, { resources: "usable", runScripts: "dangerously" }).then((dom) => { dom.window.name = "jsdom"; dom.window.onload = function () { + global.MutationObserver = dom.window.MutationObserver; global.document = dom.window.document; setTimeout(() => { 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 + }); + }); +}; diff --git a/tests/e2e/modules/calendar_spec.js b/tests/e2e/modules/calendar_spec.js index 8fb803d7..9f3518cb 100644 --- a/tests/e2e/modules/calendar_spec.js +++ b/tests/e2e/modules/calendar_spec.js @@ -9,13 +9,20 @@ describe("Calendar module", function () { * @param {string} not reverse result */ 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); - if (not === "not") { - expect(elem.length).not.toBe(result); - } else { - expect(elem.length).toBe(result); - } + expect(elem.textContent).toContain(text); } afterAll(function () { @@ -25,7 +32,7 @@ describe("Calendar module", function () { describe("Default configuration", function () { beforeAll(function (done) { helpers.startApplication("tests/configs/modules/calendar/default.js"); - helpers.getDocument(done, testDelay); + helpers.getDocument(done); }); it("should show the default maximumEntries of 10", () => { @@ -40,7 +47,7 @@ describe("Calendar module", function () { describe("Custom configuration", function () { beforeAll(function (done) { helpers.startApplication("tests/configs/modules/calendar/custom.js"); - helpers.getDocument(done, testDelay); + helpers.getDocument(done); }); it("should show the custom maximumEntries of 4", () => { @@ -63,7 +70,7 @@ describe("Calendar module", function () { describe("Recurring event", function () { beforeAll(function (done) { helpers.startApplication("tests/configs/modules/calendar/recurring.js"); - helpers.getDocument(done, testDelay); + helpers.getDocument(done); }); 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, () => { - const elem = document.querySelector(".calendar"); - expect(elem).not.toBe(null); - expect(elem.textContent).toContain("Mar 25th"); + testTextContain(".calendar", "Mar 25th"); }); }); } @@ -94,7 +99,7 @@ describe("Calendar module", function () { beforeAll(function (done) { helpers.startApplication("tests/configs/modules/calendar/changed-port.js"); serverBasicAuth.listen(8010); - helpers.getDocument(done, testDelay); + helpers.getDocument(done); }); afterAll(function (done) { @@ -109,7 +114,7 @@ describe("Calendar module", function () { describe("Basic auth", function () { beforeAll(function (done) { helpers.startApplication("tests/configs/modules/calendar/basic-auth.js"); - helpers.getDocument(done, testDelay); + helpers.getDocument(done); }); it("should return TestEvents", function () { @@ -120,7 +125,7 @@ describe("Calendar module", function () { describe("Basic auth by default", function () { beforeAll(function (done) { helpers.startApplication("tests/configs/modules/calendar/auth-default.js"); - helpers.getDocument(done, testDelay); + helpers.getDocument(done); }); it("should return TestEvents", function () { @@ -131,7 +136,7 @@ describe("Calendar module", function () { describe("Basic auth backward compatibility configuration: DEPRECATED", function () { beforeAll(function (done) { helpers.startApplication("tests/configs/modules/calendar/old-basic-auth.js"); - helpers.getDocument(done, testDelay); + helpers.getDocument(done); }); it("should return TestEvents", function () { @@ -151,9 +156,7 @@ describe("Calendar module", function () { }); it("should show Unauthorized error", function () { - const elem = document.querySelector(".calendar"); - expect(elem).not.toBe(null); - expect(elem.textContent).toContain("Error in the calendar module. Authorization failed"); + testTextContain(".calendar", "Error in the calendar module. Authorization failed"); }); }); }); diff --git a/tests/e2e/modules/clock_spec.js b/tests/e2e/modules/clock_spec.js index 79e26f47..0b15f75f 100644 --- a/tests/e2e/modules/clock_spec.js +++ b/tests/e2e/modules/clock_spec.js @@ -15,7 +15,7 @@ describe("Clock module", function () { describe("with default 24hr clock config", function () { beforeAll(function (done) { 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 () { @@ -32,7 +32,7 @@ describe("Clock module", function () { describe("with default 12hr clock config", function () { beforeAll(function (done) { 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 () { @@ -49,7 +49,7 @@ describe("Clock module", function () { describe("with showPeriodUpper config enabled", function () { beforeAll(function (done) { 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 () { @@ -61,7 +61,7 @@ describe("Clock module", function () { describe("with displaySeconds config disabled", function () { beforeAll(function (done) { 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 () { @@ -73,7 +73,7 @@ describe("Clock module", function () { describe("with showTime config disabled", function () { beforeAll(function (done) { 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 () { @@ -85,7 +85,7 @@ describe("Clock module", function () { describe("with showWeek config enabled", function () { beforeAll(function (done) { 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 () { @@ -105,7 +105,7 @@ describe("Clock module", function () { describe("with analog clock face enabled", function () { beforeAll(function (done) { helpers.startApplication("tests/configs/modules/clock/clock_analog.js"); - helpers.getDocument(done, 1000); + helpers.getDocument(done); }); it("should show the analog clock face", () => { diff --git a/tests/e2e/without_modules.js b/tests/e2e/without_modules.js index 8aeeee28..1227b6c5 100644 --- a/tests/e2e/without_modules.js +++ b/tests/e2e/without_modules.js @@ -3,7 +3,7 @@ const helpers = require("./global-setup"); describe("Check configuration without modules", function () { beforeAll(function (done) { helpers.startApplication("tests/configs/without_modules.js"); - helpers.getDocument(done, 1000); + helpers.getDocument(done); }); afterAll(function () { helpers.stopApplication();