Commit 4c3469d3 authored by Mike Bostock's avatar Mike Bostock
Browse files

Refactor selection tests for better isolation.

Fixes #939.
parent 36c59cd2
var smash = require("smash");
var smash = require("smash"),
jsdom = require("jsdom");
module.exports = function() {
var files = [].slice.call(arguments).map(function(d) { return "src/" + d; }),
......@@ -22,5 +23,11 @@ module.exports = function() {
return topic;
};
topic.document = function(_) {
var document = jsdom.jsdom("<html><head></head><body></body></html>");
sandbox = {document: document, window: document.createWindow()};
return topic;
};
return topic;
};
var vows = require("vows"),
d3 = require("../../"),
load = require("../load"),
assert = require("../env-assert"),
document = d3.selection().node()._ownerDocument,
window = document.defaultView;
assert = require("../env-assert");
var suite = vows.describe("selection.append");
suite.addBatch({
"select(body)": {
topic: load("selection/selection").sandbox({
document: document,
window: window
}).expression("d3.select"),
topic: load("selection/append").document(),
"on a simple page": {
topic: function(select) {
return select("body").html("");
topic: function(d3) {
return d3.select("body");
},
"appends an HTML element": function(body) {
var div = body.append("div");
assert.equal(div[0][0].tagName, "DIV");
assert.isNull(div[0][0].namespaceURI);
assert.isTrue(div[0][0].parentNode === document.body);
assert.isTrue(div[0][0] === document.body.lastChild);
assert.isTrue(div[0][0].parentNode === body.node());
assert.isTrue(div[0][0] === body.node().lastChild);
},
"appends an SVG element": function(body) {
var svg = body.append("svg:svg");
assert.equal(svg[0][0].tagName, "SVG");
assert.equal(svg[0][0].namespaceURI, "http://www.w3.org/2000/svg");
assert.isTrue(svg[0][0].parentNode === document.body);
assert.isTrue(svg[0][0] === document.body.lastChild);
assert.isTrue(svg[0][0].parentNode === body.node());
assert.isTrue(svg[0][0] === body.node().lastChild);
},
"propagates data to new element": function(body) {
var data = new Object(), div = body.data([data]).append("div");
......@@ -48,13 +42,10 @@ suite.addBatch({
suite.addBatch({
"selectAll(div)": {
topic: load("selection/selection").sandbox({
document: document,
window: window
}),
topic: load("selection/selection").document(),
"on a simple page": {
topic: function(d3) {
return d3.select("body").html("").selectAll("div").data([0, 1]).enter().append("div");
return d3.select("body").selectAll("div").data([0, 1]).enter().append("div");
},
"appends an HTML element": function(div) {
var span = div.append("span");
......@@ -87,39 +78,35 @@ suite.addBatch({
},
"returns a new selection": function(div) {
assert.isFalse(div.append("div") === div);
},
"ignores null nodes": function(div) {
var node = div.html("")[0][1];
div[0][1] = null;
var span = div.append("span");
assert.equal(span[0].length, 2);
assert.equal(span[0][0].tagName, "SPAN");
assert.isNull(span[0][1]);
assert.isTrue(span[0][0].parentNode === div[0][0]);
assert.isTrue(div[0][0].lastChild === span[0][0]);
assert.isNull(node.lastChild);
}
},
"ignores null nodes": function(d3) {
var div = d3.select("body").html("").selectAll("div").data([0, 1]).enter().append("div"),
some = d3.selectAll("div");
some[0][1] = null;
var span = some.append("span");
assert.equal(span[0].length, 2);
assert.equal(span[0][0].tagName, "SPAN");
assert.isNull(span[0][1]);
assert.isTrue(span[0][0].parentNode === div[0][0]);
assert.isTrue(div[0][0].lastChild === span[0][0]);
assert.isNull(div[0][1].lastChild);
}
}
});
suite.addBatch({
"selectAll(div).data(…).enter()": {
topic: load("selection/selection").sandbox({
document: document,
window: window
}).expression("d3.select"),
topic: load("selection/selection").document(),
"on a simple page": {
topic: function(select) {
return select("body");
topic: function(d3) {
return d3.select("body");
},
"appends to the parent node": function(body) {
var div = body.html("").selectAll("div").data(d3.range(2)).enter().append("div");
var div = body.selectAll("div").data([0, 1]).enter().append("div");
assert.equal(div.length, 1);
assert.equal(div[0].length, 2);
assert.domEqual(div[0][0].parentNode, document.body);
assert.domEqual(div[0][1].parentNode, document.body);
assert.domEqual(div[0][0].parentNode, body.node());
assert.domEqual(div[0][1].parentNode, body.node());
},
"propagates data to new elements": function(body) {
var a = new Object(), b = new Object(), div = body.html("").selectAll("div").data([a, b]).enter().append("div");
......@@ -128,12 +115,12 @@ suite.addBatch({
},
"ignores null nodes": function(body) {
body.html("").append("div");
var div = body.selectAll("div").data(d3.range(3)).enter().append("div");
var div = body.selectAll("div").data([0, 1, 2]).enter().append("div");
assert.equal(div.length, 1);
assert.equal(div[0].length, 3);
assert.domNull(div[0][0]);
assert.domEqual(div[0][1].parentNode, document.body);
assert.domEqual(div[0][2].parentNode, document.body);
assert.domEqual(div[0][1].parentNode, body.node());
assert.domEqual(div[0][2].parentNode, body.node());
}
}
}
......
var vows = require("vows"),
d3 = require("../../"),
interpolateRgb = require("../../").interpolateRgb,
load = require("../load"),
assert = require("../env-assert"),
document = d3.selection().node()._ownerDocument,
window = document.defaultView;
assert = require("../env-assert");
var suite = vows.describe("selection.attr");
suite.addBatch({
"select(body)": {
topic: load("selection/attr").sandbox({
document: document,
window: window
}),
topic: load("selection/attr").document(),
"on a simple page": {
topic: function(d3) {
return d3.select("body");
},
"sets an attribute as a string": function(body) {
body.attr("bgcolor", "red");
assert.equal(document.body.getAttribute("bgcolor"), "red");
assert.equal(body.node().getAttribute("bgcolor"), "red");
},
"sets an attribute as a number": function(body) {
body.attr("opacity", 1);
assert.equal(document.body.getAttribute("opacity"), "1");
assert.equal(body.node().getAttribute("opacity"), "1");
},
"sets an attribute as a function": function(body) {
body.attr("bgcolor", function() { return "orange"; });
assert.equal(document.body.getAttribute("bgcolor"), "orange");
assert.equal(body.node().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");
assert.equal(body.node().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");
assert.equal(body.node().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");
assert.equal(body.node().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");
assert.equal(body.node().getAttributeNS("http://www.w3.org/1999/xlink", "href"), "orange-0");
},
"sets attributes as a map of constants": function(body) {
body.attr({bgcolor: "white", "xlink:href": "url.png"});
assert.equal(document.body.getAttribute("bgcolor"), "white");
assert.equal(document.body.getAttributeNS("http://www.w3.org/1999/xlink", "href"), "url.png");
assert.equal(body.node().getAttribute("bgcolor"), "white");
assert.equal(body.node().getAttributeNS("http://www.w3.org/1999/xlink", "href"), "url.png");
},
"sets attributes as a map of functions": function(body) {
body.data(["orange"]).attr({"xlink:href": function(d, i) { return d + "-" + i + ".png"; }});
assert.equal(document.body.getAttributeNS("http://www.w3.org/1999/xlink", "href"), "orange-0.png");
assert.equal(body.node().getAttributeNS("http://www.w3.org/1999/xlink", "href"), "orange-0.png");
},
"gets an attribute value": function(body) {
document.body.setAttribute("bgcolor", "yellow");
body.node().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");
body.node().setAttributeNS("http://www.w3.org/1999/xlink", "foo", "bar");
assert.equal(body.attr("xlink:foo"), "bar");
},
"removes an attribute as null": function(body) {
......@@ -79,18 +74,18 @@ suite.addBatch({
assert.isNull(body.attr("xlink:href"));
},
"removes attributes as a map of null": function(body) {
document.body.setAttribute("bgcolor", "white");
document.body.setAttributeNS("http://www.w3.org/1999/xlink", "href", "foo.png");
body.node().setAttribute("bgcolor", "white");
body.node().setAttributeNS("http://www.w3.org/1999/xlink", "href", "foo.png");
body.attr({bgcolor: null, "xlink:href": null});
assert.isNull(document.body.getAttribute("bgcolor"));
assert.isNull(document.body.getAttributeNS("http://www.w3.org/1999/xlink", "href"));
assert.isNull(body.node().getAttribute("bgcolor"));
assert.isNull(body.node().getAttributeNS("http://www.w3.org/1999/xlink", "href"));
},
"removes attributes as a map of functions that return null": function(body) {
document.body.setAttribute("bgcolor", "white");
document.body.setAttributeNS("http://www.w3.org/1999/xlink", "href", "foo.png");
body.node().setAttribute("bgcolor", "white");
body.node().setAttributeNS("http://www.w3.org/1999/xlink", "href", "foo.png");
body.attr({bgcolor: function() {}, "xlink:href": function() {}});
assert.isNull(document.body.getAttribute("bgcolor"));
assert.isNull(document.body.getAttributeNS("http://www.w3.org/1999/xlink", "href"));
assert.isNull(body.node().getAttribute("bgcolor"));
assert.isNull(body.node().getAttributeNS("http://www.w3.org/1999/xlink", "href"));
},
"returns the current selection": function(body) {
assert.isTrue(body.attr("foo", "bar") === body);
......@@ -101,13 +96,10 @@ suite.addBatch({
suite.addBatch({
"selectAll(div)": {
topic: load("selection/attr").sandbox({
document: document,
window: window
}),
topic: load("selection/attr").document(),
"on a simple page": {
topic: function(d3) {
return d3.select("body").html("").selectAll("div").data([0, 1]).enter().append("div");
return d3.select("body").selectAll("div").data([0, 1]).enter().append("div");
},
"sets an attribute as a string": function(div) {
div.attr("bgcolor", "red");
......@@ -125,7 +117,7 @@ suite.addBatch({
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"));
div.attr("bgcolor", interpolateRgb("brown", "steelblue"));
assert.equal(div[0][0].getAttribute("bgcolor"), "#a52a2a");
assert.equal(div[0][1].getAttribute("bgcolor"), "#4682b4");
},
......@@ -174,15 +166,15 @@ suite.addBatch({
},
"returns the current selection": function(div) {
assert.isTrue(div.attr("foo", "bar") === div);
},
"ignores null nodes": function(div) {
var node = div[0][1];
div.attr("href", null);
div[0][1] = null;
div.attr("href", "url");
assert.equal(div[0][0].getAttribute("href"), "url");
assert.isNull(node.getAttribute("href"));
}
},
"ignores null nodes": function(d3) {
var div = d3.select("body").html("").selectAll("div").data([0, 1]).enter().append("div"),
some = d3.selectAll("div");
some[0][1] = null;
some.attr("href", null).attr("href", "url");
assert.equal(div[0][0].getAttribute("href"), "url");
assert.isNull(div[0][1].getAttribute("href"));
}
}
});
......
var vows = require("vows"),
d3 = require("../../"),
load = require("../load"),
assert = require("../env-assert"),
document = d3.selection().node()._ownerDocument,
window = document.defaultView;
assert = require("../env-assert");
var suite = vows.describe("selection.call");
suite.addBatch({
"select(body)": {
topic: load("selection/call").sandbox({
document: document,
window: window
}),
topic: load("selection/call").document(),
"on a simple page": {
topic: function(d3) {
return d3.select("body").html("");
return d3.select("body");
},
"calls the function once": function(body) {
var count = 0;
......@@ -46,13 +40,10 @@ suite.addBatch({
suite.addBatch({
"selectAll(div)": {
topic: load("selection/call").sandbox({
document: document,
window: window
}).expression("d3.select"),
topic: load("selection/call").document(),
"on a simple page": {
topic: function(select) {
return select("body").html("").selectAll("div").data(d3.range(2)).enter().append("div");
topic: function(d3) {
return d3.select("body").selectAll("div").data([0, 1]).enter().append("div");
},
"calls the function once": function(div) {
var count = 0;
......
var vows = require("vows"),
d3 = require("../../"),
load = require("../load"),
assert = require("../env-assert"),
document = d3.selection().node()._ownerDocument,
window = document.defaultView;
assert = require("../env-assert");
var suite = vows.describe("selection.classed");
suite.addBatch({
"select(body)": {
topic: load("selection/classed").sandbox({
document: document,
window: window
}),
topic: load("selection/classed").document(),
"on a simple page": {
topic: function(d3) {
return d3.select("body").html("");
return d3.select("body");
},
"adds a missing class as true": function(body) {
body.attr("class", null);
body.classed("foo", true);
assert.equal(document.body.className, "foo");
assert.equal(body.node().className, "foo");
body.classed("bar", true);
assert.equal(document.body.className, "foo bar");
assert.equal(body.node().className, "foo bar");
},
"removes an existing class as false": function(body) {
body.attr("class", "bar foo");
body.classed("foo", false);
assert.equal(document.body.className, "bar");
assert.equal(body.node().className, "bar");
body.classed("bar", false);
assert.equal(document.body.className, "");
assert.equal(body.node().className, "");
},
"preserves an existing class as true": function(body) {
body.attr("class", "bar foo");
body.classed("foo", true);
assert.equal(document.body.className, "bar foo");
assert.equal(body.node().className, "bar foo");
body.classed("bar", true);
assert.equal(document.body.className, "bar foo");
assert.equal(body.node().className, "bar foo");
},
"preserves a missing class as false": function(body) {
body.attr("class", "baz");
body.classed("foo", false);
assert.equal(document.body.className, "baz");
assert.equal(body.node().className, "baz");
body.attr("class", null);
body.classed("bar", false);
assert.equal(document.body.className, "");
assert.equal(body.node().className, "");
},
"gets an existing class": function(body) {
body.attr("class", " foo\tbar baz");
......@@ -61,94 +55,94 @@ suite.addBatch({
"accepts a name with whitespace, collapsing it": function(body) {
body.attr("class", null);
body.classed(" foo\t", true);
assert.equal(document.body.className, "foo");
assert.equal(body.node().className, "foo");
body.classed("\tfoo ", false);
assert.equal(document.body.className, "");
assert.equal(body.node().className, "");
},
"accepts a name with multiple classes separated by whitespace": function(body) {
body.attr("class", null);
body.classed("foo bar", true);
assert.equal(document.body.className, "foo bar");
assert.equal(body.node().className, "foo bar");
assert.isTrue(body.classed("foo bar"));
assert.isTrue(body.classed("bar foo"));
assert.isFalse(body.classed("foo bar baz"));
assert.isFalse(body.classed("foob bar"));
body.classed("bar foo", false);
assert.equal(document.body.className, "");
assert.equal(body.node().className, "");
},
"accepts a silly class name with unsafe characters": function(body) {
body.attr("class", null);
body.classed("foo.bar", true);
assert.equal(document.body.className, "foo.bar");
assert.equal(body.node().className, "foo.bar");
assert.isTrue(body.classed("foo.bar"));
assert.isFalse(body.classed("foo"));
assert.isFalse(body.classed("bar"));
body.classed("bar.foo", false);
assert.equal(document.body.className, "foo.bar");
assert.equal(body.node().className, "foo.bar");
body.classed("foo.bar", false);
assert.equal(document.body.className, "");
assert.equal(body.node().className, "");
},
"accepts a name with duplicate classes, ignoring them": function(body) {
body.attr("class", null);
body.classed(" foo\tfoo ", true);
assert.equal(document.body.className, "foo");
assert.equal(body.node().className, "foo");
body.classed("\tfoo foo ", false);
assert.equal(document.body.className, "");
assert.equal(body.node().className, "");
},
"accepts a value function returning true or false": function(body) {
body.attr("class", null);
body.classed("foo", function() { return true; });
assert.equal(document.body.className, "foo");
assert.equal(body.node().className, "foo");
body.classed("foo bar", function() { return true; });
assert.equal(document.body.className, "foo bar");
assert.equal(body.node().className, "foo bar");
body.classed("foo", function() { return false; });
assert.equal(document.body.className, "bar");
assert.equal(body.node().className, "bar");
},
"accepts a name object containing true or false": function(body) {
body.attr("class", null);
body.classed({foo: true});
assert.equal(document.body.className, "foo");
assert.equal(body.node().className, "foo");
body.classed({bar: true, foo: false});
assert.equal(document.body.className, "bar");
assert.equal(body.node().className, "bar");
},
"accepts a name object containing a function returning true or false": function(body) {
body.attr("class", null);
body.classed({foo: function() { return true; }});
assert.equal(document.body.className, "foo");
assert.equal(body.node().className, "foo");
},
"accepts a name object containing a mix of functions and non-functions": function(body) {
body.attr("class", "foo");
body.classed({foo: false, bar: function() { return true; }});
assert.equal(document.body.className, "bar");
assert.equal(body.node().className, "bar");
},
"the value may be truthy or falsey": function(body) {
body.attr("class", "foo");
body.classed({foo: null, bar: function() { return 1; }});
assert.equal(document.body.className, "bar");
assert.equal(body.node().className, "bar");
},
"keys in the name object may contain whitespace": function(body) {
body.attr("class", null);
body.classed({" foo\t": function() { return true; }});
assert.equal(document.body.className, "foo");
assert.equal(body.node().className, "foo");
body.attr("class", null);
},
"keys in the name object may reference multiple classes": function(body) {
body.attr("class", null);
body.classed({"foo bar": function() { return true; }});
assert.equal(document.body.className, "foo bar");
assert.equal(body.node().className, "foo bar");
body.attr("class", null);
},
"keys in the name object may contain duplicates": function(body) {
body.attr("class", null);
body.classed({"foo foo": function() { return true; }});
assert.equal(document.body.className, "foo");
assert.equal(body.node().className, "foo");
body.attr("class", null);
},
"value functions are only evaluated once when used for multiple classes": function(body) {
var count = 0;
body.attr("class", null);
body.classed({"foo bar": function() { return ++count; }});
assert.equal(document.body.className, "foo bar");
assert.equal(body.node().className, "foo bar");
assert.equal(count, 1);
},
"returns the current selection": function(body) {
......@@ -160,13 +154,10 @@ suite.addBatch({
suite.addBatch({
"selectAll(div)": {
topic: load("selection/classed").sandbox({
document: document,
window: window
}),
topic: load("selection/classed").document(),
"on a simple page": {
topic: function(d3) {
return d3.select("body").html("").selectAll("div").data([0, 1]).enter().append("div");
return d3.select("body").selectAll("div").data([0, 1]).enter().append("div");
},
"adds a missing class as true": function(div) {
div.attr("class", null);
......@@ -262,15 +253,15 @@ suite.addBatch({
},
"returns the current selection": function(div) {
assert.isTrue(div.classed("foo", true) === div);
},
"ignores null nodes": function(div) {
var node = div[0][1];
div.attr("class", null);
div[0][1] = null;
div.classed("foo", true);
assert.equal(div[0][0].className, "foo");
assert.equal(node.className, "");
}
},
"ignores null nodes": function(d3) {
var div = d3.select("body").html("").selectAll("div").data([0, 1]).enter().append("div"),
some = d3.selectAll("div");