Skip to content

Commit

Permalink
Add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
fb55 committed Apr 29, 2023
1 parent 7bac6eb commit 43b95d2
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 11 deletions.
36 changes: 36 additions & 0 deletions src/legacy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ describe("legacy", () => {
expect(getElements({ id: "asdfs" }, fixture, true)).toHaveLength(
0
));
it("returns the node for a ID function", () =>
expect(
getElements({ id: (id) => id === "asdf" }, fixture, true, 1)
).toEqual([expected.idAsdf]));
it("returns the nodes with the specified tag name", () =>
expect(getElements({ tag_name: "tag2" }, fixture, true)).toEqual(
expected.tag2
Expand All @@ -40,6 +44,18 @@ describe("legacy", () => {
expect(
getElements({ tag_name: "asdfs" }, fixture, true)
).toHaveLength(0));
it("returns all elements for wildcard tag names", () =>
expect(getElements({ tag_name: "*" }, fixture, true)).toHaveLength(
60
));
it("returns the nodes with the specified tag name function", () =>
expect(
getElements(
{ tag_name: (name) => name === "tag2" },
fixture,
true
)
).toEqual(expected.tag2));
it("returns the nodes with the specified tag type", () =>
expect(getElements({ tag_type: "script" }, fixture, true)).toEqual(
expected.typeScript
Expand All @@ -48,6 +64,26 @@ describe("legacy", () => {
expect(
getElements({ tag_type: "video" }, fixture, true)
).toHaveLength(0));
it("returns the nodes with the specified tag type function", () =>
expect(
getElements(
{ tag_type: (type) => type === "script" },
fixture,
true
)
).toEqual(expected.typeScript));
it("returns elements for contains", () =>
expect(
getElements({ tag_contains: "text" }, fixture, true)
).toHaveLength(20));
it("returns elements for contains function", () =>
expect(
getElements(
{ tag_contains: (text) => text === "text" },
fixture,
true
)
).toHaveLength(20));
});

describe("getElementById", () => {
Expand Down
60 changes: 49 additions & 11 deletions src/traversal.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { parseDOM } from "htmlparser2";
import type { Element } from "domhandler";
import {
getAttributeValue,
getName,
getSiblings,
hasAttrib,
nextElementSibling,
Expand All @@ -18,25 +20,36 @@ describe("traversal", () => {
it("returns a root element's siblings", () => {
const dom = parseDOM("<h1></h1><p><p><p>") as Element[];

for (const node of dom) {
node.parent = null;
}

expect(getSiblings(dom[2])).toHaveLength(4);
});
});

describe("hasAttrib", () => {
it("doesn't throw on text nodes", () => {
const [firstNode] = parseDOM("textnode");

it("doesn't throw on text nodes", () =>
expect(() =>
hasAttrib(firstNode as never, "some-attrib")
).not.toThrow();
});
hasAttrib(parseDOM("textnode")[0] as never, "some-attrib")
).not.toThrow());

it("returns `false` for Object prototype properties", () => {
const dom = parseDOM(
"<div><h1></h1>test<p></p></div>"
)[0] as Element;
it("returns `false` for Object prototype properties", () =>
expect(
hasAttrib(
parseDOM("<div><h1></h1>test<p></p></div>")[0] as Element,
"constructor"
)
).toBeFalsy());

it('should return `false` for "null" values', () => {
const div = parseDOM("<div class=test>")[0] as Element;

expect(hasAttrib(dom, "constructor")).toBeFalsy();
expect(hasAttrib(div, "class")).toBeTruthy();

div.attribs["class"] = null as never;

expect(hasAttrib(div, "class")).toBeFalsy();
});
});

Expand Down Expand Up @@ -93,4 +106,29 @@ describe("traversal", () => {
expect(prev).toHaveProperty("tagName", "script");
});
});

describe("getAttributeValue", () => {
it("returns the attribute value", () =>
expect(
getAttributeValue(
parseDOM("<div class='test'>")[0] as Element,
"class"
)
).toBe("test"));
it("returns undefined if attribute does not exist", () =>
expect(
getAttributeValue(parseDOM("<div>")[0] as Element, "id")
).toBeUndefined());
it("should return undefined if a random node is passed", () =>
expect(
getAttributeValue(parseDOM("TEXT")[0] as never, "id")
).toBeUndefined());
});

describe("getName", () => {
it("returns the name of the element", () =>
expect(getName(parseDOM("<div>")[0] as Element)).toBe("div"));
it("should return undefined if a random node is passed", () =>
expect(getName(parseDOM("TEXT")[0] as never)).toBeUndefined());
});
});

0 comments on commit 43b95d2

Please sign in to comment.