forked from d3/d3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection-attr-test.js
More file actions
153 lines (148 loc) · 6.62 KB
/
Copy pathselection-attr-test.js
File metadata and controls
153 lines (148 loc) · 6.62 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
require("../env");
require("../../d3");
var vows = require("vows"),
assert = require("assert");
var suite = vows.describe("selection.attr");
suite.addBatch({
"select(body)": {
topic: function() {
return d3.select("body");
},
"sets an attribute as a string": function(body) {
body.attr("bgcolor", "red");
assert.equal(document.body.getAttribute("bgcolor"), "red");
},
"sets an attribute as a number": function(body) {
body.attr("opacity", 1);
assert.equal(document.body.getAttribute("opacity"), "1");
},
"sets an attribute as a function": function(body) {
body.attr("bgcolor", function() { return "orange"; });
assert.equal(document.body.getAttribute("bgcolor"), "orange");
},
"sets an attribute as a function of data": function(body) {
body.data(["cyan"]).attr("bgcolor", String);
assert.equal(document.body.getAttribute("bgcolor"), "cyan");
},
"sets an attribute as a function of index": function(body) {
body.attr("bgcolor", function(d, i) { return "orange-" + i; });
assert.equal(document.body.getAttribute("bgcolor"), "orange-0");
},
"sets a namespaced attribute as a string": function(body) {
body.attr("xlink:href", "url");
assert.equal(document.body.getAttributeNS("http://www.w3.org/1999/xlink", "href"), "url");
},
"sets a namespaced attribute as a function": function(body) {
body.data(["orange"]).attr("xlink:href", function(d, i) { return d + "-" + i; });
assert.equal(document.body.getAttributeNS("http://www.w3.org/1999/xlink", "href"), "orange-0");
},
"gets an attribute value": function(body) {
document.body.setAttribute("bgcolor", "yellow");
assert.equal(body.attr("bgcolor"), "yellow");
},
"gets a namespaced attribute value": function(body) {
document.body.setAttributeNS("http://www.w3.org/1999/xlink", "foo", "bar");
assert.equal(body.attr("xlink:foo"), "bar");
},
"removes an attribute as null": function(body) {
body.attr("bgcolor", "red").attr("bgcolor", null);
assert.equal(body.attr("bgcolor"), "");
},
"removes an attribute as a function": function(body) {
body.attr("bgcolor", "red").attr("bgcolor", function() { return null; });
assert.equal(body.attr("bgcolor"), "");
},
"removes a namespaced attribute as null": function(body) {
body.attr("xlink:href", "url").attr("xlink:href", null);
assert.equal(body.attr("bgcolor"), "");
},
"removes a namespaced attribute as a function": function(body) {
body.attr("xlink:href", "url").attr("xlink:href", function() { return null; });
assert.equal(body.attr("xlink:href"), "");
},
"returns the current selection": function(body) {
assert.isTrue(body.attr("foo", "bar") === body);
}
}
});
suite.addBatch({
"selectAll(div)": {
topic: function() {
return d3.select("body").html("").selectAll("div").data(d3.range(2)).enter().append("div");
},
"sets an attribute as a string": function(div) {
div.attr("bgcolor", "red");
assert.equal(div[0][0].getAttribute("bgcolor"), "red");
assert.equal(div[0][1].getAttribute("bgcolor"), "red");
},
"sets an attribute as a number": function(div) {
div.attr("opacity", 0.4);
assert.equal(div[0][0].getAttribute("opacity"), "0.4");
assert.equal(div[0][1].getAttribute("opacity"), "0.4");
},
"sets an attribute as a function": function(div) {
div.attr("bgcolor", function() { return "coral"; });
assert.equal(div[0][0].getAttribute("bgcolor"), "coral");
assert.equal(div[0][1].getAttribute("bgcolor"), "coral");
},
"sets an attribute as a function of data": function(div) {
div.attr("bgcolor", d3.interpolateRgb("brown", "steelblue"));
assert.equal(div[0][0].getAttribute("bgcolor"), "#a52a2a");
assert.equal(div[0][1].getAttribute("bgcolor"), "#4682b4");
},
"sets an attribute as a function of index": function(div) {
div.attr("bgcolor", function(d, i) { return "color-" + i; });
assert.equal(div[0][0].getAttribute("bgcolor"), "color-0");
assert.equal(div[0][1].getAttribute("bgcolor"), "color-1");
},
"sets a namespaced attribute as a string": function(div) {
div.attr("xlink:href", "url");
assert.equal(div[0][0].getAttributeNS("http://www.w3.org/1999/xlink", "href"), "url");
assert.equal(div[0][1].getAttributeNS("http://www.w3.org/1999/xlink", "href"), "url");
},
"sets a namespaced attribute as a function": function(div) {
div.data(["red", "blue"]).attr("xlink:href", function(d, i) { return d + "-" + i; });
assert.equal(div[0][0].getAttributeNS("http://www.w3.org/1999/xlink", "href"), "red-0");
assert.equal(div[0][1].getAttributeNS("http://www.w3.org/1999/xlink", "href"), "blue-1");
},
"gets an attribute value": function(div) {
div[0][0].setAttribute("bgcolor", "purple");
assert.equal(div.attr("bgcolor"), "purple");
},
"gets a namespaced attribute value": function(div) {
div[0][0].setAttributeNS("http://www.w3.org/1999/xlink", "foo", "bar");
assert.equal(div.attr("xlink:foo"), "bar");
},
"removes an attribute as null": function(div) {
div.attr("href", "url").attr("href", null);
assert.equal(div[0][0].getAttribute("href"), "");
assert.equal(div[0][1].getAttribute("href"), "");
},
"removes an attribute as a function": function(div) {
div.attr("href", "url").attr("href", function() { return null; });
assert.equal(div[0][0].getAttribute("href"), "");
assert.equal(div[0][1].getAttribute("href"), "");
},
"removes a namespaced attribute as null": function(div) {
div.attr("xlink:foo", "bar").attr("xlink:foo", null);
assert.equal(div[0][0].getAttributeNS("http://www.w3.org/1999/xlink", "foo"), "");
assert.equal(div[0][1].getAttributeNS("http://www.w3.org/1999/xlink", "foo"), "");
},
"removes a namespaced attribute as a function": function(div) {
div.attr("xlink:foo", "bar").attr("xlink:foo", function() { return null; });
assert.equal(div[0][0].getAttributeNS("http://www.w3.org/1999/xlink", "foo"), "");
assert.equal(div[0][1].getAttributeNS("http://www.w3.org/1999/xlink", "foo"), "");
},
"ignores null nodes": function(div) {
var some = d3.selectAll("div");
some[0][1] = null;
some.attr("href", null).attr("href", "url");
assert.equal(div[0][0].getAttribute("href"), "url");
assert.equal(div[0][1].getAttribute("href"), "");
},
"returns the current selection": function(div) {
assert.isTrue(div.attr("foo", "bar") === div);
}
}
});
suite.export(module);