Skip to content

Commit

Permalink
remove callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
khassel committed Oct 2, 2022
1 parent cda2c5f commit 94b1c8c
Show file tree
Hide file tree
Showing 20 changed files with 384 additions and 444 deletions.
27 changes: 12 additions & 15 deletions tests/e2e/env_spec.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
const helpers = require("./global-setup");

describe("App environment", () => {
beforeAll((done) => {
beforeAll(async () => {
helpers.startApplication("tests/configs/default.js");
helpers.getDocument(done);
await helpers.getDocument();
});
afterAll(async () => {
await helpers.stopApplication();
});

it("get request from http://localhost:8080 should return 200", (done) => {
helpers.fetch(done, "http://localhost:8080").then((res) => {
expect(res.status).toBe(200);
});
it("get request from http://localhost:8080 should return 200", async () => {
const res = await helpers.fetch("http://localhost:8080");
expect(res.status).toBe(200);
});

it("get request from http://localhost:8080/nothing should return 404", (done) => {
helpers.fetch(done, "http://localhost:8080/nothing").then((res) => {
expect(res.status).toBe(404);
});
it("get request from http://localhost:8080/nothing should return 404", async () => {
const res = await helpers.fetch("http://localhost:8080/nothing");
expect(res.status).toBe(404);
});

it("should show the title MagicMirror²", (done) => {
helpers.waitForElement(done, "title").then((elem) => {
expect(elem).not.toBe(null);
expect(elem.textContent).toBe("MagicMirror²");
});
it("should show the title MagicMirror²", async () => {
const elem = await helpers.waitForElement("title");
expect(elem).not.toBe(null);
expect(elem.textContent).toBe("MagicMirror²");
});
});
7 changes: 3 additions & 4 deletions tests/e2e/fonts.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ describe("All font files from roboto.css should be downloadable", () => {
await helpers.stopApplication();
});

test.each(fontFiles)("should return 200 HTTP code for file '%s'", (fontFile, done) => {
test.each(fontFiles)("should return 200 HTTP code for file '%s'", async (fontFile) => {
const fontUrl = "http://localhost:8080/fonts/" + fontFile;
helpers.fetch(done, fontUrl).then((res) => {
expect(res.status).toBe(200);
});
const res = await helpers.fetch(fontUrl);
expect(res.status).toBe(200);
});
});
33 changes: 19 additions & 14 deletions tests/e2e/global-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@ exports.stopApplication = async () => {
await new Promise((resolve) => setTimeout(resolve, 100));
};

exports.getDocument = (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 = () => {
global.document = dom.window.document;
callback();
};
exports.getDocument = () => {
return new Promise((resolve) => {
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 = () => {
global.document = dom.window.document;
resolve();
};
});
});
};

exports.waitForElement = (done, selector, ignoreValue = "") => {
exports.waitForElement = (selector, ignoreValue = "") => {
return new Promise((resolve) => {
let oldVal = "dummy12345";
const interval = setInterval(() => {
Expand All @@ -43,7 +45,6 @@ exports.waitForElement = (done, selector, ignoreValue = "") => {
if (newVal === oldVal) {
clearInterval(interval);
resolve(element);
if (done) done();
} else {
if (ignoreValue === "") {
oldVal = newVal;
Expand All @@ -56,7 +57,7 @@ exports.waitForElement = (done, selector, ignoreValue = "") => {
});
};

exports.waitForAllElements = (done, selector) => {
exports.waitForAllElements = (selector) => {
return new Promise((resolve) => {
let oldVal = 999999;
const interval = setInterval(() => {
Expand All @@ -66,7 +67,6 @@ exports.waitForAllElements = (done, selector) => {
if (newVal === oldVal) {
clearInterval(interval);
resolve(element);
if (done) done();
} else {
if (newVal !== 0) oldVal = newVal;
}
Expand All @@ -75,11 +75,16 @@ exports.waitForAllElements = (done, selector) => {
});
};

exports.fetch = (done, url) => {
exports.fetch = (url) => {
return new Promise((resolve) => {
corefetch(url).then((res) => {
done();
resolve(res);
});
});
};

exports.testMatch = async (element, regex) => {
const elem = await this.waitForElement(element);
expect(elem).not.toBe(null);
expect(elem.textContent).toMatch(regex);
};
14 changes: 6 additions & 8 deletions tests/e2e/ipWhitelist_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ describe("ipWhitelist directive configuration", () => {
await helpers.stopApplication();
});

it("should return 403", (done) => {
helpers.fetch(done, "http://localhost:8080").then((res) => {
expect(res.status).toBe(403);
});
it("should return 403", async () => {
const res = await helpers.fetch("http://localhost:8080");
expect(res.status).toBe(403);
});
});

Expand All @@ -24,10 +23,9 @@ describe("ipWhitelist directive configuration", () => {
await helpers.stopApplication();
});

it("should return 200", (done) => {
helpers.fetch(done, "http://localhost:8080").then((res) => {
expect(res.status).toBe(200);
});
it("should return 200", async () => {
const res = await helpers.fetch("http://localhost:8080");
expect(res.status).toBe(200);
});
});
});
13 changes: 6 additions & 7 deletions tests/e2e/modules/alert_spec.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
const helpers = require("../global-setup");

describe("Alert module", () => {
beforeAll((done) => {
beforeAll(async () => {
helpers.startApplication("tests/configs/modules/alert/default.js");
helpers.getDocument(done);
await helpers.getDocument();
});
afterAll(async () => {
await helpers.stopApplication();
});

it("should show the welcome message", (done) => {
helpers.waitForElement(done, ".ns-box .ns-box-inner .light.bright.small").then((elem) => {
expect(elem).not.toBe(null);
expect(elem.textContent).toContain("Welcome, start was successful!");
});
it("should show the welcome message", async () => {
const elem = await helpers.waitForElement(".ns-box .ns-box-inner .light.bright.small");
expect(elem).not.toBe(null);
expect(elem.textContent).toContain("Welcome, start was successful!");
});
});
4 changes: 2 additions & 2 deletions tests/e2e/modules/basic-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ exports.listen = (...args) => {
server = app.listen.apply(app, args);
};

exports.close = (callback) => {
server.close(callback);
exports.close = async () => {
await server.close();
};
Loading

0 comments on commit 94b1c8c

Please sign in to comment.