Commit dd971666 authored by Jason Davies's avatar Jason Davies
Browse files

Refactor selection tests for minimal loading.

parent 697db521
import "../core/array";
import "../core/document";
import "../core/noop";
import "selection";
d3_selectionPrototype.on = function(type, listener, capture) {
......
var vows = require("vows"),
d3 = require("../../"),
load = require("../load"),
assert = require("../env-assert"),
document = d3.selection().node()._ownerDocument,
window = document.defaultView;
var suite = vows.describe("selection.append");
suite.addBatch({
"select(body)": {
topic: load("selection/selection").sandbox({
document: document,
window: window
}).expression("d3.select"),
"on a simple page": {
topic: function(select) {
return select("body").html("");
},
"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);
},
"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);
},
"propagates data to new element": function(body) {
var data = new Object(), div = body.data([data]).append("div");
assert.strictEqual(div[0][0].__data__, data);
},
"returns a new selection": function(body) {
assert.isFalse(body.append("div") === body);
},
"inherits namespace from parent node": function(body) {
var g = body.append("svg:svg").append("g");
assert.equal(g[0][0].namespaceURI, "http://www.w3.org/2000/svg");
}
}
}
});
suite.addBatch({
"selectAll(div)": {
topic: load("selection/selection").sandbox({
document: document,
window: window
}),
"on a simple page": {
topic: function(d3) {
return d3.select("body").html("").selectAll("div").data([0, 1]).enter().append("div");
},
"appends an HTML element": function(div) {
var span = div.append("span");
assert.equal(span[0].length, 2);
assert.equal(span[0][0].tagName, "SPAN");
assert.equal(span[0][1].tagName, "SPAN");
assert.isNull(span[0][0].namespaceURI);
assert.isNull(span[0][1].namespaceURI);
assert.isTrue(span[0][0].parentNode === div[0][0]);
assert.isTrue(span[0][1].parentNode === div[0][1]);
assert.isTrue(div[0][0].lastChild === span[0][0]);
assert.isTrue(div[0][1].lastChild === span[0][1]);
},
"appends an SVG element": function(div) {
var svg = div.append("svg:svg");
assert.equal(svg[0].length, 2);
assert.equal(svg[0][0].tagName, "SVG");
assert.equal(svg[0][1].tagName, "SVG");
assert.equal(svg[0][0].namespaceURI, "http://www.w3.org/2000/svg");
assert.equal(svg[0][1].namespaceURI, "http://www.w3.org/2000/svg");
assert.isTrue(svg[0][0].parentNode === div[0][0]);
assert.isTrue(svg[0][1].parentNode === div[0][1]);
assert.isTrue(div[0][0].lastChild === svg[0][0]);
assert.isTrue(div[0][1].lastChild === svg[0][1]);
},
"propagates data to new elements": function(div) {
var a = new Object(), b = new Object(), span = div.data([a, b]).append("span");
assert.strictEqual(span[0][0].__data__, a);
assert.strictEqual(span[0][1].__data__, b);
},
"returns a new selection": function(div) {
assert.isFalse(div.append("div") === div);
}
},
"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"),
"on a simple page": {
topic: function(select) {
return select("body");
},
"appends to the parent node": function(body) {
var div = body.html("").selectAll("div").data(d3.range(2)).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);
},
"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");
assert.strictEqual(div[0][0].__data__, a);
assert.strictEqual(div[0][1].__data__, b);
},
"ignores null nodes": function(body) {
body.html("").append("div");
var div = body.selectAll("div").data(d3.range(3)).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);
}
}
}
});
suite.export(module);
var vows = require("vows"),
d3 = require("../../"),
load = require("../load"),
assert = require("../env-assert"),
document = d3.selection().node()._ownerDocument,
window = document.defaultView;
var suite = vows.describe("selection.attr");
suite.addBatch({
"select(body)": {
topic: load("selection/attr").sandbox({
document: document,
window: window
}),
"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");
},
"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");
},
"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");
},
"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");
},
"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.isNull(body.attr("bgcolor"));
},
"removes an attribute as a function": function(body) {
body.attr("bgcolor", "red").attr("bgcolor", function() { return null; });
assert.isNull(body.attr("bgcolor"));
},
"removes a namespaced attribute as null": function(body) {
body.attr("xlink:href", "url").attr("xlink:href", null);
assert.isNull(body.attr("bgcolor"));
},
"removes a namespaced attribute as a function": function(body) {
body.attr("xlink:href", "url").attr("xlink:href", function() { return null; });
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.attr({bgcolor: null, "xlink:href": null});
assert.isNull(document.body.getAttribute("bgcolor"));
assert.isNull(document.body.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.attr({bgcolor: function() {}, "xlink:href": function() {}});
assert.isNull(document.body.getAttribute("bgcolor"));
assert.isNull(document.body.getAttributeNS("http://www.w3.org/1999/xlink", "href"));
},
"returns the current selection": function(body) {
assert.isTrue(body.attr("foo", "bar") === body);
}
}
}
});
suite.addBatch({
"selectAll(div)": {
topic: load("selection/attr").sandbox({
document: document,
window: window
}),
"on a simple page": {
topic: function(d3) {
return d3.select("body").html("").selectAll("div").data([0, 1]).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.isNull(div[0][0].getAttribute("href"));
assert.isNull(div[0][1].getAttribute("href"));
},
"removes an attribute as a function": function(div) {
div.attr("href", "url").attr("href", function() { return null; });
assert.isNull(div[0][0].getAttribute("href"));
assert.isNull(div[0][1].getAttribute("href"));
},
"removes a namespaced attribute as null": function(div) {
div.attr("xlink:foo", "bar").attr("xlink:foo", null);
assert.isNull(div[0][0].getAttributeNS("http://www.w3.org/1999/xlink", "foo"));
assert.isNull(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.isNull(div[0][0].getAttributeNS("http://www.w3.org/1999/xlink", "foo"));
assert.isNull(div[0][1].getAttributeNS("http://www.w3.org/1999/xlink", "foo"));
},
"returns the current selection": function(div) {
assert.isTrue(div.attr("foo", "bar") === div);
}
},
"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"));
}
}
});
suite.export(module);
var vows = require("vows"),
d3 = require("../../"),
load = require("../load"),
assert = require("../env-assert"),
document = d3.selection().node()._ownerDocument,
window = document.defaultView;
var suite = vows.describe("selection.call");
suite.addBatch({
"select(body)": {
topic: load("selection/call").sandbox({
document: document,
window: window
}),
"on a simple page": {
topic: function(d3) {
return d3.select("body").html("");
},
"calls the function once": function(body) {
var count = 0;
body.call(function() { ++count; });
assert.equal(count, 1);
},
"passes any optional arguments": function(body) {
var abc;
body.call(function(selection, a, b, c) { abc = [a, b, c]; }, "a", "b", "c");
assert.deepEqual(abc, ["a", "b", "c"]);
},
"passes the selection as the first argument": function(body) {
var s;
body.call(function(selection) { s = selection; });
assert.isTrue(s === body);
},
"uses the selection as the context": function(body) {
var s;
body.call(function() { s = this; });
assert.isTrue(s === body);
},
"returns the current selection": function(body) {
assert.isTrue(body.call(function() {}) === body);
}
}
}
});
suite.addBatch({
"selectAll(div)": {
topic: load("selection/call").sandbox({
document: document,
window: window
}).expression("d3.select"),
"on a simple page": {
topic: function(select) {
return select("body").html("").selectAll("div").data(d3.range(2)).enter().append("div");
},
"calls the function once": function(div) {
var count = 0;
div.call(function() { ++count; });
assert.equal(count, 1);
},
"passes any optional arguments": function(div) {
var abc;
div.call(function(selection, a, b, c) { abc = [a, b, c]; }, "a", "b", "c");
assert.deepEqual(abc, ["a", "b", "c"]);
},
"passes the selection as the first argument": function(div) {
var s;
div.call(function(selection) { s = selection; });
assert.isTrue(s === div);
},
"uses the selection as the context": function(div) {
var s;
div.call(function() { s = this; });
assert.isTrue(s === div);
},
"returns the current selection": function(div) {
assert.isTrue(div.call(function() {}) === div);
}
}
}
});
suite.export(module);
var vows = require("vows"),
d3 = require("../../"),
load = require("../load"),
assert = require("../env-assert"),
document = d3.selection().node()._ownerDocument,
window = document.defaultView;
var suite = vows.describe("selection.classed");
suite.addBatch({
"select(body)": {
topic: load("selection/classed").sandbox({
document: document,
window: window
}),
"on a simple page": {
topic: function(d3) {
return d3.select("body").html("");
},
"adds a missing class as true": function(body) {
body.attr("class", null);
body.classed("foo", true);
assert.equal(document.body.className, "foo");
body.classed("bar", true);
assert.equal(document.body.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");
body.classed("bar", false);
assert.equal(document.body.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");
body.classed("bar", true);
assert.equal(document.body.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");
body.attr("class", null);
body.classed("bar", false);
assert.equal(document.body.className, "");
},
"gets an existing class": function(body) {
body.attr("class", " foo\tbar baz");
assert.isTrue(body.classed("foo"));
assert.isTrue(body.classed("bar"));
assert.isTrue(body.classed("baz"));
},
"does not get a missing class": function(body) {
body.attr("class", " foo\tbar baz");
assert.isFalse(body.classed("foob"));
assert.isFalse(body.classed("bare"));
assert.isFalse(body.classed("rbaz"));
},
"accepts a name with whitespace, collapsing it": function(body) {
body.attr("class", null);
body.classed(" foo\t", true);
assert.equal(document.body.className, "foo");
body.classed("\tfoo ", false);
assert.equal(document.body.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.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, "");
},
"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.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");
body.classed("foo.bar", false);
assert.equal(document.body.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");
body.classed("\tfoo foo ", false);
assert.equal(document.body.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");
body.classed("foo bar", function() { return true; });
assert.equal(document.body.className, "foo bar");
body.classed("foo", function() { return false; });
assert.equal(document.body.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");
body.classed({bar: true, foo: false});
assert.equal(document.body.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");
},
"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");
},
"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");
},
"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");
body.attr("class", null);
},
"keys in the name object may reference multiple classes": function(body) {