-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
traversal.spec.ts
134 lines (113 loc) · 4.46 KB
/
traversal.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { parseDOM } from "htmlparser2";
import type { Element } from "domhandler";
import {
getAttributeValue,
getName,
getSiblings,
hasAttrib,
nextElementSibling,
prevElementSibling,
} from "./traversal";
describe("traversal", () => {
describe("getSiblings", () => {
it("returns an element's siblings", () => {
const dom = parseDOM("<div><h1></h1><p><p><p></div>")[0] as Element;
expect(getSiblings(dom.children[1])).toHaveLength(4);
});
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", () =>
expect(() =>
hasAttrib(parseDOM("textnode")[0] as never, "some-attrib"),
).not.toThrow());
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(div, "class")).toBeTruthy();
div.attribs["class"] = null as never;
expect(hasAttrib(div, "class")).toBeFalsy();
});
});
describe("nextElementSibling", () => {
it("return Element if found", () => {
const dom = parseDOM(
"<div><h1></h1>test<p></p></div>",
)[0] as Element;
const firstNode = dom.children[0];
const next = nextElementSibling(firstNode);
expect(next).toHaveProperty("tagName", "p");
});
it("return null if not found", () => {
const dom = parseDOM("<div><p></p>test</div>")[0] as Element;
const firstNode = dom.children[0];
expect(nextElementSibling(firstNode)).toBeNull();
});
it("does not ignore script tags", () => {
const dom = parseDOM(
"<div><p></p><script></script><p></div>",
)[0] as Element;
const firstNode = dom.children[0];
const next = nextElementSibling(firstNode);
expect(next).toHaveProperty("tagName", "script");
});
});
describe("prevElementSibling", () => {
it("return Element if found", () => {
const dom = parseDOM(
"<div><h1></h1>test<p></p></div>",
)[0] as Element;
const lastNode = dom.children[2];
const prev = prevElementSibling(lastNode);
expect(prev).toHaveProperty("tagName", "h1");
});
it("return null if not found", () => {
const dom = parseDOM("<div>test<p></p></div>")[0] as Element;
const lastNode = dom.children[1];
expect(prevElementSibling(lastNode)).toBeNull();
});
it("does not ignore script tags", () => {
const dom = parseDOM(
"<div><p></p><script></script><p></p></div>",
)[0] as Element;
const lastNode = dom.children[2];
const prev = prevElementSibling(lastNode);
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());
});
});