Skip to content

Commit 822bcdd

Browse files
committed
Update tests for select and selectAll.
1 parent 53326da commit 822bcdd

2 files changed

Lines changed: 54 additions & 96 deletions

File tree

test/core/select-test.js

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,34 @@ var vows = require("vows"),
77
var suite = vows.describe("d3.select");
88

99
suite.addBatch({
10-
"style": {
10+
"select": {
1111
topic: function() {
12-
return d3.select("body");
13-
},
14-
"can set a property as a string": function(body) {
15-
body.style("background-color", "red");
16-
assert.equal(body[0][0].style["background-color"], "red");
17-
},
18-
"can set a property as a number": function(body) {
19-
body.style("opacity", .3);
20-
assert.equal(body[0][0].style["opacity"], ".3");
21-
},
22-
"can set a property as a function": function(body) {
23-
body.style("background-color", function() { return "orange"; });
24-
assert.equal(body[0][0].style["background-color"], "orange");
25-
},
26-
"can get a property value": function(body) {
27-
body[0][0].style.setProperty("background-color", "yellow", "");
28-
assert.equal(body.style("background-color"), "yellow");
29-
},
30-
"observes the specified priority": function(body) {
31-
body.style("background-color", "green", "important");
32-
assert.equal(body[0][0].style.getPropertyPriority("background-color"), "important");
33-
}
34-
},
35-
"attr": {
36-
topic: function() {
37-
return d3.select("body");
38-
},
39-
"can set an attribute as a string": function(body) {
40-
body.attr("bgcolor", "red");
41-
assert.equal(body[0][0].getAttribute("bgcolor"), "red");
42-
},
43-
"can set an attribute as a number": function(body) {
44-
body.attr("opacity", 1);
45-
assert.equal(body[0][0].getAttribute("opacity"), "1");
46-
},
47-
"can set an attribute as a function": function(body) {
48-
body.attr("bgcolor", function() { return "orange"; });
49-
assert.equal(body[0][0].getAttribute("bgcolor"), "orange");
50-
},
51-
"can get an attribute value": function(body) {
52-
body[0][0].setAttribute("bgcolor", "yellow");
53-
assert.equal(body.attr("bgcolor"), "yellow");
12+
var body = d3.select("body").html("");
13+
body.append("span").attr("class", "f00").attr("id", "b4r").attr("name", "b4z");
14+
body.append("div").attr("class", "foo").attr("id", "bar").attr("name", "baz");
15+
return body;
16+
},
17+
"selects by element name": function() {
18+
var div = d3.select("div");
19+
assert.equal(div[0][0].tagName, "DIV");
20+
},
21+
"selects by class name": function() {
22+
var div = d3.select(".foo");
23+
assert.equal(div[0][0].className, "foo");
24+
},
25+
"selects by id": function() {
26+
var div = d3.select("div#bar");
27+
assert.equal(div[0][0].id, "bar");
28+
},
29+
"selects by attribute value": function() {
30+
var div = d3.select("[name=baz]");
31+
assert.equal(div[0][0].getAttribute("name"), "baz");
32+
},
33+
"selects by node": function() {
34+
var div = d3.select(document.body.lastChild);
35+
assert.isTrue(div[0][0] === document.body.lastChild);
36+
assert.length(div, 1);
37+
assert.length(div[0], 1);
5438
}
5539
}
5640
});

test/core/selectAll-test.js

Lines changed: 27 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,60 +7,34 @@ var vows = require("vows"),
77
var suite = vows.describe("d3.selectAll");
88

99
suite.addBatch({
10-
"style": {
10+
"selectAll": {
1111
topic: function() {
12-
return d3.select("body").selectAll("div").data(d3.range(2)).enter().append("div");
13-
},
14-
"can set a property as a string": function(div) {
15-
div.style("background-color", "red");
16-
assert.equal(div[0][0].style["background-color"], "red");
17-
assert.equal(div[0][1].style["background-color"], "red");
18-
},
19-
"can set a property as a number": function(div) {
20-
div.style("opacity", .5);
21-
assert.equal(div[0][0].style["opacity"], ".5");
22-
assert.equal(div[0][1].style["opacity"], ".5");
23-
},
24-
"can set a property as a function": function(div) {
25-
div.style("background-color", d3.interpolateRgb("orange", "yellow"));
26-
assert.equal(div[0][0].style["background-color"], "rgb(255,165,0)");
27-
assert.equal(div[0][1].style["background-color"], "rgb(255,255,0)");
28-
},
29-
"can get a property value": function(div) {
30-
div[0][0].style.setProperty("background-color", "green", "");
31-
assert.equal(div.style("background-color"), "green");
32-
},
33-
"observes the specified priority": function(div) {
34-
div.style("background-color", "blue", "important");
35-
assert.equal(div[0][0].style.getPropertyPriority("background-color"), "important");
36-
assert.equal(div[0][1].style.getPropertyPriority("background-color"), "important");
37-
}
38-
}
39-
});
40-
41-
suite.addBatch({
42-
"attr": {
43-
topic: function() {
44-
return d3.select("body").selectAll("div");
45-
},
46-
"can set an attribute as a string": function(div) {
47-
div.attr("bgcolor", "red");
48-
assert.equal(div[0][0].getAttribute("bgcolor"), "red");
49-
assert.equal(div[0][1].getAttribute("bgcolor"), "red");
50-
},
51-
"can set an attribute as a number": function(div) {
52-
div.attr("opacity", 0.4);
53-
assert.equal(div[0][0].getAttribute("opacity"), "0.4");
54-
assert.equal(div[0][1].getAttribute("opacity"), "0.4");
55-
},
56-
"can set an attribute as a function": function(div) {
57-
div.attr("bgcolor", d3.interpolateRgb("brown", "steelblue"));
58-
assert.equal(div[0][0].getAttribute("bgcolor"), "rgb(165,42,42)");
59-
assert.equal(div[0][1].getAttribute("bgcolor"), "rgb(70,130,180)");
60-
},
61-
"can get an attribute value": function(div) {
62-
div[0][0].setAttribute("bgcolor", "purple");
63-
assert.equal(div.attr("bgcolor"), "purple");
12+
var body = d3.select("body").html("");
13+
body.append("span").attr("class", "f00").attr("id", "b4r").attr("name", "b4z");
14+
body.append("div").attr("class", "foo").attr("id", "bar").attr("name", "baz");
15+
return body;
16+
},
17+
"selects by element name": function() {
18+
var div = d3.selectAll("div");
19+
assert.equal(div[0][0].tagName, "DIV");
20+
},
21+
"selects by class name": function() {
22+
var div = d3.selectAll(".foo");
23+
assert.equal(div[0][0].className, "foo");
24+
},
25+
"selects by id": function() {
26+
var div = d3.select("div#bar");
27+
assert.equal(div[0][0].id, "bar");
28+
},
29+
"selects by attribute value": function() {
30+
var div = d3.selectAll("[name=baz]");
31+
assert.equal(div[0][0].getAttribute("name"), "baz");
32+
},
33+
"selects by array": function() {
34+
var div = d3.selectAll([document.body.lastChild]);
35+
assert.isTrue(div[0][0] === document.body.lastChild);
36+
assert.length(div, 1);
37+
assert.length(div[0], 1);
6438
}
6539
}
6640
});

0 commit comments

Comments
 (0)