This commit is contained in:
Karsten Hassel 2021-09-23 22:52:32 +02:00
parent 60f8de282d
commit 54b04962a8
5 changed files with 49 additions and 55 deletions

View File

@ -26,6 +26,7 @@ jobs:
Xvfb :99 -screen 0 1024x768x16 & Xvfb :99 -screen 0 1024x768x16 &
export DISPLAY=:99 export DISPLAY=:99
npm install npm install
touch css/custom.css
npm run test:prettier npm run test:prettier
npm run test:js npm run test:js
npm run test:css npm run test:css

View File

@ -1,3 +1,5 @@
const jsdom = require("jsdom");
exports.startApplication = function (configFilename, exec) { exports.startApplication = function (configFilename, exec) {
jest.resetModules(); jest.resetModules();
// Set config sample for use in test // Set config sample for use in test
@ -14,3 +16,12 @@ exports.stopApplication = function (app) {
app.stop(); app.stop();
} }
}; };
exports.getDocument = function (url, callback) {
jsdom.JSDOM.fromURL(url, { resources: "usable", runScripts: "dangerously" }).then((dom) => {
dom.window.onload = function () {
global.document = dom.window.document;
callback();
};
});
};

View File

@ -1,13 +1,15 @@
function myError(err) { function myError(err) {
//console.dir(err); if (err.includes("ECONNREFUSED")) {
if (err.includes("ECONNREFUSED")) { jest.fn() } else { console.dir(err) }; jest.fn();
}; } else {
console.dir(err);
}
}
global.console = { global.console = {
log: jest.fn(), log: jest.fn(),
dir: console.dir, dir: console.dir,
error: myError, error: myError,
// error: console.error,
warn: console.warn, warn: console.warn,
info: jest.fn(), info: jest.fn(),
debug: console.debug debug: console.debug

View File

@ -1,45 +0,0 @@
const jsdom = require("jsdom");
// const fetch = require("node-fetch");
const helpers = require("./global-setup");
let app = null;
describe("Display of modules", function () {
beforeAll(function () {
// todo: require is not defined ...
// jest.mock("logger");
app = helpers.startApplication("tests/configs/modules/display.js");
// app = helpers.startApplication("config/config.js");
});
afterAll(function () {
helpers.stopApplication(app);
});
it("should show the test header", function (done) {
jsdom.JSDOM.fromURL("http://localhost:8080", { resources: "usable", runScripts: "dangerously" }).then((dom) => {
// console.log(dom.serialize());
dom.window.onload = function () {
const doc = dom.window.document;
let elem = doc.querySelector("title");
expect(elem).not.toBe(null);
expect(elem.textContent).toBe("MagicMirror²");
/*
const children = doc.body.getElementsByTagName("*");
for (var i = 0, length = children.length; i < length; i++) {
child = children[i];
if (child.id !== "") console.dir(child.id);
}
*/
elem = doc.querySelector("#module_0_helloworld .module-header");
expect(elem).not.toBe(null);
expect(elem.textContent).toBe("test_header");
//result ist leider lowercase wegen fehlendem css, siehe https://stackoverflow.com/questions/10318330/how-do-you-add-stylesheets-to-jsdom
elem = doc.querySelector("#module_1_helloworld .module-header");
expect(elem).not.toBe(null);
expect(elem.textContent).toBe("undefined");
//dom.window.close();
done();
};
});
});
});

View File

@ -0,0 +1,25 @@
const helpers = require("./global-setup");
let app = null;
describe("Display of modules", function () {
beforeAll(function (done) {
app = helpers.startApplication("tests/configs/modules/display.js");
helpers.getDocument("http://localhost:8080", done);
});
afterAll(function () {
helpers.stopApplication(app);
});
it("should show the test header", function () {
elem = document.querySelector("#module_0_helloworld .module-header");
expect(elem).not.toBe(null);
expect(elem.textContent).toBe("test_header");
//result ist leider lowercase wegen fehlendem css, siehe https://stackoverflow.com/questions/10318330/how-do-you-add-stylesheets-to-jsdom
});
it("should show no header if no header text is specified", function () {
elem = document.querySelector("#module_1_helloworld .module-header");
expect(elem).not.toBe(null);
expect(elem.textContent).toBe("undefined");
});
});