Skip to content

Commit

Permalink
use jest instead of Vow xmppjs#145
Browse files Browse the repository at this point in the history
  • Loading branch information
erossignon committed Jan 27, 2022
1 parent 81d2b62 commit 0ee7a59
Show file tree
Hide file tree
Showing 29 changed files with 6,458 additions and 2,056 deletions.
6 changes: 6 additions & 0 deletions __tests__/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
extends: "../.eslintrc.cjs",
env: {
jest: true,
},
};
17 changes: 17 additions & 0 deletions __tests__/JSONify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import assert from "assert";
import JSONify from "../src/JSONify.js";
import Element from "../src/Element.js";

describe("JSONify", () => {
it("serialize to json", () => {
const e = new Element("e", { foo: 23, bar: 0, nil: null })
.c("f")
.t(1000)
.up();
assert.deepStrictEqual(JSONify(e), {
name: "e",
attrs: { foo: 23, bar: 0, nil: null },
children: [{ name: "f", attrs: {}, children: [1000] }],
});
});
});
80 changes: 80 additions & 0 deletions __tests__/cdata-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import assert from "assert";
import _parse from "../src/parse.js";
import LtxParser from "../src/parsers/ltx.js";
import h from "../src/createElement.js";
import Parser from "../src/Parser.js";

function parse(s) {
return _parse(s, { Parser: LtxParser });
}

function parseChunks(chunks) {
const p = new Parser();

let result = null;
let error = null;

p.on("tree", (tree) => {
result = tree;
});
p.on("error", (e) => {
error = e;
});

for (const chunk of chunks) {
p.write(chunk);
}
p.end();

if (error) {
throw error;
} else {
return result;
}
}

describe("sax_ltx", () => {
describe("CDATA parsing", () => {
it("issue-19: parse CDATA content as text", () => {
const el = parse("<root><![CDATA[Content]]></root>");
assert.strictEqual(el.name, "root");
assert.strictEqual(el.getText(), "Content");
});
it("do not unescape CDATA content", () => {
const el = parse(
'<root><![CDATA[Content &amp; "more content&quot;]]></root>'
);
assert.strictEqual(el.name, "root");
assert.strictEqual(el.getText(), 'Content &amp; "more content&quot;');
});
it("issue-132", () => {
const el = parse(
"<a><b><![CDATA[]]></b><b><![CDATA[--><c>&d;]]></b></a>"
);
assert.deepStrictEqual(
el,
h("a", null, h("b", null, ""), h("b", null, "--><c>&d;"))
);
});
it("split CDATA between chunks", () => {
const el1 = parseChunks(["<root><![CDA", "TA[Content]]></root>"]);
assert.strictEqual(el1.getText(), "Content");
const el2 = parseChunks(["<root><![CDATA[Con", "tent]]></root>"]);
assert.strictEqual(el2.getText(), "Content");
const el3 = parseChunks(["<root><![CDATA[Content]]", "></root>"]);
assert.strictEqual(el3.getText(), "Content");

const str = "<root><![CDATA[Content]]></root>";
for (let i = 0; i < str.length; i++) {
const chunks = [
str.slice(0, Math.max(0, i)) || "",
str.slice(Math.max(0, i)) || "",
];
assert.strictEqual(
parseChunks(chunks).toString(),
"<root>Content</root>"
);
}
});
});
});
62 changes: 62 additions & 0 deletions __tests__/clone-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import assert from "assert";
import _clone from "../src/clone.js";
import Element from "../src/Element.js";

describe("clone", () => {
it("clones", () => {
const orig = new Element("msg", { type: "get" })
.c("content")
.t("foo")
.root();
const clone = _clone(orig);
assert.strictEqual(clone.name, orig.name);
assert.strictEqual(clone.attrs.type, orig.attrs.type);
assert.strictEqual(clone.attrs.to, orig.attrs.to);
assert.strictEqual(clone.children.length, orig.children.length);
assert.strictEqual(
clone.getChildText("content"),
orig.getChildText("content")
);

assert.strictEqual(orig.getChild("content").up(), orig);
assert.strictEqual(clone.getChild("content").up(), clone);
});
it("mod attr", () => {
const orig = new Element("msg", { type: "get" });
const clone = _clone(orig);
clone.attrs.type += "-result";

assert.strictEqual(orig.attrs.type, "get");
assert.strictEqual(clone.attrs.type, "get-result");
});
it("rm attr", () => {
const orig = new Element("msg", { from: "me" });
const clone = _clone(orig);
delete clone.attrs.from;
clone.attrs.to = "you";

assert.strictEqual(orig.attrs.from, "me");
assert.strictEqual(orig.attrs.to, undefined);
assert.strictEqual(clone.attrs.from, undefined);
assert.strictEqual(clone.attrs.to, "you");
});
it("mod child", () => {
const orig = new Element("msg", { type: "get" })
.c("content")
.t("foo")
.root();
const clone = _clone(orig);
clone.getChild("content").t("bar").name = "description";

assert.strictEqual(orig.children[0].name, "content");
assert.strictEqual(orig.getChildText("content"), "foo");
assert.strictEqual(clone.children[0].name, "description");
assert.strictEqual(clone.getChildText("description"), "foobar");
});
it("use original constructor for the clone", () => {
class Foo extends Element {}
const foo = new Foo();
assert(_clone(foo) instanceof Element);
assert(_clone(foo) instanceof Foo);
});
});
69 changes: 69 additions & 0 deletions __tests__/createElement-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import assert from "assert";
import Element from "../src/Element.js";
import createElement from "../src/createElement.js";

describe("createElement", () => {
it("create a new element and set children", () => {
const c = new Element("bar");
const e = createElement("foo", { foo: "bar" }, "foo", c);
assert(e instanceof Element);
assert(e.is("foo"));
assert.strictEqual(e.attrs.foo, "bar");
assert.strictEqual(e.children.length, 2);
assert.strictEqual(e.children[0], "foo");
assert.strictEqual(e.children[1], c);
});
it("null and undefined children are discarded", () => {
const e = createElement(
"foo",
null,
undefined,
"bar",
null,
createElement("test"),
"baz"
);
const b = new Element("foo").t("bar").c("test").up().t("baz");
assert.strictEqual(e.root().toString(), b.root().toString());
});
it("boolean children are discarded", () => {
assert.deepEqual(
createElement("foo", null, true, false),
new Element("foo")
);
});
it("falsy numbers", () => {
assert.deepEqual(
createElement("foo", null, -1, 0, 1).toString(),
"<foo>-101</foo>"
);
});
it("__source and __self attributes are discarded", () => {
const e = createElement("foo", {
__self: "foo",
__source: "bar",
foo: "bar",
});
assert.equal(e.attrs.__self, undefined);
assert.equal(e.attrs.__source, undefined);
assert.equal(e.attrs.foo, "bar");
});
it("children array", () => {
assert.deepEqual(
createElement("bar", null, "foo", [
"foo",
["foo", ["foo"]],
"foo",
]).toString(),
"<bar>foofoofoofoofoo</bar>"
);
});
it("null and undefined attributes are discarded", () => {
const e = createElement("foo", { foo: null, bar: undefined });
assert.deepEqual(e.attrs, {});
});
it("number attribute are converted to string", () => {
const e = createElement("foo", { foo: 1 });
assert.deepEqual(e.attrs, { foo: "1" });
});
});
50 changes: 50 additions & 0 deletions __tests__/dom-element-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import assert from "assert";
import parsers from "../src/parsers.js";
import DOMElement from "../src/DOMElement.js";
import _parse from "../src/parse.js";

for (const Parser of parsers) {
// eslint-disable-next-line no-inner-declarations
function parse(s) {
return _parse(s, { Parser: Parser, Element: DOMElement });
}
describe("Parsing returns DOMElement's", () => {
it("Returns DOMElement on parse", () => {
const stanza =
'<message><body xmlns="http://www.w3.org/1999/xhtml">' +
"<p>DOM</p></body></message>";
const el = parse(stanza);

assert(el.getChild("body") instanceof DOMElement);
assert.strictEqual(el.getChild("body").constructor.name, "DOMElement");
const body = el.getChild("body");
expect(body.localName).toBeDefined();
assert.strictEqual(body.localName, "body");

expect(body.namespaceURI).toBeDefined();
assert.strictEqual(body.namespaceURI, "http://www.w3.org/1999/xhtml");

expect(body.parentNode).toBeDefined();
assert.strictEqual(body.parentNode.getName(), "message");

expect(body.childNodes).toBeDefined();
expect(body.childNodes).toBeInstanceOf(Array);
assert.strictEqual(body.childNodes.length, 1);

expect(body.textContent).toBeDefined();
assert.strictEqual(body.textContent, "");

assert.strictEqual(body.getChild("p").textContent, "DOM");
});
it("createElement: create a new element and set children", () => {
const c = new DOMElement("bar");
const e = DOMElement.createElement("foo", { foo: "bar" }, "foo", c);
assert(e instanceof DOMElement);
assert.strictEqual(e.localName, "foo");
assert.strictEqual(e.getAttribute("foo"), "bar");
assert.strictEqual(e.childNodes.length, 2);
assert.strictEqual(e.childNodes[0], "foo");
assert.strictEqual(e.childNodes[1], c);
});
});
}
Loading

0 comments on commit 0ee7a59

Please sign in to comment.