From c1be0180f967d4da148f895b7f97a2e829aa7965 Mon Sep 17 00:00:00 2001 From: Karsten Hassel Date: Thu, 13 Jan 2022 22:33:57 +0100 Subject: [PATCH] change weather_spec --- tests/e2e/global-setup.js | 6 ++-- tests/e2e/modules/weather_spec.js | 57 ++++++++++++++++--------------- 2 files changed, 31 insertions(+), 32 deletions(-) diff --git a/tests/e2e/global-setup.js b/tests/e2e/global-setup.js index c1bf70bc..3cf8e908 100644 --- a/tests/e2e/global-setup.js +++ b/tests/e2e/global-setup.js @@ -22,16 +22,14 @@ exports.stopApplication = function () { } }; -exports.getDocument = function (callback, ms) { +exports.getDocument = function (callback) { 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(); - }, ms); + callback(); }; }); }; diff --git a/tests/e2e/modules/weather_spec.js b/tests/e2e/modules/weather_spec.js index 56581d25..a62bcd82 100644 --- a/tests/e2e/modules/weather_spec.js +++ b/tests/e2e/modules/weather_spec.js @@ -5,29 +5,20 @@ const fs = require("fs"); const { generateWeather, generateWeatherForecast } = require("./mocks"); describe("Weather module", function () { - /** - * - * @param {string} element css selector - * @returns {Promise} Promise with the element once it is rendered - */ - function getElement(element) { - const elem = document.querySelector(element); - expect(elem).not.toBe(null); - return elem; - } - /** * @param {string} element css selector * @param {string} result Expected text in given selector */ function getText(element, result) { - const elem = getElement(element); - expect( - elem.textContent - .trim() - .replace(/(\r\n|\n|\r)/gm, "") - .replace(/[ ]+/g, " ") - ).toBe(result); + helpers.waitForElement(element).then((elem) => { + expect(elem).not.toBe(null); + expect( + elem.textContent + .trim() + .replace(/(\r\n|\n|\r)/gm, "") + .replace(/[ ]+/g, " ") + ).toBe(result); + }); } /** @@ -46,7 +37,7 @@ describe("Weather module", function () { content = content.replace("#####WEATHERDATA#####", mockWeather); fs.writeFileSync(path.resolve(__dirname + "../../../../config/config.js"), content); helpers.startApplication(""); - helpers.getDocument(callback, 3000); + helpers.getDocument(callback); } afterAll(function () { @@ -117,8 +108,10 @@ describe("Weather module", function () { }); it("should render showWindDirectionAsArrow = true", function () { - const elem = getElement(".weather .normal.medium sup i.fa-long-arrow-alt-up"); - expect(elem.outerHTML).toContain("transform:rotate(250deg);"); + helpers.waitForElement(".weather .normal.medium sup i.fa-long-arrow-alt-up").then((elem) => { + expect(elem).not.toBe(null); + expect(elem.outerHTML).toContain("transform:rotate(250deg);"); + }); }); it("should render showHumidity = true", function () { @@ -180,7 +173,9 @@ describe("Weather module", function () { const icons = ["day-cloudy", "rain", "day-sunny", "day-sunny", "day-sunny"]; for (const [index, icon] of icons.entries()) { - getElement(`.weather table.small tr:nth-child(${index + 1}) td:nth-child(2) span.wi-${icon}`); + helpers.waitForElement(`.weather table.small tr:nth-child(${index + 1}) td:nth-child(2) span.wi-${icon}`).then((elem) => { + expect(elem).not.toBe(null); + }); } }); @@ -204,8 +199,10 @@ describe("Weather module", function () { const opacities = [1, 1, 0.8, 0.5333333333333333, 0.2666666666666667]; for (const [index, opacity] of opacities.entries()) { - const elem = getElement(`.weather table.small tr:nth-child(${index + 1})`); - expect(elem.outerHTML).toContain(``); + helpers.waitForElement(`.weather table.small tr:nth-child(${index + 1})`).then((elem) => { + expect(elem).not.toBe(null); + expect(elem.outerHTML).toContain(``); + }); } }); }); @@ -230,13 +227,17 @@ describe("Weather module", function () { }); it("should render custom table class", function () { - getElement(".weather table.myTableClass"); + helpers.waitForElement(".weather table.myTableClass").then((elem) => { + expect(elem).not.toBe(null); + }); }); it("should render colored rows", function () { - const table = getElement(".weather table.myTableClass"); - expect(table.rows).not.toBe(null); - expect(table.rows.length).toBe(5); + helpers.waitForElement(".weather table.myTableClass").then((elem) => { + expect(elem).not.toBe(null); + expect(table.rows).not.toBe(null); + expect(table.rows.length).toBe(5); + }); }); });