MagicMirror/tests/unit/classes/class_spec.js

110 lines
3.1 KiB
JavaScript
Raw Normal View History

2019-06-04 10:43:06 +02:00
const expect = require("chai").expect;
2018-02-16 00:01:02 +01:00
const path = require("path");
const { JSDOM } = require("jsdom");
describe("File js/class", function () {
describe("Test function cloneObject", function () {
2018-02-16 00:08:11 +01:00
let clone;
2018-02-16 08:43:27 +01:00
let dom;
2018-02-16 00:08:11 +01:00
2021-06-06 23:13:09 +02:00
beforeAll(function (done) {
dom = new JSDOM(
`<script>var Log = {log: function() {}};</script>\
<script src="file://${path.join(__dirname, "..", "..", "..", "js", "class.js")}">`,
{ runScripts: "dangerously", resources: "usable" }
);
dom.window.onload = function () {
const { cloneObject } = dom.window;
2018-02-16 00:08:11 +01:00
clone = cloneObject;
done();
};
});
it("should clone object", function () {
const expected = { name: "Rodrigo", web: "https://rodrigoramirez.com", project: "MagicMirror" };
const obj = clone(expected);
expect(obj).to.deep.equal(expected);
expect(expected === obj).to.equal(false);
});
it("should clone array", function () {
const expected = [1, null, undefined, "TEST"];
const obj = clone(expected);
expect(obj).to.deep.equal(expected);
expect(expected === obj).to.equal(false);
2018-02-17 09:20:34 +01:00
});
it("should clone number", function () {
let expected = 1;
let obj = clone(expected);
expect(obj).to.equal(expected);
2018-02-17 09:20:34 +01:00
expected = 1.23;
obj = clone(expected);
expect(obj).to.equal(expected);
});
it("should clone string", function () {
2018-02-16 00:08:11 +01:00
const expected = "Perfect stranger";
const obj = clone(expected);
2018-02-17 09:20:34 +01:00
expect(obj).to.equal(expected);
});
it("should clone undefined", function () {
2018-02-16 00:08:11 +01:00
const expected = undefined;
const obj = clone(expected);
2018-02-17 09:20:34 +01:00
expect(obj).to.equal(expected);
});
it("should clone null", function () {
2018-02-17 09:20:34 +01:00
const expected = null;
const obj = clone(expected);
expect(obj).to.equal(expected);
});
it("should clone nested object", function () {
const expected = {
name: "fewieden",
link: "https://github.com/fewieden",
versions: ["2.0", "2.1", "2.2"],
answerForAllQuestions: 42,
properties: {
items: [{ foo: "bar" }, { lorem: "ipsum" }],
invalid: undefined,
nothing: null
}
};
const obj = clone(expected);
expect(obj).to.deep.equal(expected);
expect(expected === obj).to.equal(false);
expect(expected.versions === obj.versions).to.equal(false);
expect(expected.properties === obj.properties).to.equal(false);
expect(expected.properties.items === obj.properties.items).to.equal(false);
expect(expected.properties.items[0] === obj.properties.items[0]).to.equal(false);
expect(expected.properties.items[1] === obj.properties.items[1]).to.equal(false);
2018-02-17 09:20:34 +01:00
});
describe("Test lockstring code", function () {
2018-02-16 08:43:27 +01:00
let log;
2021-06-06 23:13:09 +02:00
beforeAll(function () {
2018-02-17 09:20:34 +01:00
log = dom.window.Log.log;
dom.window.Log.log = function cmp(str) {
expect(str).to.equal("lockStrings");
};
2018-02-16 08:43:27 +01:00
});
2021-06-06 23:13:09 +02:00
afterAll(function () {
2018-02-17 09:20:34 +01:00
dom.window.Log.log = log;
});
2018-02-16 08:43:27 +01:00
it("should clone object and log lockStrings", function () {
const expected = { name: "Module", lockStrings: "stringLock" };
const obj = clone(expected);
2018-02-17 09:20:34 +01:00
expect(obj).to.deep.equal(expected);
expect(expected === obj).to.equal(false);
});
});
});
});