-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext-test.js
115 lines (111 loc) · 4.19 KB
/
text-test.js
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
var vows = require("vows"),
load = require("../load"),
assert = require("../assert");
var suite = vows.describe("selection.text");
suite.addBatch({
"on select(body)": {
topic: load("selection/text").document(),
"on an initially-empty page": {
topic: function(d3) {
return d3.select("body");
},
"sets the text content as a string": function(body) {
body.text("Hello, world!");
assert.equal(body.node().textContent, "Hello, world!");
},
"sets the text content as a number": function(body) {
body.text(42);
assert.equal(body.node().textContent, "42");
},
"sets the text content as a function": function(body) {
body.data(["Subject"]).text(function(d, i) { return "Hello, " + d + " " + i + "!"; });
assert.equal(body.node().textContent, "Hello, Subject 0!");
},
"escapes html content to text": function(body) {
body.text("<h1>Hello, world!</h1>");
assert.equal(body.node().textContent, "<h1>Hello, world!</h1>");
assert.equal(body.node().firstChild.nodeType, 3);
},
"clears the text content as null": function(body) {
body.text(null);
assert.equal(body.node().textContent, "");
},
"clears the text content as undefined": function(body) {
body.text(undefined);
assert.equal(body.node().textContent, "");
},
"clears the text content as a function returning null": function(body) {
body.text(function() { return null; });
assert.equal(body.node().textContent, "");
},
"clears the text content as a function returning undefined": function(body) {
body.text(function() { return undefined; });
assert.equal(body.node().textContent, "");
},
"returns the current selection": function(body) {
assert.isTrue(body.text("hello") === body);
},
"ignores null nodes": function(body) {
var node = body.node();
node.textContent = "foo";
body[0][0] = null;
body.text("bar");
assert.equal(node.textContent, "foo");
}
}
}
});
suite.addBatch({
"on selectAll(div)": {
topic: load("selection/text").document(),
"on a page with a few divs": {
topic: function(d3) {
return d3.select("body").selectAll("div").data([0, 1]).enter().append("div");
},
"sets the text content as a string": function(div) {
div.text("Hello, world!");
assert.equal(div[0][0].textContent, "Hello, world!");
assert.equal(div[0][1].textContent, "Hello, world!");
},
"sets the text content as a number": function(div) {
div.text(42);
assert.equal(div[0][0].textContent, "42");
assert.equal(div[0][1].textContent, "42");
},
"sets the text content as a function": function(div) {
div.data(["foo", "bar"]).text(function(d, i) { return "Hello, " + d + " " + i + "!"; });
assert.equal(div[0][0].textContent, "Hello, foo 0!");
assert.equal(div[0][1].textContent, "Hello, bar 1!");
},
"escapes html content to text": function(div) {
div.text("<h1>Hello, world!</h1>");
assert.equal(div[0][0].textContent, "<h1>Hello, world!</h1>");
assert.equal(div[0][1].textContent, "<h1>Hello, world!</h1>");
assert.equal(div[0][0].firstChild.nodeType, 3);
assert.equal(div[0][1].firstChild.nodeType, 3);
},
"clears the text content as null": function(div) {
div.text(null);
assert.equal(div[0][0].textContent, "");
assert.equal(div[0][1].textContent, "");
},
"clears the text content as a function": function(div) {
div.text(function() { return null; });
assert.equal(div[0][0].textContent, "");
assert.equal(div[0][1].textContent, "");
},
"returns the current selection": function(div) {
assert.isTrue(div.text("hello") === div);
},
"ignores null nodes": function(div) {
var node = div[0][0];
node.textContent = "foo";
div[0][0] = null;
div.text("bar");
assert.equal(node.textContent, "foo");
assert.equal(div[0][1].textContent, "bar");
}
}
}
});
suite.export(module);