change weather_spec

This commit is contained in:
Karsten Hassel 2022-01-13 22:33:57 +01:00
parent 2cfafe7bfe
commit c1be0180f9
2 changed files with 31 additions and 32 deletions

View File

@ -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"); 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.MutationObserver = dom.window.MutationObserver;
global.document = dom.window.document; global.document = dom.window.document;
setTimeout(() => {
callback(); callback();
}, ms);
}; };
}); });
}; };

View File

@ -5,29 +5,20 @@ const fs = require("fs");
const { generateWeather, generateWeatherForecast } = require("./mocks"); const { generateWeather, generateWeatherForecast } = require("./mocks");
describe("Weather module", function () { describe("Weather module", function () {
/**
*
* @param {string} element css selector
* @returns {Promise<Element>} 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} element css selector
* @param {string} result Expected text in given selector * @param {string} result Expected text in given selector
*/ */
function getText(element, result) { function getText(element, result) {
const elem = getElement(element); helpers.waitForElement(element).then((elem) => {
expect(elem).not.toBe(null);
expect( expect(
elem.textContent elem.textContent
.trim() .trim()
.replace(/(\r\n|\n|\r)/gm, "") .replace(/(\r\n|\n|\r)/gm, "")
.replace(/[ ]+/g, " ") .replace(/[ ]+/g, " ")
).toBe(result); ).toBe(result);
});
} }
/** /**
@ -46,7 +37,7 @@ describe("Weather module", function () {
content = content.replace("#####WEATHERDATA#####", mockWeather); content = content.replace("#####WEATHERDATA#####", mockWeather);
fs.writeFileSync(path.resolve(__dirname + "../../../../config/config.js"), content); fs.writeFileSync(path.resolve(__dirname + "../../../../config/config.js"), content);
helpers.startApplication(""); helpers.startApplication("");
helpers.getDocument(callback, 3000); helpers.getDocument(callback);
} }
afterAll(function () { afterAll(function () {
@ -117,9 +108,11 @@ describe("Weather module", function () {
}); });
it("should render showWindDirectionAsArrow = true", function () { it("should render showWindDirectionAsArrow = true", function () {
const elem = getElement(".weather .normal.medium sup i.fa-long-arrow-alt-up"); 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);"); expect(elem.outerHTML).toContain("transform:rotate(250deg);");
}); });
});
it("should render showHumidity = true", function () { it("should render showHumidity = true", function () {
getText(".weather .normal.medium span:nth-child(3)", "93.7"); getText(".weather .normal.medium span:nth-child(3)", "93.7");
@ -180,7 +173,9 @@ describe("Weather module", function () {
const icons = ["day-cloudy", "rain", "day-sunny", "day-sunny", "day-sunny"]; const icons = ["day-cloudy", "rain", "day-sunny", "day-sunny", "day-sunny"];
for (const [index, icon] of icons.entries()) { 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]; const opacities = [1, 1, 0.8, 0.5333333333333333, 0.2666666666666667];
for (const [index, opacity] of opacities.entries()) { for (const [index, opacity] of opacities.entries()) {
const elem = getElement(`.weather table.small tr:nth-child(${index + 1})`); helpers.waitForElement(`.weather table.small tr:nth-child(${index + 1})`).then((elem) => {
expect(elem).not.toBe(null);
expect(elem.outerHTML).toContain(`<tr style="opacity: ${opacity};">`); expect(elem.outerHTML).toContain(`<tr style="opacity: ${opacity};">`);
});
} }
}); });
}); });
@ -230,15 +227,19 @@ describe("Weather module", function () {
}); });
it("should render custom table class", 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 () { it("should render colored rows", function () {
const table = getElement(".weather table.myTableClass"); helpers.waitForElement(".weather table.myTableClass").then((elem) => {
expect(elem).not.toBe(null);
expect(table.rows).not.toBe(null); expect(table.rows).not.toBe(null);
expect(table.rows.length).toBe(5); expect(table.rows.length).toBe(5);
}); });
}); });
});
describe("Forecast weather units", function () { describe("Forecast weather units", function () {
beforeAll(function (done) { beforeAll(function (done) {