Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81153 views
1
var DomUtils = require("../..");
2
var fixture = require("../fixture");
3
var assert = require("assert");
4
5
// Set up expected structures
6
var expected = {
7
idAsdf: fixture[1],
8
tag2: [],
9
typeScript: []
10
};
11
for (var idx = 0; idx < 20; ++idx) {
12
expected.tag2.push(fixture[idx*2 + 1].children[5]);
13
expected.typeScript.push(fixture[idx*2 + 1].children[1]);
14
}
15
16
describe("legacy", function() {
17
describe("getElements", function() {
18
var getElements = DomUtils.getElements;
19
it("returns the node with the specified ID", function() {
20
assert.deepEqual(
21
getElements({ id: "asdf" }, fixture, true, 1),
22
[expected.idAsdf]
23
);
24
});
25
it("returns empty array for unknown IDs", function() {
26
assert.deepEqual(getElements({ id: "asdfs" }, fixture, true), []);
27
});
28
it("returns the nodes with the specified tag name", function() {
29
assert.deepEqual(
30
getElements({ tag_name:"tag2" }, fixture, true),
31
expected.tag2
32
);
33
});
34
it("returns empty array for unknown tag names", function() {
35
assert.deepEqual(
36
getElements({ tag_name : "asdfs" }, fixture, true),
37
[]
38
);
39
});
40
it("returns the nodes with the specified tag type", function() {
41
assert.deepEqual(
42
getElements({ tag_type: "script" }, fixture, true),
43
expected.typeScript
44
);
45
});
46
it("returns empty array for unknown tag types", function() {
47
assert.deepEqual(
48
getElements({ tag_type: "video" }, fixture, true),
49
[]
50
);
51
});
52
});
53
54
describe("getElementById", function() {
55
var getElementById = DomUtils.getElementById;
56
it("returns the specified node", function() {
57
assert.equal(
58
expected.idAsdf,
59
getElementById("asdf", fixture, true)
60
);
61
});
62
it("returns `null` for unknown IDs", function() {
63
assert.equal(null, getElementById("asdfs", fixture, true));
64
});
65
});
66
67
describe("getElementsByTagName", function() {
68
var getElementsByTagName = DomUtils.getElementsByTagName;
69
it("returns the specified nodes", function() {
70
assert.deepEqual(
71
getElementsByTagName("tag2", fixture, true),
72
expected.tag2
73
);
74
});
75
it("returns empty array for unknown tag names", function() {
76
assert.deepEqual(
77
getElementsByTagName("tag23", fixture, true),
78
[]
79
);
80
});
81
});
82
83
describe("getElementsByTagType", function() {
84
var getElementsByTagType = DomUtils.getElementsByTagType;
85
it("returns the specified nodes", function() {
86
assert.deepEqual(
87
getElementsByTagType("script", fixture, true),
88
expected.typeScript
89
);
90
});
91
it("returns empty array for unknown tag types", function() {
92
assert.deepEqual(
93
getElementsByTagType("video", fixture, true),
94
[]
95
);
96
});
97
});
98
99
describe("getOuterHTML", function() {
100
var getOuterHTML = DomUtils.getOuterHTML;
101
it("Correctly renders the outer HTML", function() {
102
assert.equal(
103
getOuterHTML(fixture[1]),
104
"<tag1 id=\"asdf\"> <script>text</script> <!-- comment --> <tag2> text </tag2></tag1>"
105
);
106
});
107
});
108
109
describe("getInnerHTML", function() {
110
var getInnerHTML = DomUtils.getInnerHTML;
111
it("Correctly renders the inner HTML", function() {
112
assert.equal(
113
getInnerHTML(fixture[1]),
114
" <script>text</script> <!-- comment --> <tag2> text </tag2>"
115
);
116
});
117
});
118
119
});
120
121