unit tests

This commit is contained in:
Karsten Hassel
2021-06-06 23:13:09 +02:00
parent 32df76bdff
commit 66759a33fa
12 changed files with 78 additions and 50 deletions

View File

@@ -7,7 +7,7 @@ describe("File js/class", function () {
let clone;
let dom;
before(function (done) {
beforeAll(function (done) {
dom = new JSDOM(
`<script>var Log = {log: function() {}};</script>\
<script src="file://${path.join(__dirname, "..", "..", "..", "js", "class.js")}">`,
@@ -87,14 +87,14 @@ describe("File js/class", function () {
describe("Test lockstring code", function () {
let log;
before(function () {
beforeAll(function () {
log = dom.window.Log.log;
dom.window.Log.log = function cmp(str) {
expect(str).to.equal("lockStrings");
};
});
after(function () {
afterAll(function () {
dom.window.Log.log = log;
});

View File

@@ -3,11 +3,12 @@ const path = require("path");
const helmet = require("helmet");
const { JSDOM } = require("jsdom");
const express = require("express");
const sockets = new Set();
describe("Translator", function () {
let server;
before(function () {
beforeAll(function () {
const app = express();
app.use(helmet());
app.use(function (req, res, next) {
@@ -17,9 +18,20 @@ describe("Translator", function () {
app.use("/translations", express.static(path.join(__dirname, "..", "..", "..", "tests", "configs", "data")));
server = app.listen(3000);
server.on('connection', (socket) => {
sockets.add(socket);
});
});
after(function () {
afterAll(function () {
for (const socket of sockets) {
socket.destroy();
sockets.delete(socket);
}
server.close();
});

View File

@@ -10,7 +10,7 @@ describe("Functions into modules/default/calendar/calendar.js", function () {
Module.definitions[name] = moduleDefinition;
};
before(function () {
beforeAll(function () {
// load calendar.js
require("../../../modules/default/calendar/calendar.js");
});

View File

@@ -5,7 +5,7 @@ const { JSDOM } = require("jsdom");
describe("Test function cmpVersions in js/module.js", function () {
let cmp;
before(function (done) {
beforeAll(function (done) {
const dom = new JSDOM(
`<script>var Class = {extend: function() { return {}; }};</script>\
<script src="file://${path.join(__dirname, "..", "..", "..", "js", "module.js")}">`,

View File

@@ -10,14 +10,14 @@ describe("Functions module currentweather", function () {
Module.definitions[name] = moduleDefinition;
};
before(function () {
beforeAll(function () {
require("../../../modules/default/currentweather/currentweather.js");
Module.definitions.currentweather.config = {};
});
describe("roundValue", function () {
describe("this.config.roundTemp is true", function () {
before(function () {
beforeAll(function () {
Module.definitions.currentweather.config.roundTemp = true;
});
@@ -41,7 +41,7 @@ describe("Functions module currentweather", function () {
});
describe("this.config.roundTemp is false", function () {
before(function () {
beforeAll(function () {
Module.definitions.currentweather.config.roundTemp = false;
});

View File

@@ -8,8 +8,10 @@ describe("Functions into modules/default/newsfeed/newsfeed.js", function () {
Module.definitions[name] = moduleDefinition;
};
before(function () {
beforeAll(function () {
// load newsfeed.js
require("../../../modules/default/newsfeed/newsfeed.js");
});
test.skip('skip', () => {});
});

View File

@@ -4,7 +4,7 @@ const moment = require("moment-timezone");
const data = require("../../configs/data/weatherforecast_data.json");
describe("Functions module weatherforecast", function () {
before(function () {
beforeAll(function () {
Module = {};
config = {};
Module.definitions = {};
@@ -17,7 +17,7 @@ describe("Functions module weatherforecast", function () {
describe("roundValue", function () {
describe("this.config.roundTemp is true", function () {
before(function () {
beforeAll(function () {
Module.definitions.weatherforecast.config.roundTemp = true;
});
@@ -41,7 +41,7 @@ describe("Functions module weatherforecast", function () {
});
describe("this.config.roundTemp is false", function () {
before(function () {
beforeAll(function () {
Module.definitions.weatherforecast.config.roundTemp = false;
});
@@ -73,7 +73,7 @@ describe("Functions module weatherforecast", function () {
let originalLocale;
let originalTimeZone;
before(function () {
beforeAll(function () {
originalLocale = moment.locale();
originalTimeZone = moment.tz.guess();
moment.locale("hi");
@@ -81,7 +81,7 @@ describe("Functions module weatherforecast", function () {
});
describe("forecastIcons sunset specified", function () {
before(function () {
beforeAll(function () {
Module.definitions.weatherforecast.Log = {};
Module.definitions.weatherforecast.forecast = [];
Module.definitions.weatherforecast.show = Module.definitions.weatherforecast.updateDom = function () {};
@@ -89,7 +89,7 @@ describe("Functions module weatherforecast", function () {
});
it(`returns correct icons with sunset time`, function () {
Module.definitions.weatherforecast.processWeather(data.withSunset);
Module.definitions.weatherforecast.processWeather(data.withSunset, moment);
let forecastData = Module.definitions.weatherforecast.forecast;
expect(forecastData.length).to.equal(4);
expect(forecastData[2].icon).to.equal("wi-rain");
@@ -97,19 +97,19 @@ describe("Functions module weatherforecast", function () {
});
describe("forecastIcons sunset not specified", function () {
before(function () {
beforeAll(function () {
Module.definitions.weatherforecast.forecast = [];
});
it(`returns correct icons with out sunset time`, function () {
Module.definitions.weatherforecast.processWeather(data.withoutSunset);
Module.definitions.weatherforecast.processWeather(data.withoutSunset, moment);
let forecastData = Module.definitions.weatherforecast.forecast;
expect(forecastData.length).to.equal(4);
expect(forecastData[2].icon).to.equal("wi-rain");
});
});
after(function () {
afterAll(function () {
moment.locale(originalLocale);
moment.tz.setDefault(originalTimeZone);
});

View File

@@ -5,12 +5,12 @@ const vm = require("vm");
const basedir = path.join(__dirname, "../../..");
before(function () {
beforeAll(function () {
const fileName = "js/app.js";
const filePath = path.join(basedir, fileName);
const code = fs.readFileSync(filePath);
this.sandbox = {
sandbox = {
module: {},
__dirname: path.dirname(filePath),
global: {},
@@ -27,13 +27,13 @@ before(function () {
}
};
this.sandbox.require = function (filename) {
sandbox.require = function (filename) {
// This modifies the global slightly,
// but supplies vm with essential code
return require(filename);
};
vm.runInNewContext(code, this.sandbox, fileName);
vm.runInNewContext(code, sandbox, fileName);
});
describe("Default modules set in modules/default/defaultmodules.js", function () {
@@ -41,7 +41,7 @@ describe("Default modules set in modules/default/defaultmodules.js", function ()
for (const defaultModule of expectedDefaultModules) {
it(`contains a folder for modules/default/${defaultModule}"`, function () {
expect(fs.existsSync(path.join(this.sandbox.global.root_path, "modules/default", defaultModule))).to.equal(true);
expect(fs.existsSync(path.join(sandbox.global.root_path, "modules/default", defaultModule))).to.equal(true);
});
}
});

View File

@@ -3,14 +3,14 @@ const path = require("path");
const expect = require("chai").expect;
const vm = require("vm");
before(function () {
beforeAll(function () {
const basedir = path.join(__dirname, "../../..");
const fileName = "js/app.js";
const filePath = path.join(basedir, fileName);
const code = fs.readFileSync(filePath);
this.sandbox = {
sandbox = {
module: {},
__dirname: path.dirname(filePath),
global: {},
@@ -27,16 +27,16 @@ before(function () {
}
};
this.sandbox.require = function (filename) {
sandbox.require = function (filename) {
// This modifies the global slightly,
// but supplies vm with essential code
return require(filename);
};
vm.runInNewContext(code, this.sandbox, fileName);
vm.runInNewContext(code, sandbox, fileName);
});
after(function () {
afterAll(function () {
//console.log(global);
});
@@ -45,7 +45,7 @@ describe("'global.root_path' set in js/app.js", function () {
expectedSubPaths.forEach((subpath) => {
it(`contains a file/folder "${subpath}"`, function () {
expect(fs.existsSync(path.join(this.sandbox.global.root_path, subpath))).to.equal(true);
expect(fs.existsSync(path.join(sandbox.global.root_path, subpath))).to.equal(true);
});
});
@@ -59,6 +59,6 @@ describe("'global.root_path' set in js/app.js", function () {
it("should expect the global.version equals package.json file", function () {
const versionPackage = JSON.parse(fs.readFileSync("package.json", "utf8")).version;
expect(this.sandbox.global.version).to.equal(versionPackage);
expect(sandbox.global.version).to.equal(versionPackage);
});
});