Skip to content

Commit 4c3469d

Browse files
committed
Refactor selection tests for better isolation.
Fixes d3#939.
1 parent 36c59cd commit 4c3469d

26 files changed

Lines changed: 523 additions & 740 deletions

test/load.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
var smash = require("smash");
1+
var smash = require("smash"),
2+
jsdom = require("jsdom");
23

34
module.exports = function() {
45
var files = [].slice.call(arguments).map(function(d) { return "src/" + d; }),
@@ -22,5 +23,11 @@ module.exports = function() {
2223
return topic;
2324
};
2425

26+
topic.document = function(_) {
27+
var document = jsdom.jsdom("<html><head></head><body></body></html>");
28+
sandbox = {document: document, window: document.createWindow()};
29+
return topic;
30+
};
31+
2532
return topic;
2633
};

test/selection/append-test.js

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,29 @@
11
var vows = require("vows"),
2-
d3 = require("../../"),
32
load = require("../load"),
4-
assert = require("../env-assert"),
5-
document = d3.selection().node()._ownerDocument,
6-
window = document.defaultView;
3+
assert = require("../env-assert");
74

85
var suite = vows.describe("selection.append");
96

107
suite.addBatch({
118
"select(body)": {
12-
topic: load("selection/selection").sandbox({
13-
document: document,
14-
window: window
15-
}).expression("d3.select"),
9+
topic: load("selection/append").document(),
1610
"on a simple page": {
17-
topic: function(select) {
18-
return select("body").html("");
11+
topic: function(d3) {
12+
return d3.select("body");
1913
},
2014
"appends an HTML element": function(body) {
2115
var div = body.append("div");
2216
assert.equal(div[0][0].tagName, "DIV");
2317
assert.isNull(div[0][0].namespaceURI);
24-
assert.isTrue(div[0][0].parentNode === document.body);
25-
assert.isTrue(div[0][0] === document.body.lastChild);
18+
assert.isTrue(div[0][0].parentNode === body.node());
19+
assert.isTrue(div[0][0] === body.node().lastChild);
2620
},
2721
"appends an SVG element": function(body) {
2822
var svg = body.append("svg:svg");
2923
assert.equal(svg[0][0].tagName, "SVG");
3024
assert.equal(svg[0][0].namespaceURI, "http://www.w3.org/2000/svg");
31-
assert.isTrue(svg[0][0].parentNode === document.body);
32-
assert.isTrue(svg[0][0] === document.body.lastChild);
25+
assert.isTrue(svg[0][0].parentNode === body.node());
26+
assert.isTrue(svg[0][0] === body.node().lastChild);
3327
},
3428
"propagates data to new element": function(body) {
3529
var data = new Object(), div = body.data([data]).append("div");
@@ -48,13 +42,10 @@ suite.addBatch({
4842

4943
suite.addBatch({
5044
"selectAll(div)": {
51-
topic: load("selection/selection").sandbox({
52-
document: document,
53-
window: window
54-
}),
45+
topic: load("selection/selection").document(),
5546
"on a simple page": {
5647
topic: function(d3) {
57-
return d3.select("body").html("").selectAll("div").data([0, 1]).enter().append("div");
48+
return d3.select("body").selectAll("div").data([0, 1]).enter().append("div");
5849
},
5950
"appends an HTML element": function(div) {
6051
var span = div.append("span");
@@ -87,39 +78,35 @@ suite.addBatch({
8778
},
8879
"returns a new selection": function(div) {
8980
assert.isFalse(div.append("div") === div);
81+
},
82+
"ignores null nodes": function(div) {
83+
var node = div.html("")[0][1];
84+
div[0][1] = null;
85+
var span = div.append("span");
86+
assert.equal(span[0].length, 2);
87+
assert.equal(span[0][0].tagName, "SPAN");
88+
assert.isNull(span[0][1]);
89+
assert.isTrue(span[0][0].parentNode === div[0][0]);
90+
assert.isTrue(div[0][0].lastChild === span[0][0]);
91+
assert.isNull(node.lastChild);
9092
}
91-
},
92-
"ignores null nodes": function(d3) {
93-
var div = d3.select("body").html("").selectAll("div").data([0, 1]).enter().append("div"),
94-
some = d3.selectAll("div");
95-
some[0][1] = null;
96-
var span = some.append("span");
97-
assert.equal(span[0].length, 2);
98-
assert.equal(span[0][0].tagName, "SPAN");
99-
assert.isNull(span[0][1]);
100-
assert.isTrue(span[0][0].parentNode === div[0][0]);
101-
assert.isTrue(div[0][0].lastChild === span[0][0]);
102-
assert.isNull(div[0][1].lastChild);
10393
}
10494
}
10595
});
10696

10797
suite.addBatch({
10898
"selectAll(div).data(…).enter()": {
109-
topic: load("selection/selection").sandbox({
110-
document: document,
111-
window: window
112-
}).expression("d3.select"),
99+
topic: load("selection/selection").document(),
113100
"on a simple page": {
114-
topic: function(select) {
115-
return select("body");
101+
topic: function(d3) {
102+
return d3.select("body");
116103
},
117104
"appends to the parent node": function(body) {
118-
var div = body.html("").selectAll("div").data(d3.range(2)).enter().append("div");
105+
var div = body.selectAll("div").data([0, 1]).enter().append("div");
119106
assert.equal(div.length, 1);
120107
assert.equal(div[0].length, 2);
121-
assert.domEqual(div[0][0].parentNode, document.body);
122-
assert.domEqual(div[0][1].parentNode, document.body);
108+
assert.domEqual(div[0][0].parentNode, body.node());
109+
assert.domEqual(div[0][1].parentNode, body.node());
123110
},
124111
"propagates data to new elements": function(body) {
125112
var a = new Object(), b = new Object(), div = body.html("").selectAll("div").data([a, b]).enter().append("div");
@@ -128,12 +115,12 @@ suite.addBatch({
128115
},
129116
"ignores null nodes": function(body) {
130117
body.html("").append("div");
131-
var div = body.selectAll("div").data(d3.range(3)).enter().append("div");
118+
var div = body.selectAll("div").data([0, 1, 2]).enter().append("div");
132119
assert.equal(div.length, 1);
133120
assert.equal(div[0].length, 3);
134121
assert.domNull(div[0][0]);
135-
assert.domEqual(div[0][1].parentNode, document.body);
136-
assert.domEqual(div[0][2].parentNode, document.body);
122+
assert.domEqual(div[0][1].parentNode, body.node());
123+
assert.domEqual(div[0][2].parentNode, body.node());
137124
}
138125
}
139126
}

test/selection/attr-test.js

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,60 @@
11
var vows = require("vows"),
2-
d3 = require("../../"),
2+
interpolateRgb = require("../../").interpolateRgb,
33
load = require("../load"),
4-
assert = require("../env-assert"),
5-
document = d3.selection().node()._ownerDocument,
6-
window = document.defaultView;
4+
assert = require("../env-assert");
75

86
var suite = vows.describe("selection.attr");
97

108
suite.addBatch({
119
"select(body)": {
12-
topic: load("selection/attr").sandbox({
13-
document: document,
14-
window: window
15-
}),
10+
topic: load("selection/attr").document(),
1611
"on a simple page": {
1712
topic: function(d3) {
1813
return d3.select("body");
1914
},
2015
"sets an attribute as a string": function(body) {
2116
body.attr("bgcolor", "red");
22-
assert.equal(document.body.getAttribute("bgcolor"), "red");
17+
assert.equal(body.node().getAttribute("bgcolor"), "red");
2318
},
2419
"sets an attribute as a number": function(body) {
2520
body.attr("opacity", 1);
26-
assert.equal(document.body.getAttribute("opacity"), "1");
21+
assert.equal(body.node().getAttribute("opacity"), "1");
2722
},
2823
"sets an attribute as a function": function(body) {
2924
body.attr("bgcolor", function() { return "orange"; });
30-
assert.equal(document.body.getAttribute("bgcolor"), "orange");
25+
assert.equal(body.node().getAttribute("bgcolor"), "orange");
3126
},
3227
"sets an attribute as a function of data": function(body) {
3328
body.data(["cyan"]).attr("bgcolor", String);
34-
assert.equal(document.body.getAttribute("bgcolor"), "cyan");
29+
assert.equal(body.node().getAttribute("bgcolor"), "cyan");
3530
},
3631
"sets an attribute as a function of index": function(body) {
3732
body.attr("bgcolor", function(d, i) { return "orange-" + i; });
38-
assert.equal(document.body.getAttribute("bgcolor"), "orange-0");
33+
assert.equal(body.node().getAttribute("bgcolor"), "orange-0");
3934
},
4035
"sets a namespaced attribute as a string": function(body) {
4136
body.attr("xlink:href", "url");
42-
assert.equal(document.body.getAttributeNS("http://www.w3.org/1999/xlink", "href"), "url");
37+
assert.equal(body.node().getAttributeNS("http://www.w3.org/1999/xlink", "href"), "url");
4338
},
4439
"sets a namespaced attribute as a function": function(body) {
4540
body.data(["orange"]).attr("xlink:href", function(d, i) { return d + "-" + i; });
46-
assert.equal(document.body.getAttributeNS("http://www.w3.org/1999/xlink", "href"), "orange-0");
41+
assert.equal(body.node().getAttributeNS("http://www.w3.org/1999/xlink", "href"), "orange-0");
4742
},
4843
"sets attributes as a map of constants": function(body) {
4944
body.attr({bgcolor: "white", "xlink:href": "url.png"});
50-
assert.equal(document.body.getAttribute("bgcolor"), "white");
51-
assert.equal(document.body.getAttributeNS("http://www.w3.org/1999/xlink", "href"), "url.png");
45+
assert.equal(body.node().getAttribute("bgcolor"), "white");
46+
assert.equal(body.node().getAttributeNS("http://www.w3.org/1999/xlink", "href"), "url.png");
5247
},
5348
"sets attributes as a map of functions": function(body) {
5449
body.data(["orange"]).attr({"xlink:href": function(d, i) { return d + "-" + i + ".png"; }});
55-
assert.equal(document.body.getAttributeNS("http://www.w3.org/1999/xlink", "href"), "orange-0.png");
50+
assert.equal(body.node().getAttributeNS("http://www.w3.org/1999/xlink", "href"), "orange-0.png");
5651
},
5752
"gets an attribute value": function(body) {
58-
document.body.setAttribute("bgcolor", "yellow");
53+
body.node().setAttribute("bgcolor", "yellow");
5954
assert.equal(body.attr("bgcolor"), "yellow");
6055
},
6156
"gets a namespaced attribute value": function(body) {
62-
document.body.setAttributeNS("http://www.w3.org/1999/xlink", "foo", "bar");
57+
body.node().setAttributeNS("http://www.w3.org/1999/xlink", "foo", "bar");
6358
assert.equal(body.attr("xlink:foo"), "bar");
6459
},
6560
"removes an attribute as null": function(body) {
@@ -79,18 +74,18 @@ suite.addBatch({
7974
assert.isNull(body.attr("xlink:href"));
8075
},
8176
"removes attributes as a map of null": function(body) {
82-
document.body.setAttribute("bgcolor", "white");
83-
document.body.setAttributeNS("http://www.w3.org/1999/xlink", "href", "foo.png");
77+
body.node().setAttribute("bgcolor", "white");
78+
body.node().setAttributeNS("http://www.w3.org/1999/xlink", "href", "foo.png");
8479
body.attr({bgcolor: null, "xlink:href": null});
85-
assert.isNull(document.body.getAttribute("bgcolor"));
86-
assert.isNull(document.body.getAttributeNS("http://www.w3.org/1999/xlink", "href"));
80+
assert.isNull(body.node().getAttribute("bgcolor"));
81+
assert.isNull(body.node().getAttributeNS("http://www.w3.org/1999/xlink", "href"));
8782
},
8883
"removes attributes as a map of functions that return null": function(body) {
89-
document.body.setAttribute("bgcolor", "white");
90-
document.body.setAttributeNS("http://www.w3.org/1999/xlink", "href", "foo.png");
84+
body.node().setAttribute("bgcolor", "white");
85+
body.node().setAttributeNS("http://www.w3.org/1999/xlink", "href", "foo.png");
9186
body.attr({bgcolor: function() {}, "xlink:href": function() {}});
92-
assert.isNull(document.body.getAttribute("bgcolor"));
93-
assert.isNull(document.body.getAttributeNS("http://www.w3.org/1999/xlink", "href"));
87+
assert.isNull(body.node().getAttribute("bgcolor"));
88+
assert.isNull(body.node().getAttributeNS("http://www.w3.org/1999/xlink", "href"));
9489
},
9590
"returns the current selection": function(body) {
9691
assert.isTrue(body.attr("foo", "bar") === body);
@@ -101,13 +96,10 @@ suite.addBatch({
10196

10297
suite.addBatch({
10398
"selectAll(div)": {
104-
topic: load("selection/attr").sandbox({
105-
document: document,
106-
window: window
107-
}),
99+
topic: load("selection/attr").document(),
108100
"on a simple page": {
109101
topic: function(d3) {
110-
return d3.select("body").html("").selectAll("div").data([0, 1]).enter().append("div");
102+
return d3.select("body").selectAll("div").data([0, 1]).enter().append("div");
111103
},
112104
"sets an attribute as a string": function(div) {
113105
div.attr("bgcolor", "red");
@@ -125,7 +117,7 @@ suite.addBatch({
125117
assert.equal(div[0][1].getAttribute("bgcolor"), "coral");
126118
},
127119
"sets an attribute as a function of data": function(div) {
128-
div.attr("bgcolor", d3.interpolateRgb("brown", "steelblue"));
120+
div.attr("bgcolor", interpolateRgb("brown", "steelblue"));
129121
assert.equal(div[0][0].getAttribute("bgcolor"), "#a52a2a");
130122
assert.equal(div[0][1].getAttribute("bgcolor"), "#4682b4");
131123
},
@@ -174,15 +166,15 @@ suite.addBatch({
174166
},
175167
"returns the current selection": function(div) {
176168
assert.isTrue(div.attr("foo", "bar") === div);
169+
},
170+
"ignores null nodes": function(div) {
171+
var node = div[0][1];
172+
div.attr("href", null);
173+
div[0][1] = null;
174+
div.attr("href", "url");
175+
assert.equal(div[0][0].getAttribute("href"), "url");
176+
assert.isNull(node.getAttribute("href"));
177177
}
178-
},
179-
"ignores null nodes": function(d3) {
180-
var div = d3.select("body").html("").selectAll("div").data([0, 1]).enter().append("div"),
181-
some = d3.selectAll("div");
182-
some[0][1] = null;
183-
some.attr("href", null).attr("href", "url");
184-
assert.equal(div[0][0].getAttribute("href"), "url");
185-
assert.isNull(div[0][1].getAttribute("href"));
186178
}
187179
}
188180
});

test/selection/call-test.js

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
var vows = require("vows"),
2-
d3 = require("../../"),
32
load = require("../load"),
4-
assert = require("../env-assert"),
5-
document = d3.selection().node()._ownerDocument,
6-
window = document.defaultView;
3+
assert = require("../env-assert");
74

85
var suite = vows.describe("selection.call");
96

107
suite.addBatch({
118
"select(body)": {
12-
topic: load("selection/call").sandbox({
13-
document: document,
14-
window: window
15-
}),
9+
topic: load("selection/call").document(),
1610
"on a simple page": {
1711
topic: function(d3) {
18-
return d3.select("body").html("");
12+
return d3.select("body");
1913
},
2014
"calls the function once": function(body) {
2115
var count = 0;
@@ -46,13 +40,10 @@ suite.addBatch({
4640

4741
suite.addBatch({
4842
"selectAll(div)": {
49-
topic: load("selection/call").sandbox({
50-
document: document,
51-
window: window
52-
}).expression("d3.select"),
43+
topic: load("selection/call").document(),
5344
"on a simple page": {
54-
topic: function(select) {
55-
return select("body").html("").selectAll("div").data(d3.range(2)).enter().append("div");
45+
topic: function(d3) {
46+
return d3.select("body").selectAll("div").data([0, 1]).enter().append("div");
5647
},
5748
"calls the function once": function(div) {
5849
var count = 0;

0 commit comments

Comments
 (0)