Refactor selection tests for minimal loading.

This commit is contained in:
Jason Davies 2013-03-14 17:03:10 +00:00
Родитель 697db5217a
Коммит dd971666a6
46 изменённых файлов: 2716 добавлений и 2392 удалений

Просмотреть файл

@ -1,5 +1,6 @@
import "../core/array";
import "../core/document";
import "../core/noop";
import "selection";
d3_selectionPrototype.on = function(type, listener, capture) {

Просмотреть файл

@ -0,0 +1,142 @@
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);

190
test/selection/attr-test.js Normal file
Просмотреть файл

@ -0,0 +1,190 @@
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);

Просмотреть файл

@ -0,0 +1,84 @@
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);

Просмотреть файл

@ -0,0 +1,278 @@
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) {
body.attr("class", null);
body.classed({"foo bar": function() { return true; }});
assert.equal(document.body.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");
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(count, 1);
},
"returns the current selection": function(body) {
assert.isTrue(body.classed("foo", true) === body);
}
}
}
});
suite.addBatch({
"selectAll(div)": {
topic: load("selection/classed").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");
},
"adds a missing class as true": function(div) {
div.attr("class", null);
div.classed("foo", true);
assert.equal(div[0][0].className, "foo");
assert.equal(div[0][1].className, "foo");
div.classed("bar", true);
assert.equal(div[0][0].className, "foo bar");
assert.equal(div[0][1].className, "foo bar");
},
"adds a missing class as a function": function(div) {
div.data([0, 1]).attr("class", null);
div.classed("foo", function(d, i) { return d === 0; });
assert.equal(div[0][0].className, "foo");
assert.equal(div[0][1].className, "");
div.classed("bar", function(d, i) { return i === 1; });
assert.equal(div[0][0].className, "foo");
assert.equal(div[0][1].className, "bar");
},
"removes an existing class as false": function(div) {
div.attr("class", "bar foo");
div.classed("foo", false);
assert.equal(div[0][0].className, "bar");
assert.equal(div[0][1].className, "bar");
div.classed("bar", false);
assert.equal(div[0][0].className, "");
assert.equal(div[0][1].className, "");
},
"removes an existing class as a function": function(div) {
div.data([0, 1]).attr("class", "bar foo");
div.classed("foo", function(d, i) { return d === 0; });
assert.equal(div[0][0].className, "bar foo");
assert.equal(div[0][1].className, "bar");
div.classed("bar", function(d, i) { return i === 1; });
assert.equal(div[0][0].className, "foo");
assert.equal(div[0][1].className, "bar");
div.classed("foo", function() { return false; });
assert.equal(div[0][0].className, "");
assert.equal(div[0][1].className, "bar");
div.classed("bar", function() { return false; });
assert.equal(div[0][0].className, "");
assert.equal(div[0][1].className, "");
},
"preserves an existing class as true": function(div) {
div.attr("class", "bar foo");
div.classed("foo", true);
assert.equal(div[0][0].className, "bar foo");
assert.equal(div[0][1].className, "bar foo");
div.classed("bar", true);
assert.equal(div[0][0].className, "bar foo");
assert.equal(div[0][1].className, "bar foo");
},
"preserves an existing class as a function": function(div) {
div.attr("class", "bar foo");
div.classed("foo", function() { return true; });
assert.equal(div[0][0].className, "bar foo");
assert.equal(div[0][1].className, "bar foo");
div.classed("bar", function() { return true; });
assert.equal(div[0][0].className, "bar foo");
assert.equal(div[0][1].className, "bar foo");
},
"preserves a missing class as false": function(div) {
div.attr("class", "baz");
div.classed("foo", false);
assert.equal(div[0][0].className, "baz");
assert.equal(div[0][1].className, "baz");
div.attr("class", null);
div.classed("bar", false);
assert.equal(div[0][0].className, "");
assert.equal(div[0][1].className, "");
},
"preserves a missing class as a function": function(div) {
div.attr("class", "baz");
div.classed("foo", function() { return false; });
assert.equal(div[0][0].className, "baz");
assert.equal(div[0][1].className, "baz");
div.attr("class", null);
div.classed("bar", function() { return false; });
assert.equal(div[0][0].className, "");
assert.equal(div[0][1].className, "");
},
"gets an existing class": function(div) {
div[0][0].className = " foo\tbar baz";
assert.isTrue(div.classed("foo"));
assert.isTrue(div.classed("bar"));
assert.isTrue(div.classed("baz"));
},
"does not get a missing class": function(div) {
div[0][0].className = " foo\tbar baz";
assert.isFalse(div.classed("foob"));
assert.isFalse(div.classed("bare"));
assert.isFalse(div.classed("rbaz"));
},
"returns the current selection": function(div) {
assert.isTrue(div.classed("foo", true) === 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("class", null).classed("foo", true);
assert.equal(div[0][0].className, "foo");
assert.equal(div[0][1].className, "");
}
}
});
suite.export(module);

264
test/selection/data-test.js Normal file
Просмотреть файл

@ -0,0 +1,264 @@
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.data");
suite.addBatch({
"select(body)": {
topic: load("selection/selection").sandbox({
document: document,
window: window
}),
"on a simple page": {
topic: function(d3) {
return d3.select("body").html("");
},
"assigns data as an array": function(body) {
var data = new Object();
body.data([data]);
assert.strictEqual(document.body.__data__, data);
},
"assigns data as a function": function(body) {
var data = new Object();
body.data(function() { return [data]; });
assert.strictEqual(document.body.__data__, data);
},
"stores data in the DOM": function(body) {
var expected = new Object(), actual;
document.body.__data__ = expected;
body.each(function(d) { actual = d; });
assert.strictEqual(actual, expected);
},
"returns a new selection": function(body) {
assert.isFalse(body.data([1]) === body);
},
"with no arguments, returns an array of data": function(body) {
var data = new Object();
body.data([data]);
assert.deepEqual(body.data(), [data]);
assert.strictEqual(body.data()[0], data);
},
// TODO not sure why Node is not catching this error:
"throws an error if data is null or undefined": function(body) {
assert.throws(function() { body.data(null); }, Error);
assert.throws(function() { body.data(function() {}); }, Error);
}
}
}
});
suite.addBatch({
"selectAll(div)": {
topic: load("selection/data").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");
},
"assigns data as an array": function(div) {
var a = new Object(), b = new Object();
div.data([a, b]);
assert.strictEqual(div[0][0].__data__, a);
assert.strictEqual(div[0][1].__data__, b);
},
"assigns data as a function": function(div) {
var a = new Object(), b = new Object();
div.data(function() { return [a, b]; });
assert.strictEqual(div[0][0].__data__, a);
assert.strictEqual(div[0][1].__data__, b);
},
"stores data in the DOM": function(div) {
var a = new Object(), b = new Object(), actual = [];
div[0][0].__data__ = a;
div[0][1].__data__ = b;
div.each(function(d) { actual.push(d); });
assert.deepEqual(actual, [a, b]);
},
"returns a new selection": function(div) {
assert.isFalse(div.data([0, 1]) === div);
},
// TODO not sure why Node is not catching this error:
"throws an error if data is null or undefined": function(div) {
assert.throws(function() { div.data(null); }, Error);
assert.throws(function() { div.data(function() {}); }, Error);
},
"with no arguments, returns an array of data": function(div) {
var a = new Object(), b = new Object(), actual = [];
div[0][0].__data__ = a;
div[0][1].__data__ = b;
assert.deepEqual(div.data(), [a, b]);
},
"with no arguments, returned array has undefined for null nodes": function(div) {
var b = new Object(), actual = [];
div[0][0] = null;
div[0][1].__data__ = b;
var data = div.data();
assert.isUndefined(data[0]);
assert.strictEqual(data[1], b);
assert.equal(data.length, 2);
}
},
"ignores duplicate keys in both data and selection": function(d3) {
var div = d3.select("body").html("").selectAll("div")
.data(["aa", "ab", "ac", "ba", "bb", "bc"])
.enter().append("div")
.text(function(d) { return d; });
var update = div.data(["aa", "ab", "ba", "bb"], function(d) { return d.substring(0, 1); }),
enter = update.enter(),
exit = update.exit();
assert.equal(update.length, 1);
// enter - [ null, null, null, null]
assert.equal(enter[0].length, 4);
assert.equal(enter[0][0], null);
assert.equal(enter[0][1], null);
assert.equal(enter[0][2], null);
assert.equal(enter[0][3], null);
// update - [ aa (a), null, ba (b), null]
assert.equal(update[0].length, 4);
assert.strictEqual(update[0][0], div[0][0]);
assert.equal(update[0][1], null);
assert.strictEqual(update[0][2], div[0][3]);
assert.equal(update[0][3], null);
// exit - [ null, ab (a), ac (a), null, bb (b), bc (b)]
assert.equal(exit[0].length, 6);
assert.equal(exit[0][0], null);
assert.strictEqual(exit[0][1], div[0][1]);
assert.strictEqual(exit[0][2], div[0][2]);
assert.equal(exit[0][3], null);
assert.strictEqual(exit[0][4], div[0][4]);
assert.strictEqual(exit[0][5], div[0][5]);
}
}
});
suite.addBatch({
"selectAll(div).selectAll(span)": {
topic: load("selection/data").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").selectAll("span")
.data([0, 1])
.enter().append("span");
},
"assigns data as an array": function(span) {
var a = new Object(), b = new Object();
span.data([a, b]);
assert.strictEqual(span[0][0].__data__, a);
assert.strictEqual(span[0][1].__data__, b);
assert.strictEqual(span[1][0].__data__, a);
assert.strictEqual(span[1][1].__data__, b);
},
"assigns data as a function": function(span) {
var a = new Object(), b = new Object(), c = new Object(), d = new Object();
span.data(function(z, i) { return i ? [c, d] : [a, b]; });
assert.strictEqual(span[0][0].__data__, a);
assert.strictEqual(span[0][1].__data__, b);
assert.strictEqual(span[1][0].__data__, c);
assert.strictEqual(span[1][1].__data__, d);
},
"evaluates the function once per group": function(span) {
var count = 0;
span.data(function() { ++count; return [0, 1]; });
assert.equal(count, 2);
},
"defines an update selection for updating data": function(span) {
var update = span.data(d3.range(4));
assert.equal(update.length, 2);
assert.equal(update[0].length, 4);
assert.equal(update[1].length, 4);
assert.domEqual(update[0][0], span[0][0]);
assert.domEqual(update[0][1], span[0][1]);
assert.domNull(update[0][2]);
assert.domNull(update[0][3]);
assert.domEqual(update[1][0], span[1][0]);
assert.domEqual(update[1][1], span[1][1]);
assert.domNull(update[1][2]);
assert.domNull(update[1][3]);
},
"defines an enter selection for entering data": function(span) {
var enter = span.data(d3.range(4)).enter();
assert.isFalse(enter.empty());
assert.equal(enter.length, 2);
assert.equal(enter[0].length, 4);
assert.equal(enter[1].length, 4);
assert.domNull(enter[0][0]);
assert.domNull(enter[0][1]);
assert.deepEqual(enter[0][2], {__data__: 2});
assert.deepEqual(enter[0][3], {__data__: 3});
assert.domNull(enter[1][0]);
assert.domNull(enter[1][1]);
assert.deepEqual(enter[1][2], {__data__: 2});
assert.deepEqual(enter[1][3], {__data__: 3});
},
"defines an exit selection for exiting data": function(span) {
var exit = span.data(d3.range(1)).exit();
assert.isFalse(exit.empty());
assert.equal(exit.length, 2);
assert.equal(exit[0].length, 2);
assert.equal(exit[1].length, 2);
assert.domNull(exit[0][0]);
assert.domEqual(exit[0][1], span[0][1]);
assert.domNull(exit[1][0]);
assert.domEqual(exit[1][1], span[1][1]);
},
"observes the specified key function": function(span) {
var update = span.data([1, 2], Number);
assert.isFalse(update.empty());
assert.equal(update.length, 2);
assert.equal(update[0].length, 2);
assert.equal(update[1].length, 2);
assert.domEqual(update[0][0], span[0][1]);
assert.domNull(update[0][1]);
assert.domEqual(update[1][0], span[1][1]);
assert.domNull(update[1][1]);
var enter = update.enter();
assert.equal(enter.length, 2);
assert.equal(enter[0].length, 2);
assert.equal(enter[1].length, 2);
assert.domNull(enter[0][0]);
assert.deepEqual(enter[0][1], {__data__: 2});
assert.domNull(enter[1][0]);
assert.deepEqual(enter[1][1], {__data__: 2});
var exit = update.exit();
assert.equal(exit.length, 2);
assert.equal(exit[0].length, 2);
assert.equal(exit[1].length, 2);
assert.domEqual(exit[0][0], span[0][0]);
assert.domNull(exit[0][1]);
assert.domEqual(exit[1][0], span[1][0]);
assert.domNull(exit[1][1]);
},
"handles keys that are in the default object's prototype chain": function(span) {
// This also applies to the non-standard "watch" and "unwatch" in Mozilla Firefox.
var update = span.data(["hasOwnProperty", "isPrototypeOf", "toLocaleString", "toString", "valueOf"], String);
assert.domNull(update[0][0]);
assert.domNull(update[0][1]);
assert.domNull(update[0][2]);
assert.domNull(update[0][3]);
assert.domNull(update[0][4]);
// This throws an error if Object.hasOwnProperty isn't used.
span.data([0], function() { return "hasOwnProperty"; });
}
}
}
});
suite.export(module);

Просмотреть файл

@ -0,0 +1,97 @@
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.datum");
suite.addBatch({
"select(body)": {
topic: load("selection/datum").sandbox({
document: document,
window: window
}),
"on a simple page": {
topic: function(d3) {
return d3.select("body").html("");
},
"updates the data according to the specified function": function(body) {
body.data([42]).datum(function(d, i) { return d + i; });
assert.equal(document.body.__data__, 42);
},
"updates the data to the specified constant": function(body) {
body.datum(43);
assert.equal(document.body.__data__, 43);
},
"deletes the data if the function returns null": function(body) {
body.data([42]).datum(function() { return null; });
assert.isFalse("__data__" in document.body);
},
"deletes the data if the constant is null": function(body) {
body.data([42]).datum(null);
assert.isFalse("__data__" in document.body);
},
"returns the current selection": function(body) {
assert.isTrue(body.datum(function() { return 1; }) === body);
assert.isTrue(body.datum(2) === body);
},
"with no arguments, returns the first node's datum": function(body) {
body.data([42]);
assert.equal(body.datum(), 42);
}
}
}
});
suite.addBatch({
"selectAll(div)": {
topic: load("selection/datum").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");
},
"updates the data according to the specified function": function(div) {
div.data([42, 43]).datum(function(d, i) { return d + i; });
assert.equal(div[0][0].__data__, 42);
assert.equal(div[0][1].__data__, 44);
},
"updates the data to the specified constant": function(div) {
div.datum(44);
assert.equal(div[0][0].__data__, 44);
assert.equal(div[0][1].__data__, 44);
},
"deletes the data if the function returns null": function(div) {
div.datum(function() { return null; });
assert.isFalse("__data__" in div[0][0]);
assert.isFalse("__data__" in div[0][1]);
},
"deletes the data if the constant is null": function(div) {
div.datum(null);
assert.isFalse("__data__" in div[0][0]);
assert.isFalse("__data__" in div[0][1]);
},
"returns the current selection": function(div) {
assert.isTrue(div.datum(function() { return 1; }) === div);
assert.isTrue(div.datum(2) === div);
}
},
"ignores null nodes": function(d3) {
var div = d3.select("body").html("").selectAll("div").data([0, 1]).enter().append("div"),
some = d3.selectAll("div").data([42, 43]);
some[0][1] = null;
some.datum(function() { return 1; });
assert.equal(div[0][0].__data__, 1);
assert.equal(div[0][1].__data__, 43);
some.datum(2);
assert.equal(div[0][0].__data__, 2);
assert.equal(div[0][1].__data__, 43);
}
}
});
suite.export(module);

Просмотреть файл

@ -0,0 +1,97 @@
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.each");
suite.addBatch({
"select(body)": {
topic: load("selection/each").sandbox({
document: document,
window: window
}),
"on a simple page": {
topic: function(d3) {
return d3.select("body").html("");
},
"calls the function once per element": function(body) {
var count = 0;
body.each(function() { ++count; });
assert.equal(count, 1);
},
"passes the data and index to the function": function(body) {
var data = new Object(), dd, ii;
body.data([data]).each(function(d, i) { dd = d; ii = i; });
assert.isTrue(dd === data);
assert.isTrue(ii === 0);
},
"uses the node as the context": function(body) {
var node;
body.each(function() { node = this; });
assert.isTrue(node === document.body);
},
"returns the same selection": function(body) {
assert.isTrue(body.each(function() {}) === body);
},
"returns the current selection": function(body) {
assert.isTrue(body.each(function() {}) === body);
}
},
"ignores null nodes": function(d3) {
var count = 0, body = d3.select("body");
body[0][0] = null;
body.each(function() { ++count; });
assert.equal(count, 0);
}
}
});
suite.addBatch({
"selectAll(div)": {
topic: load("selection/each").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");
},
"calls the function once per element": function(div) {
var count = 0;
div.each(function() { ++count; });
assert.equal(count, 2);
},
"passes the data and index to the function": function(div) {
var data = [new Object(), new Object()], dd = [], ii = [];
div.data(data).each(function(d, i) { dd.push(d); ii.push(i); });
assert.deepEqual(dd, data);
assert.deepEqual(ii, [0, 1]);
},
"uses the node as the context": function(div) {
var nodes = [];
div.each(function() { nodes.push(this); });
assert.equal(nodes.length, 2);
assert.isTrue(div[0][0] == nodes[0]);
assert.isTrue(div[0][1] == nodes[1]);
},
"returns the same selection": function(div) {
assert.isTrue(div.each(function() {}) === div);
},
"returns the current selection": function(div) {
assert.isTrue(div.each(function() {}) === div);
}
},
"ignores null nodes": function(d3) {
d3.select("body").html("").selectAll("div").data([0, 1]).enter().append("div");
var count = 0, some = d3.selectAll("div");
some[0][0] = null;
some.each(function() { ++count; });
assert.equal(count, 1);
}
}
});
suite.export(module);

Просмотреть файл

@ -0,0 +1,67 @@
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.empty");
suite.addBatch({
"select(body)": {
topic: load("selection/empty").sandbox({
document: document,
window: window
}),
"on a simple page": {
topic: function(d3) {
return d3.select("body").html("");
},
"returns true for empty selections": function(body) {
assert.isTrue(body.select("foo").empty());
},
"returns false for non-empty selections": function(body) {
assert.isFalse(body.empty());
},
},
"ignores null nodes": function(d3) {
var some = d3.select("body");
some[0][0] = null;
assert.isTrue(some.empty());
}
}
});
suite.addBatch({
"selectAll(div)": {
topic: load("selection/empty").sandbox({
document: document,
window: window
}),
"on a simple page": {
topic: function(d3) {
var body = d3.select("body").html("");
body.append("div").append("span");
body.append("div");
return body.selectAll("div");
},
"returns true for empty selections": function(div) {
assert.isTrue(div.select("foo").empty());
},
"returns false for non-empty selections": function(div) {
assert.isFalse(div.empty());
assert.isFalse(div.select("span").empty());
}
},
"ignores null nodes": function(d3) {
var body = d3.select("body").html("");
body.append("div").append("span");
body.append("div");
var some = d3.selectAll("div");
some[0][0] = null;
assert.isTrue(some.select("span").empty());
}
}
});
suite.export(module);

Просмотреть файл

@ -0,0 +1,30 @@
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.enter");
suite.addBatch({
"selectAll(div)": {
topic: load("selection/call").sandbox({
document: document,
window: window
}),
"is an instanceof d3.selection.enter": function(d3) {
var enter = d3.select("body").html("").selectAll("div").data([0, 1]).enter();
assert.instanceOf(enter, d3.selection.enter);
},
"selection prototype can be extended": function(d3) {
var enter = d3.select("body").html("").selectAll("div").data([0, 1]).enter();
d3.selection.enter.prototype.foo = function() { return this.append("foo"); };
var selection = enter.foo();
assert.equal(document.body.innerHTML, "<foo></foo><foo></foo>");
delete d3.selection.enter.prototype.foo;
}
}
});
suite.export(module);

Просмотреть файл

@ -0,0 +1,83 @@
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.filter");
suite.addBatch({
"selectAll(div)": {
topic: load("selection/filter").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")
.selectAll("span")
.data(function(d) { d <<= 1; return [d, d + 1]; })
.enter().append("span");
},
"preserves matching elements": function(span) {
var some = span.filter(function(d, i) { return i === 0; });
assert.isTrue(some[0][0] === span[0][0]);
assert.isTrue(some[1][0] === span[1][0]);
},
"removes non-matching elements": function(span) {
var some = d3.merge(span.filter(function(d, i) { return d & 1; }));
assert.equal(some.indexOf(span[0][0]), -1);
assert.equal(some.indexOf(span[1][0]), -1);
},
"preserves data": function(span) {
var some = span.filter(function(d, i) { return d & 1; });
assert.equal(some[0][0].__data__, 1);
assert.equal(some[1][0].__data__, 3);
},
"preserves grouping": function(span) {
var some = span.filter(function(d, i) { return d & 1; });
assert.equal(some.length, 2);
assert.equal(some[0].length, 1);
assert.equal(some[1].length, 1);
},
"preserves parent node": function(span) {
var some = span.filter(function(d, i) { return d & 1; });
assert.isTrue(some[0].parentNode === span[0].parentNode);
assert.isTrue(some[1].parentNode === span[1].parentNode);
},
"does not preserve index": function(span) {
var indexes = [];
span.filter(function(d, i) { return d & 1; }).each(function(d, i) { indexes.push(i); });
assert.deepEqual(indexes, [0, 0]);
},
"can be specified as a selector": function(span) {
span.classed("foo", function(d, i) { return d & 1; });
var some = span.filter(".foo");
assert.equal(some.length, 2);
assert.equal(some[0].length, 1);
assert.equal(some[1].length, 1);
},
"returns a new selection": function(span) {
assert.isFalse(span.filter(function() { return 1; }) === span);
}
},
"ignores null nodes": function(d3) {
d3.select("body").html("").selectAll("div")
.data([0, 1])
.enter().append("div")
.selectAll("span")
.data(function(d) { d <<= 1; return [d, d + 1]; })
.enter().append("span");
var span = d3.selectAll("span");
span[0][1] = null;
var some = span.filter(function(d, i) { return d & 1; });
assert.isTrue(some[0][0] === span[0][3]);
assert.equal(some.length, 1);
}
}
});
suite.export(module);

144
test/selection/html-test.js Normal file
Просмотреть файл

@ -0,0 +1,144 @@
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.html");
suite.addBatch({
"select(body)": {
topic: load("selection/selection").sandbox({
document: document,
window: window
}),
"on a simple page": {
topic: function(d3) {
return d3.select("body").html("");
},
"sets the inner HTML as a string": function(body) {
body.html("<h1>Hello, world!</h1>");
assert.equal(document.body.firstChild.tagName, "H1");
assert.equal(document.body.firstChild.textContent, "Hello, world!");
},
"sets the inner HTML as a number": function(body) {
body.html(42);
assert.equal(document.body.innerHTML, "42");
assert.equal(document.body.firstChild.nodeType, document.TEXT_NODE);
},
"sets the inner HTML as a function": function(body) {
body.data(["Subject"]).html(function(d, i) { return "<b>" + d + "</b><i>" + i + "</i>"; });
assert.equal(document.body.firstChild.tagName, "B");
assert.equal(document.body.firstChild.textContent, "Subject");
assert.equal(document.body.lastChild.tagName, "I");
assert.equal(document.body.lastChild.textContent, "0");
},
"clears the inner HTML as null": function(body) {
body.html(null);
assert.equal(document.body.innerHTML, "");
assert.isNull(document.body.firstChild);
},
"clears the inner HTML as undefined": function(body) {
body.html(undefined);
assert.equal(document.body.innerHTML, "");
assert.isNull(document.body.firstChild);
},
"clears the inner HTML as the empty string": function(body) {
body.html("");
assert.equal(document.body.innerHTML, "");
assert.isNull(document.body.firstChild);
},
"clears the inner HTML as a function returning the empty string": function(body) {
body.text(function() { return ""; });
assert.equal(document.body.innerHTML, "");
assert.isNull(document.body.firstChild);
},
"clears the inner HTML as a function returning null": function(body) {
body.text(function() { return null; });
assert.equal(document.body.innerHTML, "");
assert.isNull(document.body.firstChild);
},
"clears the inner HTML as a function returning undefined": function(body) {
body.text(function() { return undefined; });
assert.equal(document.body.innerHTML, "");
assert.isNull(document.body.firstChild);
},
"returns the current selection": function(body) {
assert.isTrue(body.html("foo") === body);
}
},
"ignores null nodes": function(d3) {
var body = d3.select("body");
body[0][0] = null;
document.body.innerHTML = "<h1>foo</h1>";
body.html("bar");
assert.equal(document.body.textContent, "foo");
}
}
});
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");
},
"sets the inner HTML as a string": function(div) {
div.html("<h1>Hello, world!</h1>");
assert.equal(div[0][0].firstChild.tagName, "H1");
assert.equal(div[0][0].firstChild.textContent, "Hello, world!");
assert.equal(div[0][1].firstChild.tagName, "H1");
assert.equal(div[0][1].firstChild.textContent, "Hello, world!");
},
"sets the inner HTML as a number": function(div) {
div.html(42);
assert.equal(div[0][0].innerHTML, "42");
assert.equal(div[0][0].firstChild.nodeType, document.TEXT_NODE);
},
"sets the inner HTML as a function": function(div) {
div.data(["foo", "bar"]).html(function(d, i) { return "<b>" + d + "</b><i>" + i + "</i>"; });
assert.equal(div[0][0].firstChild.tagName, "B");
assert.equal(div[0][0].firstChild.textContent, "foo");
assert.equal(div[0][0].lastChild.tagName, "I");
assert.equal(div[0][0].lastChild.textContent, "0");
assert.equal(div[0][1].firstChild.tagName, "B");
assert.equal(div[0][1].firstChild.textContent, "bar");
assert.equal(div[0][1].lastChild.tagName, "I");
assert.equal(div[0][1].lastChild.textContent, "1");
},
"clears the inner HTML as null": function(div) {
div.html(null);
assert.equal(div[0][0].innerHTML, "");
assert.isNull(div[0][0].firstChild);
assert.equal(div[0][1].innerHTML, "");
assert.isNull(div[0][1].firstChild);
},
"clears the inner HTML as a function": function(div) {
div.html(function() { return ""; });
assert.equal(div[0][0].innerHTML, "");
assert.isNull(div[0][0].firstChild);
assert.equal(div[0][1].innerHTML, "");
assert.isNull(div[0][1].firstChild);
},
"returns the current selection": function(div) {
assert.isTrue(div.html("foo") === 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][0] = null;
div[0][0].innerHTML = "<h1>foo</h1>";
some.html("bar");
assert.equal(div[0][0].textContent, "foo");
assert.equal(div[0][1].textContent, "bar");
}
}
});
suite.export(module);

Просмотреть файл

@ -0,0 +1,163 @@
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.insert");
suite.addBatch({
"select(body)": {
topic: load("selection/insert").sandbox({
document: document,
window: window
}),
"on a simple page": {
topic: function(d3) {
return d3.select("body").html("");
},
"inserts before the specified selector": function(body) {
var span = body.html("").append("span");
var div = body.insert("div", "span");
assert.equal(div[0][0].tagName, "DIV");
assert.isNull(div[0][0].namespaceURI);
assert.domEqual(div[0][0], document.body.firstChild);
assert.domEqual(div[0][0].nextSibling, span[0][0]);
},
"inserts before the specified node": function(body) {
var span = body.html("").append("span");
var div = body.insert("div", function() { return span.node(); });
assert.equal(div[0][0].tagName, "DIV");
assert.isNull(div[0][0].namespaceURI);
assert.domEqual(div[0][0], document.body.firstChild);
assert.domEqual(div[0][0].nextSibling, span[0][0]);
},
"appends an HTML element": function(body) {
var div = body.insert("div");
assert.equal(div[0][0].tagName, "DIV");
assert.isNull(div[0][0].namespaceURI);
assert.domEqual(div[0][0], document.body.lastChild);
},
"appends an SVG element": function(body) {
var svg = body.insert("svg:svg");
assert.equal(svg[0][0].tagName, "SVG");
assert.equal(svg[0][0].namespaceURI, "http://www.w3.org/2000/svg");
assert.domEqual(svg[0][0].parentNode, document.body);
assert.domEqual(svg[0][0], document.body.lastChild);
},
"propagates data to new element": function(body) {
var data = new Object(), div = body.data([data]).insert("div");
assert.strictEqual(div[0][0].__data__, data);
},
"returns a new selection": function(body) {
assert.isFalse(body.insert("div") === body);
},
"inherits namespace from parent node": function(body) {
var g = body.insert("svg:svg").insert("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().insert("div");
},
"appends an HTML element": function(div) {
var span = div.insert("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.domEqual(span[0][0].parentNode, div[0][0]);
assert.domEqual(span[0][1].parentNode, div[0][1]);
assert.domEqual(div[0][0].lastChild, span[0][0]);
assert.domEqual(div[0][1].lastChild, span[0][1]);
},
"appends an SVG element": function(div) {
var svg = div.insert("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.domEqual(svg[0][0].parentNode, div[0][0]);
assert.domEqual(svg[0][1].parentNode, div[0][1]);
assert.domEqual(div[0][0].lastChild, svg[0][0]);
assert.domEqual(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]).insert("span");
assert.strictEqual(span[0][0].__data__, a);
assert.strictEqual(span[0][1].__data__, b);
},
"returns a new selection": function(div) {
assert.isFalse(div.insert("div") === div);
}
},
"ignores null nodes": function(d3) {
var div = d3.select("body").html("").selectAll("div").data([0, 1]).enter().insert("div"),
some = d3.selectAll("div");
some[0][1] = null;
var span = some.insert("span");
assert.equal(span[0].length, 2);
assert.equal(span[0][0].tagName, "SPAN");
assert.domNull(span[0][1]);
assert.domEqual(span[0][0].parentNode, div[0][0]);
assert.domEqual(div[0][0].lastChild, span[0][0]);
assert.domNull(div[0][1].lastChild);
}
}
});
suite.addBatch({
"selectAll(div).data(…).enter()": {
topic: load("selection/selection").sandbox({
document: document,
window: window
}),
"on a simple page": {
topic: function(d3) {
return d3.select("body");
},
"inserts before the specified selector": function(body) {
var span = body.html("").append("span");
var div = body.selectAll("div").data([0, 1]).enter().insert("div", "span");
assert.equal(div.length, 1);
assert.equal(div[0].length, 2);
assert.domEqual(div[0][0], document.body.firstChild);
assert.domEqual(div[0][1].previousSibling, div[0][0]);
assert.domEqual(div[0][1].nextSibling, span[0][0]);
},
"propagates data to new elements": function(body) {
var a = new Object(), b = new Object(), div = body.html("").selectAll("div").data([a, b]).enter().insert("div");
assert.strictEqual(div[0][0].__data__, a);
assert.strictEqual(div[0][1].__data__, b);
},
"ignores null nodes": function(body) {
body.html("").insert("div");
var div = body.selectAll("div").data([0, 1, 2]).enter().insert("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);
},
"returns a new selection": function(body) {
var enter = body.html("").selectAll("div").data([0, 1]).enter();
assert.isFalse(enter.insert("div") === enter);
}
}
}
});
suite.export(module);

Просмотреть файл

@ -0,0 +1,66 @@
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.node");
suite.addBatch({
"select(body)": {
topic: load("selection/node").sandbox({
document: document,
window: window
}),
"on a simple page": {
topic: function(d3) {
return d3.select("body").html("");
},
"returns null for empty selections": function(body) {
assert.isNull(body.select("foo").node());
},
"returns the first element for non-empty selections": function(body) {
assert.isTrue(body.node() === document.body);
}
},
"ignores null nodes": function(d3) {
var some = d3.select("body");
some[0][0] = null;
assert.isNull(some.node());
}
}
});
suite.addBatch({
"selectAll(div)": {
topic: load("selection/node").sandbox({
document: document,
window: window
}),
"on a simple page": {
topic: function(d3) {
var body = d3.select("body").html("");
body.append("div").append("span");
body.append("div");
return body.selectAll("div");
},
"returns null for empty selections": function(div) {
assert.isNull(div.select("foo").node());
},
"returns the first element for non-empty selections": function(div) {
assert.isTrue(div.node() === div[0][0]);
},
},
"ignores null nodes": function(d3) {
var body = d3.select("body").html("");
body.append("div").append("span");
body.append("div");
var some = body.selectAll("div");
some[0][0] = null;
assert.isTrue(some.node() === some[0][1]);
}
}
});
suite.export(module);

141
test/selection/on-test.js Normal file
Просмотреть файл

@ -0,0 +1,141 @@
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.on");
suite.addBatch({
"select(body)": {
topic: load("selection/on").sandbox({
document: document,
window: window
}),
"on a simple page": {
topic: function(d3) {
return d3.select("body").html("");
},
"registers an event listener for the specified type": function(body) {
var form = body.append("form"), count = 0;
form.on("submit", function() { ++count; }); // jsdom has spotty event support
form.append("input").attr("type", "submit").node().click();
assert.equal(count, 1);
},
"replaces an existing event listener for the same type": function(body) {
var form = body.append("form"), count = 0, fail = 0;
form.on("submit", function() { ++fail; });
form.on("submit", function() { ++count; });
form.append("input").attr("type", "submit").node().click();
assert.equal(count, 1);
assert.equal(fail, 0);
},
"removes an existing event listener": function(body) {
var form = body.append("form"), fail = 0;
form.on("submit", function() { ++fail; });
form.on("submit", null);
form.append("input").attr("type", "submit").node().click();
assert.equal(fail, 0);
assert.isUndefined(form.on("submit"));
},
/* Regrettably, JSDOM ignores the capture flag, so we can't test this yet
"removing a listener doesn't require the capture flag": function(body) {
var form = body.append("form"), fail = 0;
form.on("submit", function() { ++fail; }, true);
form.on("submit", null);
form.append("input").attr("type", "submit").node().click();
assert.equal(fail, 0);
assert.isUndefined(form.on("submit"));
},
*/
"ignores removal of non-matching event listener": function(body) {
body.append("form").on("submit", null);
},
"observes the specified namespace": function(body) {
var form = body.append("form"), foo = 0, bar = 0;
form.on("submit.foo", function() { ++foo; });
form.on({"submit.bar": function() { ++bar; }});
form.append("input").attr("type", "submit").node().click();
assert.equal(foo, 1);
assert.equal(bar, 1);
},
"can register listeners as a map": function(body) {
var form = body.append("form"), count = 0, fail = 0;
form.on({submit: function() { ++fail; }});
form.on({submit: function() { ++count; }});
form.append("input").attr("type", "submit").node().click();
assert.equal(count, 1);
assert.equal(fail, 0);
form.on({submit: null});
assert.isUndefined(form.on("submit"));
},
/* Not really sure how to test this one
"observes the specified capture flag": function(body) {
},
*/
"passes the current data and index to the event listener": function(body) {
var forms = body.html("").selectAll("form").data(["a", "b"]).enter().append("form"), dd, ii, data = new Object();
forms.on("submit", function(d, i) { dd = d; ii = i; });
forms.append("input").attr("type", "submit")[0][1].click();
assert.equal(dd, "b");
assert.equal(ii, 1);
forms[0][1].__data__ = data;
forms.append("input").attr("type", "submit")[0][1].click();
assert.equal(dd, data);
assert.equal(ii, 1);
},
"uses the current element as the context": function(body) {
var forms = body.html("").selectAll("form").data(["a", "b"]).enter().append("form"), context;
forms.on("submit", function() { context = this; });
forms.append("input").attr("type", "submit")[0][1].click();
assert.domEqual(context, forms[0][1]);
},
"returns the current selection": function(body) {
assert.isTrue(body.on("submit", function() {}) === body);
},
"returns the assigned listener if called with one argument": function(body) {
body.on("mouseover", f).on("click.foo", f);
function f() {}
assert.equal(body.on("mouseover"), f);
assert.equal(body.on("click.foo"), f);
assert.isUndefined(body.on("click"));
assert.isUndefined(body.on("mouseover.foo"));
},
"omitting the event type": {
"returns undefined when retrieving a listener": function(body) {
assert.isUndefined(body.on(".foo"));
},
"null removes all named event listeners": function(body) {
body.on("mouseover.foo", f)
.on("click.foo", f)
.on("click.foobar", f)
.on(".foo", null);
function f() {}
assert.isUndefined(body.on("mouseover.foo"));
assert.isUndefined(body.on("click.foo"));
assert.equal(body.on("click.foobar"), f);
},
"no-op when setting a listener": function(body) {
assert.isTrue(body.on(".foo", function() {}) === body);
assert.isUndefined(body.on(".foo"));
}
}
},
"sets the current event as d3.event": function(d3) {
var form = d3.select("body").append("form"), event;
form.on("submit", function() { event = d3.event; });
form.append("input").attr("type", "submit").node().click();
assert.equal(event.type, "submit");
assert.domEqual(event.target, form[0][0]);
},
"restores the original event after notifying listeners": function(d3) {
var form = d3.select("body").append("form"), event = d3.event = new Object();
form.on("submit", function() {});
form.append("input").attr("type", "submit").node().click();
assert.equal(d3.event, event);
}
}
});
suite.export(module);

Просмотреть файл

@ -0,0 +1,39 @@
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.order");
suite.addBatch({
"selectAll(div)": {
topic: load("selection/call").sandbox({
document: document,
window: window
}),
"on a simple page": {
topic: function(d3) {
return d3.select("body").html("").selectAll("div")
.data([1, 2, 10, 20])
.enter().append("div")
.attr("id", String);
},
"orders elements by data": function(div) {
div = div.data([1, 10, 20, 2], String).order();
assert.domNull(div[0][0].previousSibling);
assert.domEqual(div[0][1].previousSibling, div[0][0]);
assert.domEqual(div[0][2].previousSibling, div[0][1]);
assert.domEqual(div[0][3].previousSibling, div[0][2]);
assert.domNull(div[0][3].nextSibling);
},
"returns the current selection": function(span) {
span = d3.select("body"); // https://github.com/tmpvar/jsdom/issues/277
assert.isTrue(span.order() === span);
}
}
}
});
suite.export(module);

Просмотреть файл

@ -0,0 +1,125 @@
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.property");
suite.addBatch({
"select(body)": {
topic: load("selection/property").sandbox({
document: document,
window: window
}),
"on a simple page": {
topic: function(d3) {
return d3.select("body").html("");
},
"sets a property as a string": function(body) {
body.property("bgcolor", "red");
assert.equal(document.body.bgcolor, "red");
},
"sets a property as a number": function(body) {
body.property("opacity", 1);
assert.equal(document.body.opacity, "1");
},
"sets a property as a function": function(body) {
body.property("bgcolor", function() { return "orange"; });
assert.equal(document.body.bgcolor, "orange");
},
"sets properties as a map of constants": function(body) {
body.property({bgcolor: "purple", opacity: .41});
assert.equal(document.body.bgcolor, "purple");
assert.equal(document.body.opacity, .41);
},
"sets properties as a map of functions": function(body) {
body.data(["cyan"]).property({bgcolor: String, opacity: function(d, i) { return i; }});
assert.equal(document.body.bgcolor, "cyan");
assert.equal(document.body.opacity, 0);
},
"gets a property value": function(body) {
document.body.bgcolor = "yellow";
assert.equal(body.property("bgcolor"), "yellow");
},
"removes a property as null": function(body) {
body.property("bgcolor", "yellow").property("bgcolor", null);
assert.isFalse("bgcolor" in document.body);
},
"removes a property as a function": function(body) {
body.property("bgcolor", "yellow").property("bgcolor", function() { return null });
assert.isFalse("bgcolor" in document.body);
},
"removes properties as a map of nulls": function(body) {
document.body.bgcolor = "red";
body.property({bgcolor: null});
assert.isFalse("bgcolor" in document.body);
},
"removes properties as a map of functions that return null": function(body) {
document.body.bgcolor = "red";
body.property({bgcolor: function() {}});
assert.isFalse("bgcolor" in document.body);
},
"returns the current selection": function(body) {
assert.isTrue(body.property("bgcolor", "yellow") === body);
}
}
}
});
suite.addBatch({
"selectAll(div)": {
topic: load("selection/property").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 a property as a string": function(div) {
div.property("bgcolor", "red");
assert.equal(div[0][0].bgcolor, "red");
assert.equal(div[0][1].bgcolor, "red");
},
"sets a property as a number": function(div) {
div.property("opacity", 0.4);
assert.equal(div[0][0].opacity, "0.4");
assert.equal(div[0][1].opacity, "0.4");
},
"sets a property as a function": function(div) {
div.property("bgcolor", d3.interpolateRgb("brown", "steelblue"));
assert.equal(div[0][0].bgcolor, "#a52a2a");
assert.equal(div[0][1].bgcolor, "#4682b4");
},
"gets a property value": function(div) {
div[0][0].bgcolor = "purple";
assert.equal(div.property("bgcolor"), "purple");
},
"removes a property as null": function(div) {
div.property("bgcolor", "yellow").property("bgcolor", null);
assert.isFalse("bgcolor" in div[0][0]);
assert.isFalse("bgcolor" in div[0][1]);
},
"removes a property as a function": function(div) {
div.property("bgcolor", "yellow").property("bgcolor", function() { return null });
assert.isFalse("bgcolor" in div[0][0]);
assert.isFalse("bgcolor" in div[0][1]);
},
"returns the current selection": function(div) {
assert.isTrue(div.property("bgcolor", "yellow") === 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.property("bgcolor", null).property("bgcolor", "red");
assert.equal(div[0][0].bgcolor, "red");
assert.isFalse("bgcolor" in div[0][1]);
}
}
});
suite.export(module);

Просмотреть файл

@ -0,0 +1,42 @@
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.remove");
suite.addBatch({
"select(body)": {
topic: load("selection/remove").sandbox({
document: document,
window: window
}),
"removes the matching elements": function(d3) {
var div = d3.select("body").append("div");
div.remove();
assert.domNull(div[0][0].parentNode);
},
"does not remove non-matching elements": function(d3) {
var div1 = d3.select("body").append("div"),
div2 = d3.select("body").append("div");
div1.remove();
assert.domEqual(div2[0][0].parentNode, document.body);
},
"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][0] = null;
some.remove();
assert.domEqual(div[0][0].parentNode, document.body);
assert.domNull(div[0][1].parentNode);
},
"returns the current selection": function(d3) {
var div = d3.select("body").append("div");
assert.isTrue(div.remove() === div);
}
}
});
suite.export(module);

Просмотреть файл

@ -1,9 +1,9 @@
var vows = require("vows"),
d3 = require("../../"),
load = require("../load"),
assert = require("../env-assert"),
jsdom = require("jsdom").jsdom,
document = jsdom("<html><head></head><body></body></html>"),
window = document.createWindow();
document = d3.selection().node()._ownerDocument,
window = document.defaultView;
var suite = vows.describe("d3.select");
@ -12,33 +12,33 @@ suite.addBatch({
topic: load("selection/select").sandbox({
document: document,
window: window
}),
}).expression("d3.select"),
"on a simple page": {
topic: function(d3) {
var body = d3.select("body").html("");
topic: function(select) {
var body = select("body").html("");
body.append("span").attr("class", "f00").attr("id", "b4r").attr("name", "b4z");
body.append("div").attr("class", "foo").attr("id", "bar").attr("name", "baz");
return d3;
return select;
},
"selects by element name": function(d3) {
var div = d3.select("div");
"selects by element name": function(select) {
var div = select("div");
assert.equal(div[0][0].tagName, "DIV");
},
"selects by class name": function(d3) {
var div = d3.select(".foo");
"selects by class name": function(select) {
var div = select(".foo");
assert.equal(div[0][0].className, "foo");
},
"selects by id": function(d3) {
var div = d3.select("div#bar");
"selects by id": function(select) {
var div = select("div#bar");
assert.equal(div[0][0].id, "bar");
},
"selects by attribute value": function(d3) {
var div = d3.select("[name=baz]");
"selects by attribute value": function(select) {
var div = select("[name=baz]");
assert.equal(div[0][0].getAttribute("name"), "baz");
},
"selects by node": function(d3) {
var div = d3.select(document.body.lastChild);
"selects by node": function(select) {
var div = select(document.body.lastChild);
assert.isTrue(div[0][0] === document.body.lastChild);
assert.lengthOf(div, 1);
assert.lengthOf(div[0], 1);

Просмотреть файл

@ -1,43 +1,51 @@
require("../env");
var vows = require("vows"),
assert = require("../env-assert");
d3 = require("../../"),
load = require("../load"),
assert = require("../env-assert"),
document = d3.selection().node()._ownerDocument,
window = document.defaultView;
var suite = vows.describe("d3.selectAll");
suite.addBatch({
"selectAll": {
topic: function() {
var body = d3.select("body").html("");
body.append("span").attr("class", "f00").attr("id", "b4r").attr("name", "b4z");
body.append("div").attr("class", "foo").attr("id", "bar").attr("name", "baz");
return body;
},
"selects by element name": function() {
var div = d3.selectAll("div");
assert.equal(div[0][0].tagName, "DIV");
},
"selects by class name": function() {
var div = d3.selectAll(".foo");
assert.equal(div[0][0].className, "foo");
},
"selects by id": function() {
var div = d3.select("div#bar");
assert.equal(div[0][0].id, "bar");
},
"selects by attribute value": function() {
var div = d3.selectAll("[name=baz]");
assert.equal(div[0][0].getAttribute("name"), "baz");
},
"selects by array": function() {
var div = d3.selectAll([document.body.lastChild]);
assert.isTrue(div[0][0] === document.body.lastChild);
assert.lengthOf(div, 1);
assert.lengthOf(div[0], 1);
},
"groups are not instances of NodeList": function() {
var div = d3.select("body").selectAll(function() { return this.getElementsByClassName("div"); });
assert.isFalse(div[0] instanceof window.NodeList);
topic: load("selection/selectAll").sandbox({
document: document,
window: window
}),
"on a simple page": {
topic: function(d3) {
var body = d3.select("body").html("");
body.append("span").attr("class", "f00").attr("id", "b4r").attr("name", "b4z");
body.append("div").attr("class", "foo").attr("id", "bar").attr("name", "baz");
return d3;
},
"selects by element name": function(d3) {
var div = d3.selectAll("div");
assert.equal(div[0][0].tagName, "DIV");
},
"selects by class name": function(d3) {
var div = d3.selectAll(".foo");
assert.equal(div[0][0].className, "foo");
},
"selects by id": function(d3) {
var div = d3.selectAll("div#bar");
assert.equal(div[0][0].id, "bar");
},
"selects by attribute value": function(d3) {
var div = d3.selectAll("[name=baz]");
assert.equal(div[0][0].getAttribute("name"), "baz");
},
"selects by array": function(d3) {
var div = d3.selectAll([document.body.lastChild]);
assert.isTrue(div[0][0] === document.body.lastChild);
assert.lengthOf(div, 1);
assert.lengthOf(div[0], 1);
},
"groups are not instances of NodeList": function(d3) {
var div = d3.select("body").selectAll(function() { return this.getElementsByClassName("div"); });
assert.isFalse(div[0] instanceof window.NodeList);
}
}
}
});

Просмотреть файл

@ -1,122 +0,0 @@
require("../env");
var vows = require("vows"),
assert = require("../env-assert");
var suite = vows.describe("selection.append");
suite.addBatch({
"select(body)": {
topic: function() {
return d3.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: function() {
return d3.select("body").html("").selectAll("div").data(d3.range(2)).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]);
},
"ignores null nodes": function(div) {
div.html("");
var 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);
},
"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);
}
}
});
suite.addBatch({
"selectAll(div).data(…).enter()": {
topic: function() {
return d3.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);

Просмотреть файл

@ -1,175 +0,0 @@
require("../env");
var vows = require("vows"),
assert = require("../env-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");
},
"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: 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.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"));
},
"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.isNull(div[0][1].getAttribute("href"));
},
"returns the current selection": function(div) {
assert.isTrue(div.attr("foo", "bar") === div);
}
}
});
suite.export(module);

Просмотреть файл

@ -1,70 +0,0 @@
require("../env");
var vows = require("vows"),
assert = require("../env-assert");
var suite = vows.describe("selection.call");
suite.addBatch({
"select(body)": {
topic: function() {
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: function() {
return d3.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);

Просмотреть файл

@ -1,263 +0,0 @@
require("../env");
var vows = require("vows"),
assert = require("../env-assert");
var suite = vows.describe("selection.call");
suite.addBatch({
"select(body)": {
topic: function() {
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) {
body.attr("class", null);
body.classed({"foo bar": function() { return true; }});
assert.equal(document.body.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");
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(count, 1);
},
"returns the current selection": function(body) {
assert.isTrue(body.classed("foo", true) === body);
}
}
});
suite.addBatch({
"selectAll(div)": {
topic: function() {
return d3.select("body").html("").selectAll("div").data(d3.range(2)).enter().append("div");
},
"adds a missing class as true": function(div) {
div.attr("class", null);
div.classed("foo", true);
assert.equal(div[0][0].className, "foo");
assert.equal(div[0][1].className, "foo");
div.classed("bar", true);
assert.equal(div[0][0].className, "foo bar");
assert.equal(div[0][1].className, "foo bar");
},
"adds a missing class as a function": function(div) {
div.data([0, 1]).attr("class", null);
div.classed("foo", function(d, i) { return d === 0; });
assert.equal(div[0][0].className, "foo");
assert.equal(div[0][1].className, "");
div.classed("bar", function(d, i) { return i === 1; });
assert.equal(div[0][0].className, "foo");
assert.equal(div[0][1].className, "bar");
},
"removes an existing class as false": function(div) {
div.attr("class", "bar foo");
div.classed("foo", false);
assert.equal(div[0][0].className, "bar");
assert.equal(div[0][1].className, "bar");
div.classed("bar", false);
assert.equal(div[0][0].className, "");
assert.equal(div[0][1].className, "");
},
"removes an existing class as a function": function(div) {
div.data([0, 1]).attr("class", "bar foo");
div.classed("foo", function(d, i) { return d === 0; });
assert.equal(div[0][0].className, "bar foo");
assert.equal(div[0][1].className, "bar");
div.classed("bar", function(d, i) { return i === 1; });
assert.equal(div[0][0].className, "foo");
assert.equal(div[0][1].className, "bar");
div.classed("foo", function() { return false; });
assert.equal(div[0][0].className, "");
assert.equal(div[0][1].className, "bar");
div.classed("bar", function() { return false; });
assert.equal(div[0][0].className, "");
assert.equal(div[0][1].className, "");
},
"preserves an existing class as true": function(div) {
div.attr("class", "bar foo");
div.classed("foo", true);
assert.equal(div[0][0].className, "bar foo");
assert.equal(div[0][1].className, "bar foo");
div.classed("bar", true);
assert.equal(div[0][0].className, "bar foo");
assert.equal(div[0][1].className, "bar foo");
},
"preserves an existing class as a function": function(div) {
div.attr("class", "bar foo");
div.classed("foo", function() { return true; });
assert.equal(div[0][0].className, "bar foo");
assert.equal(div[0][1].className, "bar foo");
div.classed("bar", function() { return true; });
assert.equal(div[0][0].className, "bar foo");
assert.equal(div[0][1].className, "bar foo");
},
"preserves a missing class as false": function(div) {
div.attr("class", "baz");
div.classed("foo", false);
assert.equal(div[0][0].className, "baz");
assert.equal(div[0][1].className, "baz");
div.attr("class", null);
div.classed("bar", false);
assert.equal(div[0][0].className, "");
assert.equal(div[0][1].className, "");
},
"preserves a missing class as a function": function(div) {
div.attr("class", "baz");
div.classed("foo", function() { return false; });
assert.equal(div[0][0].className, "baz");
assert.equal(div[0][1].className, "baz");
div.attr("class", null);
div.classed("bar", function() { return false; });
assert.equal(div[0][0].className, "");
assert.equal(div[0][1].className, "");
},
"gets an existing class": function(div) {
div[0][0].className = " foo\tbar baz";
assert.isTrue(div.classed("foo"));
assert.isTrue(div.classed("bar"));
assert.isTrue(div.classed("baz"));
},
"does not get a missing class": function(div) {
div[0][0].className = " foo\tbar baz";
assert.isFalse(div.classed("foob"));
assert.isFalse(div.classed("bare"));
assert.isFalse(div.classed("rbaz"));
},
"ignores null nodes": function(div) {
var some = d3.selectAll("div");
some[0][1] = null;
some.attr("class", null).classed("foo", true);
assert.equal(div[0][0].className, "foo");
assert.equal(div[0][1].className, "");
},
"returns the current selection": function(div) {
assert.isTrue(div.classed("foo", true) === div);
}
}
});
suite.export(module);

Просмотреть файл

@ -1,242 +0,0 @@
require("../env");
var vows = require("vows"),
assert = require("../env-assert");
var suite = vows.describe("selection.data");
suite.addBatch({
"select(body)": {
topic: function() {
return d3.select("body").html("");
},
"assigns data as an array": function(body) {
var data = new Object();
body.data([data]);
assert.strictEqual(document.body.__data__, data);
},
"assigns data as a function": function(body) {
var data = new Object();
body.data(function() { return [data]; });
assert.strictEqual(document.body.__data__, data);
},
"stores data in the DOM": function(body) {
var expected = new Object(), actual;
document.body.__data__ = expected;
body.each(function(d) { actual = d; });
assert.strictEqual(actual, expected);
},
"returns a new selection": function(body) {
assert.isFalse(body.data([1]) === body);
},
"with no arguments, returns an array of data": function(body) {
var data = new Object();
body.data([data]);
assert.deepEqual(body.data(), [data]);
assert.strictEqual(body.data()[0], data);
},
"throws an error if data is null or undefined": function(body) {
assert.throws(function() { body.data(null); }, Error);
assert.throws(function() { body.data(function() {}); }, Error);
}
}
});
suite.addBatch({
"selectAll(div)": {
topic: function() {
return d3.select("body").html("").selectAll("div").data(d3.range(2)).enter().append("div");
},
"assigns data as an array": function(div) {
var a = new Object(), b = new Object();
div.data([a, b]);
assert.strictEqual(div[0][0].__data__, a);
assert.strictEqual(div[0][1].__data__, b);
},
"assigns data as a function": function(div) {
var a = new Object(), b = new Object();
div.data(function() { return [a, b]; });
assert.strictEqual(div[0][0].__data__, a);
assert.strictEqual(div[0][1].__data__, b);
},
"stores data in the DOM": function(div) {
var a = new Object(), b = new Object(), actual = [];
div[0][0].__data__ = a;
div[0][1].__data__ = b;
div.each(function(d) { actual.push(d); });
assert.deepEqual(actual, [a, b]);
},
"returns a new selection": function(div) {
assert.isFalse(div.data([0, 1]) === div);
},
"throws an error if data is null or undefined": function(div) {
assert.throws(function() { div.data(null); }, Error);
assert.throws(function() { div.data(function() {}); }, Error);
},
"with no arguments, returns an array of data": function(div) {
var a = new Object(), b = new Object(), actual = [];
div[0][0].__data__ = a;
div[0][1].__data__ = b;
assert.deepEqual(div.data(), [a, b]);
},
"with no arguments, returned array has undefined for null nodes": function(div) {
var b = new Object(), actual = [];
div[0][0] = null;
div[0][1].__data__ = b;
var data = div.data();
assert.isUndefined(data[0]);
assert.strictEqual(data[1], b);
assert.equal(data.length, 2);
},
"ignores duplicate keys in both data and selection": function() {
var div = d3.select("body").html("").selectAll("div")
.data(["aa", "ab", "ac", "ba", "bb", "bc"])
.enter().append("div")
.text(function(d) { return d; });
var update = div.data(["aa", "ab", "ba", "bb"], function(d) { return d.substring(0, 1); }),
enter = update.enter(),
exit = update.exit();
assert.equal(update.length, 1);
// enter - [ null, null, null, null]
assert.equal(enter[0].length, 4);
assert.equal(enter[0][0], null);
assert.equal(enter[0][1], null);
assert.equal(enter[0][2], null);
assert.equal(enter[0][3], null);
// update - [ aa (a), null, ba (b), null]
assert.equal(update[0].length, 4);
assert.strictEqual(update[0][0], div[0][0]);
assert.equal(update[0][1], null);
assert.strictEqual(update[0][2], div[0][3]);
assert.equal(update[0][3], null);
// exit - [ null, ab (a), ac (a), null, bb (b), bc (b)]
assert.equal(exit[0].length, 6);
assert.equal(exit[0][0], null);
assert.strictEqual(exit[0][1], div[0][1]);
assert.strictEqual(exit[0][2], div[0][2]);
assert.equal(exit[0][3], null);
assert.strictEqual(exit[0][4], div[0][4]);
assert.strictEqual(exit[0][5], div[0][5]);
}
}
});
suite.addBatch({
"selectAll(div).selectAll(span)": {
topic: function() {
return d3.select("body").html("").selectAll("div")
.data(d3.range(2))
.enter().append("div").selectAll("span")
.data(d3.range(2))
.enter().append("span");
},
"assigns data as an array": function(span) {
var a = new Object(), b = new Object();
span.data([a, b]);
assert.strictEqual(span[0][0].__data__, a);
assert.strictEqual(span[0][1].__data__, b);
assert.strictEqual(span[1][0].__data__, a);
assert.strictEqual(span[1][1].__data__, b);
},
"assigns data as a function": function(span) {
var a = new Object(), b = new Object(), c = new Object(), d = new Object();
span.data(function(z, i) { return i ? [c, d] : [a, b]; });
assert.strictEqual(span[0][0].__data__, a);
assert.strictEqual(span[0][1].__data__, b);
assert.strictEqual(span[1][0].__data__, c);
assert.strictEqual(span[1][1].__data__, d);
},
"evaluates the function once per group": function(span) {
var count = 0;
span.data(function() { ++count; return [0, 1]; });
assert.equal(count, 2);
},
"defines an update selection for updating data": function(span) {
var update = span.data(d3.range(4));
assert.equal(update.length, 2);
assert.equal(update[0].length, 4);
assert.equal(update[1].length, 4);
assert.domEqual(update[0][0], span[0][0]);
assert.domEqual(update[0][1], span[0][1]);
assert.domNull(update[0][2]);
assert.domNull(update[0][3]);
assert.domEqual(update[1][0], span[1][0]);
assert.domEqual(update[1][1], span[1][1]);
assert.domNull(update[1][2]);
assert.domNull(update[1][3]);
},
"defines an enter selection for entering data": function(span) {
var enter = span.data(d3.range(4)).enter();
assert.isFalse(enter.empty());
assert.equal(enter.length, 2);
assert.equal(enter[0].length, 4);
assert.equal(enter[1].length, 4);
assert.domNull(enter[0][0]);
assert.domNull(enter[0][1]);
assert.deepEqual(enter[0][2], {__data__: 2});
assert.deepEqual(enter[0][3], {__data__: 3});
assert.domNull(enter[1][0]);
assert.domNull(enter[1][1]);
assert.deepEqual(enter[1][2], {__data__: 2});
assert.deepEqual(enter[1][3], {__data__: 3});
},
"defines an exit selection for exiting data": function(span) {
var exit = span.data(d3.range(1)).exit();
assert.isFalse(exit.empty());
assert.equal(exit.length, 2);
assert.equal(exit[0].length, 2);
assert.equal(exit[1].length, 2);
assert.domNull(exit[0][0]);
assert.domEqual(exit[0][1], span[0][1]);
assert.domNull(exit[1][0]);
assert.domEqual(exit[1][1], span[1][1]);
},
"observes the specified key function": function(span) {
var update = span.data([1, 2], Number);
assert.isFalse(update.empty());
assert.equal(update.length, 2);
assert.equal(update[0].length, 2);
assert.equal(update[1].length, 2);
assert.domEqual(update[0][0], span[0][1]);
assert.domNull(update[0][1]);
assert.domEqual(update[1][0], span[1][1]);
assert.domNull(update[1][1]);
var enter = update.enter();
assert.equal(enter.length, 2);
assert.equal(enter[0].length, 2);
assert.equal(enter[1].length, 2);
assert.domNull(enter[0][0]);
assert.deepEqual(enter[0][1], {__data__: 2});
assert.domNull(enter[1][0]);
assert.deepEqual(enter[1][1], {__data__: 2});
var exit = update.exit();
assert.equal(exit.length, 2);
assert.equal(exit[0].length, 2);
assert.equal(exit[1].length, 2);
assert.domEqual(exit[0][0], span[0][0]);
assert.domNull(exit[0][1]);
assert.domEqual(exit[1][0], span[1][0]);
assert.domNull(exit[1][1]);
},
"handles keys that are in the default object's prototype chain": function(span) {
// This also applies to the non-standard "watch" and "unwatch" in Mozilla Firefox.
var update = span.data(["hasOwnProperty", "isPrototypeOf", "toLocaleString", "toString", "valueOf"], String);
assert.domNull(update[0][0]);
assert.domNull(update[0][1]);
assert.domNull(update[0][2]);
assert.domNull(update[0][3]);
assert.domNull(update[0][4]);
// This throws an error if Object.hasOwnProperty isn't used.
span.data([0], function() { return "hasOwnProperty"; });
}
}
});
suite.export(module);

Просмотреть файл

@ -1,82 +0,0 @@
require("../env");
var vows = require("vows"),
assert = require("../env-assert");
var suite = vows.describe("selection.datum");
suite.addBatch({
"select(body)": {
topic: function() {
return d3.select("body").html("");
},
"updates the data according to the specified function": function(body) {
body.data([42]).datum(function(d, i) { return d + i; });
assert.equal(document.body.__data__, 42);
},
"updates the data to the specified constant": function(body) {
body.datum(43);
assert.equal(document.body.__data__, 43);
},
"deletes the data if the function returns null": function(body) {
body.data([42]).datum(function() { return null; });
assert.isFalse("__data__" in document.body);
},
"deletes the data if the constant is null": function(body) {
body.data([42]).datum(null);
assert.isFalse("__data__" in document.body);
},
"returns the current selection": function(body) {
assert.isTrue(body.datum(function() { return 1; }) === body);
assert.isTrue(body.datum(2) === body);
},
"with no arguments, returns the first node's datum": function(body) {
body.data([42]);
assert.equal(body.datum(), 42);
}
}
});
suite.addBatch({
"selectAll(div)": {
topic: function() {
return d3.select("body").html("").selectAll("div").data(d3.range(2)).enter().append("div");
},
"updates the data according to the specified function": function(div) {
div.data([42, 43]).datum(function(d, i) { return d + i; });
assert.equal(div[0][0].__data__, 42);
assert.equal(div[0][1].__data__, 44);
},
"updates the data to the specified constant": function(div) {
div.datum(44);
assert.equal(div[0][0].__data__, 44);
assert.equal(div[0][1].__data__, 44);
},
"deletes the data if the function returns null": function(div) {
div.datum(function() { return null; });
assert.isFalse("__data__" in div[0][0]);
assert.isFalse("__data__" in div[0][1]);
},
"deletes the data if the constant is null": function(div) {
div.datum(null);
assert.isFalse("__data__" in div[0][0]);
assert.isFalse("__data__" in div[0][1]);
},
"returns the current selection": function(div) {
assert.isTrue(div.datum(function() { return 1; }) === div);
assert.isTrue(div.datum(2) === div);
},
"ignores null nodes": function(div) {
var some = d3.selectAll("div").data([42, 43]);
some[0][1] = null;
some.datum(function() { return 1; });
assert.equal(div[0][0].__data__, 1);
assert.equal(div[0][1].__data__, 43);
some.datum(2);
assert.equal(div[0][0].__data__, 2);
assert.equal(div[0][1].__data__, 43);
}
}
});
suite.export(module);

Просмотреть файл

@ -1,82 +0,0 @@
require("../env");
var vows = require("vows"),
assert = require("../env-assert");
var suite = vows.describe("selection.each");
suite.addBatch({
"select(body)": {
topic: function() {
return d3.select("body").html("");
},
"calls the function once per element": function(body) {
var count = 0;
body.each(function() { ++count; });
assert.equal(count, 1);
},
"passes the data and index to the function": function(body) {
var data = new Object(), dd, ii;
body.data([data]).each(function(d, i) { dd = d; ii = i; });
assert.isTrue(dd === data);
assert.isTrue(ii === 0);
},
"uses the node as the context": function(body) {
var node;
body.each(function() { node = this; });
assert.isTrue(node === document.body);
},
"returns the same selection": function(body) {
assert.isTrue(body.each(function() {}) === body);
},
"ignores null nodes": function() {
var count = 0, body = d3.select("body");
body[0][0] = null;
body.each(function() { ++count; });
assert.equal(count, 0);
},
"returns the current selection": function(body) {
assert.isTrue(body.each(function() {}) === body);
}
}
});
suite.addBatch({
"selectAll(div)": {
topic: function() {
return d3.select("body").html("").selectAll("div").data(d3.range(2)).enter().append("div");
},
"calls the function once per element": function(div) {
var count = 0;
div.each(function() { ++count; });
assert.equal(count, 2);
},
"passes the data and index to the function": function(div) {
var data = [new Object(), new Object()], dd = [], ii = [];
div.data(data).each(function(d, i) { dd.push(d); ii.push(i); });
assert.deepEqual(dd, data);
assert.deepEqual(ii, [0, 1]);
},
"uses the node as the context": function(div) {
var nodes = [];
div.each(function() { nodes.push(this); });
assert.equal(nodes.length, 2);
assert.isTrue(div[0][0] == nodes[0]);
assert.isTrue(div[0][1] == nodes[1]);
},
"returns the same selection": function(div) {
assert.isTrue(div.each(function() {}) === div);
},
"ignores null nodes": function(div) {
var count = 0, some = d3.selectAll("div");
some[0][0] = null;
some.each(function() { ++count; });
assert.equal(count, 1);
},
"returns the current selection": function(div) {
assert.isTrue(div.each(function() {}) === div);
}
}
});
suite.export(module);

Просмотреть файл

@ -1,50 +0,0 @@
require("../env");
var vows = require("vows"),
assert = require("../env-assert");
var suite = vows.describe("selection.empty");
suite.addBatch({
"select(body)": {
topic: function() {
return d3.select("body").html("");
},
"returns true for empty selections": function(body) {
assert.isTrue(body.select("foo").empty());
},
"returns false for non-empty selections": function(body) {
assert.isFalse(body.empty());
},
"ignores null nodes": function(body) {
var some = d3.select("body");
some[0][0] = null;
assert.isTrue(some.empty());
}
}
});
suite.addBatch({
"selectAll(div)": {
topic: function() {
var body = d3.select("body").html("");
body.append("div").append("span");
body.append("div");
return body.selectAll("div");
},
"returns true for empty selections": function(div) {
assert.isTrue(div.select("foo").empty());
},
"returns false for non-empty selections": function(div) {
assert.isFalse(div.empty());
assert.isFalse(div.select("span").empty());
},
"ignores null nodes": function(div) {
var some = d3.selectAll("div");
some[0][0] = null;
assert.isTrue(some.select("span").empty());
}
}
});
suite.export(module);

Просмотреть файл

@ -1,25 +0,0 @@
require("../env");
var vows = require("vows"),
assert = require("../env-assert");
var suite = vows.describe("selection.enter");
suite.addBatch({
"selectAll(div)": {
topic: function() {
return d3.select("body").html("").selectAll("div").data(d3.range(2)).enter();
},
"is an instanceof d3.selection.enter": function(enter) {
assert.instanceOf(enter, d3.selection.enter);
},
"selection prototype can be extended": function(enter) {
d3.selection.enter.prototype.foo = function() { return this.append("foo"); };
var selection = enter.foo();
assert.equal(document.body.innerHTML, "<foo></foo><foo></foo>");
delete d3.selection.enter.prototype.foo;
}
}
});
suite.export(module);

Просмотреть файл

@ -1,69 +0,0 @@
require("../env");
var vows = require("vows"),
assert = require("../env-assert");
var suite = vows.describe("selection.filter");
suite.addBatch({
"selectAll(div)": {
topic: function() {
return d3.select("body").html("").selectAll("div")
.data([0, 1])
.enter().append("div")
.selectAll("span")
.data(function(d) { d <<= 1; return [d, d + 1]; })
.enter().append("span");
},
"preserves matching elements": function(span) {
var some = span.filter(function(d, i) { return i === 0; });
assert.isTrue(some[0][0] === span[0][0]);
assert.isTrue(some[1][0] === span[1][0]);
},
"removes non-matching elements": function(span) {
var some = d3.merge(span.filter(function(d, i) { return d & 1; }));
assert.equal(some.indexOf(span[0][0]), -1);
assert.equal(some.indexOf(span[1][0]), -1);
},
"preserves data": function(span) {
var some = span.filter(function(d, i) { return d & 1; });
assert.equal(some[0][0].__data__, 1);
assert.equal(some[1][0].__data__, 3);
},
"preserves grouping": function(span) {
var some = span.filter(function(d, i) { return d & 1; });
assert.equal(some.length, 2);
assert.equal(some[0].length, 1);
assert.equal(some[1].length, 1);
},
"preserves parent node": function(span) {
var some = span.filter(function(d, i) { return d & 1; });
assert.isTrue(some[0].parentNode === span[0].parentNode);
assert.isTrue(some[1].parentNode === span[1].parentNode);
},
"does not preserve index": function(span) {
var indexes = [];
span.filter(function(d, i) { return d & 1; }).each(function(d, i) { indexes.push(i); });
assert.deepEqual(indexes, [0, 0]);
},
"ignores null nodes": function() {
var span = d3.selectAll("span");
span[0][1] = null;
var some = span.filter(function(d, i) { return d & 1; });
assert.isTrue(some[0][0] === span[0][3]);
assert.equal(some.length, 1);
},
"can be specified as a selector": function(span) {
span.classed("foo", function(d, i) { return d & 1; });
var some = span.filter(".foo");
assert.equal(some.length, 2);
assert.equal(some[0].length, 1);
assert.equal(some[1].length, 1);
},
"returns a new selection": function(span) {
assert.isFalse(span.filter(function() { return 1; }) === span);
}
}
});
suite.export(module);

Просмотреть файл

@ -1,129 +0,0 @@
require("../env");
var vows = require("vows"),
assert = require("../env-assert");
var suite = vows.describe("selection.html");
suite.addBatch({
"select(body)": {
topic: function() {
return d3.select("body").html("");
},
"sets the inner HTML as a string": function(body) {
body.html("<h1>Hello, world!</h1>");
assert.equal(document.body.firstChild.tagName, "H1");
assert.equal(document.body.firstChild.textContent, "Hello, world!");
},
"sets the inner HTML as a number": function(body) {
body.html(42);
assert.equal(document.body.innerHTML, "42");
assert.equal(document.body.firstChild.nodeType, document.TEXT_NODE);
},
"sets the inner HTML as a function": function(body) {
body.data(["Subject"]).html(function(d, i) { return "<b>" + d + "</b><i>" + i + "</i>"; });
assert.equal(document.body.firstChild.tagName, "B");
assert.equal(document.body.firstChild.textContent, "Subject");
assert.equal(document.body.lastChild.tagName, "I");
assert.equal(document.body.lastChild.textContent, "0");
},
"clears the inner HTML as null": function(body) {
body.html(null);
assert.equal(document.body.innerHTML, "");
assert.isNull(document.body.firstChild);
},
"clears the inner HTML as undefined": function(body) {
body.html(undefined);
assert.equal(document.body.innerHTML, "");
assert.isNull(document.body.firstChild);
},
"clears the inner HTML as the empty string": function(body) {
body.html("");
assert.equal(document.body.innerHTML, "");
assert.isNull(document.body.firstChild);
},
"clears the inner HTML as a function returning the empty string": function(body) {
body.text(function() { return ""; });
assert.equal(document.body.innerHTML, "");
assert.isNull(document.body.firstChild);
},
"clears the inner HTML as a function returning null": function(body) {
body.text(function() { return null; });
assert.equal(document.body.innerHTML, "");
assert.isNull(document.body.firstChild);
},
"clears the inner HTML as a function returning undefined": function(body) {
body.text(function() { return undefined; });
assert.equal(document.body.innerHTML, "");
assert.isNull(document.body.firstChild);
},
"ignores null nodes": function() {
var body = d3.select("body");
body[0][0] = null;
document.body.innerHTML = "<h1>foo</h1>";
body.html("bar");
assert.equal(document.body.textContent, "foo");
},
"returns the current selection": function(body) {
assert.isTrue(body.html("foo") === body);
}
}
});
suite.addBatch({
"selectAll(div)": {
topic: function() {
return d3.select("body").html("").selectAll("div").data(d3.range(2)).enter().append("div");
},
"sets the inner HTML as a string": function(div) {
div.html("<h1>Hello, world!</h1>");
assert.equal(div[0][0].firstChild.tagName, "H1");
assert.equal(div[0][0].firstChild.textContent, "Hello, world!");
assert.equal(div[0][1].firstChild.tagName, "H1");
assert.equal(div[0][1].firstChild.textContent, "Hello, world!");
},
"sets the inner HTML as a number": function(div) {
div.html(42);
assert.equal(div[0][0].innerHTML, "42");
assert.equal(div[0][0].firstChild.nodeType, document.TEXT_NODE);
},
"sets the inner HTML as a function": function(div) {
div.data(["foo", "bar"]).html(function(d, i) { return "<b>" + d + "</b><i>" + i + "</i>"; });
assert.equal(div[0][0].firstChild.tagName, "B");
assert.equal(div[0][0].firstChild.textContent, "foo");
assert.equal(div[0][0].lastChild.tagName, "I");
assert.equal(div[0][0].lastChild.textContent, "0");
assert.equal(div[0][1].firstChild.tagName, "B");
assert.equal(div[0][1].firstChild.textContent, "bar");
assert.equal(div[0][1].lastChild.tagName, "I");
assert.equal(div[0][1].lastChild.textContent, "1");
},
"clears the inner HTML as null": function(div) {
div.html(null);
assert.equal(div[0][0].innerHTML, "");
assert.isNull(div[0][0].firstChild);
assert.equal(div[0][1].innerHTML, "");
assert.isNull(div[0][1].firstChild);
},
"clears the inner HTML as a function": function(div) {
div.html(function() { return ""; });
assert.equal(div[0][0].innerHTML, "");
assert.isNull(div[0][0].firstChild);
assert.equal(div[0][1].innerHTML, "");
assert.isNull(div[0][1].firstChild);
},
"ignores null nodes": function(div) {
var some = d3.selectAll("div");
some[0][0] = null;
div[0][0].innerHTML = "<h1>foo</h1>";
some.html("bar");
assert.equal(div[0][0].textContent, "foo");
assert.equal(div[0][1].textContent, "bar");
},
"returns the current selection": function(div) {
assert.isTrue(div.html("foo") === div);
}
}
});
suite.export(module);

Просмотреть файл

@ -1,143 +0,0 @@
require("../env");
var vows = require("vows"),
assert = require("../env-assert");
var suite = vows.describe("selection.insert");
suite.addBatch({
"select(body)": {
topic: function() {
return d3.select("body").html("");
},
"inserts before the specified selector": function(body) {
var span = body.html("").append("span");
var div = body.insert("div", "span");
assert.equal(div[0][0].tagName, "DIV");
assert.isNull(div[0][0].namespaceURI);
assert.domEqual(div[0][0], document.body.firstChild);
assert.domEqual(div[0][0].nextSibling, span[0][0]);
},
"inserts before the specified node": function(body) {
var span = body.html("").append("span");
var div = body.insert("div", function() { return span.node(); });
assert.equal(div[0][0].tagName, "DIV");
assert.isNull(div[0][0].namespaceURI);
assert.domEqual(div[0][0], document.body.firstChild);
assert.domEqual(div[0][0].nextSibling, span[0][0]);
},
"appends an HTML element": function(body) {
var div = body.insert("div");
assert.equal(div[0][0].tagName, "DIV");
assert.isNull(div[0][0].namespaceURI);
assert.domEqual(div[0][0], document.body.lastChild);
},
"appends an SVG element": function(body) {
var svg = body.insert("svg:svg");
assert.equal(svg[0][0].tagName, "SVG");
assert.equal(svg[0][0].namespaceURI, "http://www.w3.org/2000/svg");
assert.domEqual(svg[0][0].parentNode, document.body);
assert.domEqual(svg[0][0], document.body.lastChild);
},
"propagates data to new element": function(body) {
var data = new Object(), div = body.data([data]).insert("div");
assert.strictEqual(div[0][0].__data__, data);
},
"returns a new selection": function(body) {
assert.isFalse(body.insert("div") === body);
},
"inherits namespace from parent node": function(body) {
var g = body.insert("svg:svg").insert("g");
assert.equal(g[0][0].namespaceURI, "http://www.w3.org/2000/svg");
}
}
});
suite.addBatch({
"selectAll(div)": {
topic: function() {
return d3.select("body").html("").selectAll("div").data(d3.range(2)).enter().insert("div");
},
"appends an HTML element": function(div) {
var span = div.insert("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.domEqual(span[0][0].parentNode, div[0][0]);
assert.domEqual(span[0][1].parentNode, div[0][1]);
assert.domEqual(div[0][0].lastChild, span[0][0]);
assert.domEqual(div[0][1].lastChild, span[0][1]);
},
"appends an SVG element": function(div) {
var svg = div.insert("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.domEqual(svg[0][0].parentNode, div[0][0]);
assert.domEqual(svg[0][1].parentNode, div[0][1]);
assert.domEqual(div[0][0].lastChild, svg[0][0]);
assert.domEqual(div[0][1].lastChild, svg[0][1]);
},
"ignores null nodes": function(div) {
div.html("");
var some = d3.selectAll("div");
some[0][1] = null;
var span = some.insert("span");
assert.equal(span[0].length, 2);
assert.equal(span[0][0].tagName, "SPAN");
assert.domNull(span[0][1]);
assert.domEqual(span[0][0].parentNode, div[0][0]);
assert.domEqual(div[0][0].lastChild, span[0][0]);
assert.domNull(div[0][1].lastChild);
},
"propagates data to new elements": function(div) {
var a = new Object(), b = new Object(), span = div.data([a, b]).insert("span");
assert.strictEqual(span[0][0].__data__, a);
assert.strictEqual(span[0][1].__data__, b);
},
"returns a new selection": function(div) {
assert.isFalse(div.insert("div") === div);
}
}
});
suite.addBatch({
"selectAll(div).data(…).enter()": {
topic: function() {
return d3.select("body");
},
"inserts before the specified selector": function(body) {
var span = body.html("").append("span");
var div = body.selectAll("div").data(d3.range(2)).enter().insert("div", "span");
assert.equal(div.length, 1);
assert.equal(div[0].length, 2);
assert.domEqual(div[0][0], document.body.firstChild);
assert.domEqual(div[0][1].previousSibling, div[0][0]);
assert.domEqual(div[0][1].nextSibling, span[0][0]);
},
"propagates data to new elements": function(body) {
var a = new Object(), b = new Object(), div = body.html("").selectAll("div").data([a, b]).enter().insert("div");
assert.strictEqual(div[0][0].__data__, a);
assert.strictEqual(div[0][1].__data__, b);
},
"ignores null nodes": function(body) {
body.html("").insert("div");
var div = body.selectAll("div").data(d3.range(3)).enter().insert("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);
},
"returns a new selection": function(body) {
var enter = body.html("").selectAll("div").data([0, 1]).enter();
assert.isFalse(enter.insert("div") === enter);
}
}
});
suite.export(module);

Просмотреть файл

@ -1,49 +0,0 @@
require("../env");
var vows = require("vows"),
assert = require("../env-assert");
var suite = vows.describe("selection.node");
suite.addBatch({
"select(body)": {
topic: function() {
return d3.select("body").html("");
},
"returns null for empty selections": function(body) {
assert.isNull(body.select("foo").node());
},
"returns the first element for non-empty selections": function(body) {
assert.isTrue(body.node() === document.body);
},
"ignores null nodes": function(body) {
var some = d3.select("body");
some[0][0] = null;
assert.isNull(some.node());
}
}
});
suite.addBatch({
"selectAll(div)": {
topic: function() {
var body = d3.select("body").html("");
body.append("div").append("span");
body.append("div");
return body.selectAll("div");
},
"returns null for empty selections": function(div) {
assert.isNull(div.select("foo").node());
},
"returns the first element for non-empty selections": function(div) {
assert.isTrue(div.node() === div[0][0]);
},
"ignores null nodes": function(div) {
var some = d3.selectAll("div");
some[0][0] = null;
assert.isTrue(some.node() === div[0][1]);
}
}
});
suite.export(module);

Просмотреть файл

@ -1,133 +0,0 @@
require("../env");
var vows = require("vows"),
assert = require("../env-assert");
var suite = vows.describe("selection.on");
suite.addBatch({
"select(body)": {
topic: function() {
return d3.select("body").html("");
},
"registers an event listener for the specified type": function(body) {
var form = body.append("form"), count = 0;
form.on("submit", function() { ++count; }); // jsdom has spotty event support
form.append("input").attr("type", "submit").node().click();
assert.equal(count, 1);
},
"replaces an existing event listener for the same type": function(body) {
var form = body.append("form"), count = 0, fail = 0;
form.on("submit", function() { ++fail; });
form.on("submit", function() { ++count; });
form.append("input").attr("type", "submit").node().click();
assert.equal(count, 1);
assert.equal(fail, 0);
},
"removes an existing event listener": function(body) {
var form = body.append("form"), fail = 0;
form.on("submit", function() { ++fail; });
form.on("submit", null);
form.append("input").attr("type", "submit").node().click();
assert.equal(fail, 0);
assert.isUndefined(form.on("submit"));
},
/* Regrettably, JSDOM ignores the capture flag, so we can't test this yet
"removing a listener doesn't require the capture flag": function(body) {
var form = body.append("form"), fail = 0;
form.on("submit", function() { ++fail; }, true);
form.on("submit", null);
form.append("input").attr("type", "submit").node().click();
assert.equal(fail, 0);
assert.isUndefined(form.on("submit"));
},
*/
"ignores removal of non-matching event listener": function(body) {
body.append("form").on("submit", null);
},
"observes the specified namespace": function(body) {
var form = body.append("form"), foo = 0, bar = 0;
form.on("submit.foo", function() { ++foo; });
form.on({"submit.bar": function() { ++bar; }});
form.append("input").attr("type", "submit").node().click();
assert.equal(foo, 1);
assert.equal(bar, 1);
},
"can register listeners as a map": function(body) {
var form = body.append("form"), count = 0, fail = 0;
form.on({submit: function() { ++fail; }});
form.on({submit: function() { ++count; }});
form.append("input").attr("type", "submit").node().click();
assert.equal(count, 1);
assert.equal(fail, 0);
form.on({submit: null});
assert.isUndefined(form.on("submit"));
},
/* Not really sure how to test this one
"observes the specified capture flag": function(body) {
},
*/
"passes the current data and index to the event listener": function(body) {
var forms = body.html("").selectAll("form").data(["a", "b"]).enter().append("form"), dd, ii, data = new Object();
forms.on("submit", function(d, i) { dd = d; ii = i; });
forms.append("input").attr("type", "submit")[0][1].click();
assert.equal(dd, "b");
assert.equal(ii, 1);
forms[0][1].__data__ = data;
forms.append("input").attr("type", "submit")[0][1].click();
assert.equal(dd, data);
assert.equal(ii, 1);
},
"uses the current element as the context": function(body) {
var forms = body.html("").selectAll("form").data(["a", "b"]).enter().append("form"), context;
forms.on("submit", function() { context = this; });
forms.append("input").attr("type", "submit")[0][1].click();
assert.domEqual(context, forms[0][1]);
},
"sets the current event as d3.event": function(body) {
var form = body.append("form"), event;
form.on("submit", function() { event = d3.event; });
form.append("input").attr("type", "submit").node().click();
assert.equal(event.type, "submit");
assert.domEqual(event.target, form[0][0]);
},
"restores the original event after notifying listeners": function(body) {
var form = body.append("form"), event = d3.event = new Object();
form.on("submit", function() {});
form.append("input").attr("type", "submit").node().click();
assert.equal(d3.event, event);
},
"returns the current selection": function(body) {
assert.isTrue(body.on("submit", function() {}) === body);
},
"returns the assigned listener if called with one argument": function(body) {
body.on("mouseover", f).on("click.foo", f);
function f() {}
assert.equal(body.on("mouseover"), f);
assert.equal(body.on("click.foo"), f);
assert.isUndefined(body.on("click"));
assert.isUndefined(body.on("mouseover.foo"));
},
"omitting the event type": {
"returns undefined when retrieving a listener": function(body) {
assert.isUndefined(body.on(".foo"));
},
"null removes all named event listeners": function(body) {
body.on("mouseover.foo", f)
.on("click.foo", f)
.on("click.foobar", f)
.on(".foo", null);
function f() {}
assert.isUndefined(body.on("mouseover.foo"));
assert.isUndefined(body.on("click.foo"));
assert.equal(body.on("click.foobar"), f);
},
"no-op when setting a listener": function(body) {
assert.isTrue(body.on(".foo", function() {}) === body);
assert.isUndefined(body.on(".foo"));
}
}
}
});
suite.export(module);

Просмотреть файл

@ -1,31 +0,0 @@
require("../env");
var vows = require("vows"),
assert = require("../env-assert");
var suite = vows.describe("selection.order");
suite.addBatch({
"selectAll(div)": {
topic: function() {
return d3.select("body").html("").selectAll("div")
.data([1, 2, 10, 20])
.enter().append("div")
.attr("id", String);
},
"orders elements by data": function(div) {
div = div.data([1, 10, 20, 2], String).order();
assert.domNull(div[0][0].previousSibling);
assert.domEqual(div[0][1].previousSibling, div[0][0]);
assert.domEqual(div[0][2].previousSibling, div[0][1]);
assert.domEqual(div[0][3].previousSibling, div[0][2]);
assert.domNull(div[0][3].nextSibling);
},
"returns the current selection": function(span) {
span = d3.select("body"); // https://github.com/tmpvar/jsdom/issues/277
assert.isTrue(span.order() === span);
}
}
});
suite.export(module);

Просмотреть файл

@ -1,110 +0,0 @@
require("../env");
var vows = require("vows"),
assert = require("../env-assert");
var suite = vows.describe("selection.property");
suite.addBatch({
"select(body)": {
topic: function() {
return d3.select("body").html("");
},
"sets a property as a string": function(body) {
body.property("bgcolor", "red");
assert.equal(document.body.bgcolor, "red");
},
"sets a property as a number": function(body) {
body.property("opacity", 1);
assert.equal(document.body.opacity, "1");
},
"sets a property as a function": function(body) {
body.property("bgcolor", function() { return "orange"; });
assert.equal(document.body.bgcolor, "orange");
},
"sets properties as a map of constants": function(body) {
body.property({bgcolor: "purple", opacity: .41});
assert.equal(document.body.bgcolor, "purple");
assert.equal(document.body.opacity, .41);
},
"sets properties as a map of functions": function(body) {
body.data(["cyan"]).property({bgcolor: String, opacity: function(d, i) { return i; }});
assert.equal(document.body.bgcolor, "cyan");
assert.equal(document.body.opacity, 0);
},
"gets a property value": function(body) {
document.body.bgcolor = "yellow";
assert.equal(body.property("bgcolor"), "yellow");
},
"removes a property as null": function(body) {
body.property("bgcolor", "yellow").property("bgcolor", null);
assert.isFalse("bgcolor" in document.body);
},
"removes a property as a function": function(body) {
body.property("bgcolor", "yellow").property("bgcolor", function() { return null });
assert.isFalse("bgcolor" in document.body);
},
"removes properties as a map of nulls": function(body) {
document.body.bgcolor = "red";
body.property({bgcolor: null});
assert.isFalse("bgcolor" in document.body);
},
"removes properties as a map of functions that return null": function(body) {
document.body.bgcolor = "red";
body.property({bgcolor: function() {}});
assert.isFalse("bgcolor" in document.body);
},
"returns the current selection": function(body) {
assert.isTrue(body.property("bgcolor", "yellow") === body);
}
}
});
suite.addBatch({
"selectAll(div)": {
topic: function() {
return d3.select("body").html("").selectAll("div").data(d3.range(2)).enter().append("div");
},
"sets a property as a string": function(div) {
div.property("bgcolor", "red");
assert.equal(div[0][0].bgcolor, "red");
assert.equal(div[0][1].bgcolor, "red");
},
"sets a property as a number": function(div) {
div.property("opacity", 0.4);
assert.equal(div[0][0].opacity, "0.4");
assert.equal(div[0][1].opacity, "0.4");
},
"sets a property as a function": function(div) {
div.property("bgcolor", d3.interpolateRgb("brown", "steelblue"));
assert.equal(div[0][0].bgcolor, "#a52a2a");
assert.equal(div[0][1].bgcolor, "#4682b4");
},
"gets a property value": function(div) {
div[0][0].bgcolor = "purple";
assert.equal(div.property("bgcolor"), "purple");
},
"removes a property as null": function(div) {
div.property("bgcolor", "yellow").property("bgcolor", null);
assert.isFalse("bgcolor" in div[0][0]);
assert.isFalse("bgcolor" in div[0][1]);
},
"removes a property as a function": function(div) {
div.property("bgcolor", "yellow").property("bgcolor", function() { return null });
assert.isFalse("bgcolor" in div[0][0]);
assert.isFalse("bgcolor" in div[0][1]);
},
"ignores null nodes": function(div) {
var some = d3.selectAll("div");
some[0][1] = null;
some.property("bgcolor", null).property("bgcolor", "red");
assert.equal(div[0][0].bgcolor, "red");
assert.isFalse("bgcolor" in div[0][1]);
},
"returns the current selection": function(div) {
assert.isTrue(div.property("bgcolor", "yellow") === div);
}
}
});
suite.export(module);

Просмотреть файл

@ -1,38 +0,0 @@
require("../env");
var vows = require("vows"),
assert = require("../env-assert");
var suite = vows.describe("selection.remove");
suite.addBatch({
"select(body)": {
topic: function() {
return d3.select("body");
},
"removes the matching elements": function(body) {
var div = body.append("div");
div.remove();
assert.domNull(div[0][0].parentNode);
},
"does not remove non-matching elements": function(body) {
var div1 = body.append("div"), div2 = body.append("div");
div1.remove();
assert.domEqual(div2[0][0].parentNode, document.body);
},
"ignores null nodes": function(body) {
var div = body.html("").selectAll("div").data([0, 1]).enter().append("div"),
some = d3.selectAll("div");
some[0][0] = null;
some.remove();
assert.domEqual(div[0][0].parentNode, document.body);
assert.domNull(div[0][1].parentNode);
},
"returns the current selection": function(body) {
var div = body.append("div");
assert.isTrue(div.remove() === div);
}
}
});
suite.export(module);

Просмотреть файл

@ -1,105 +1,132 @@
require("../env");
var vows = require("vows"),
assert = require("../env-assert");
d3 = require("../../"),
load = require("../load"),
assert = require("../env-assert"),
document = d3.selection().node()._ownerDocument,
window = document.defaultView;
var suite = vows.describe("selection.select");
suite.addBatch({
"select(body)": {
topic: function() {
var body = d3.select("body").html("");
body.append("div").attr("class", "first");
body.append("div").attr("class", "second");
return body;
},
"selects the first matching element": function(body) {
var div = body.select("div");
assert.isTrue(div[0][0] === document.body.firstChild);
assert.equal(div.length, 1);
assert.equal(div[0].length, 1);
assert.equal(div.attr("class"), "first");
},
"propagates parent node to the selected elements": function(body) {
var div = body.select("div");
assert.isNotNull(div[0].parentNode);
assert.isTrue(div[0].parentNode === document.documentElement);
assert.isTrue(div[0].parentNode === body[0].parentNode);
},
"propagates data to the selected elements": function(body) {
var data = new Object(), div = body.data([data]).select("div");
assert.strictEqual(div[0][0].__data__, data);
},
"does not propagate data if no data was specified": function(body) {
delete document.body.__data__;
var data = new Object(), div = body.select("div").data([data]);
div = body.select("div");
assert.strictEqual(div[0][0].__data__, data);
assert.isUndefined(document.body.__data__);
},
"returns null if no match is found": function(body) {
var span = body.select("span");
assert.equal(span[0][0], null);
assert.equal(span.length, 1);
assert.equal(span[0].length, 1);
},
"can select via function": function(body) {
body.append("foo");
var d = {}, dd = [], ii = [], tt = [],
s = body.data([d]).select(function(d, i) { dd.push(d); ii.push(i); tt.push(this); return this.firstChild; });
assert.deepEqual(dd, [d], "expected data, got {actual}");
assert.deepEqual(ii, [0], "expected index, got {actual}");
assert.domEqual(tt[0], document.body, "expected this, got {actual}");
assert.domEqual(s[0][0], document.body.firstChild);
delete document.body.__data__;
topic: load("selection/selection").sandbox({
document: document,
window: window
}),
"on a simple page": {
topic: function(d3) {
var body = d3.select("body").html("");
body.append("div").attr("class", "first");
body.append("div").attr("class", "second");
return body;
},
"selects the first matching element": function(body) {
var div = body.select("div");
assert.isTrue(div[0][0] === document.body.firstChild);
assert.equal(div.length, 1);
assert.equal(div[0].length, 1);
assert.equal(div.attr("class"), "first");
},
"propagates parent node to the selected elements": function(body) {
var div = body.select("div");
assert.isNotNull(div[0].parentNode);
assert.isTrue(div[0].parentNode === document.documentElement);
assert.isTrue(div[0].parentNode === body[0].parentNode);
},
"propagates data to the selected elements": function(body) {
var data = new Object(), div = body.data([data]).select("div");
assert.strictEqual(div[0][0].__data__, data);
},
"does not propagate data if no data was specified": function(body) {
delete document.body.__data__;
var data = new Object(), div = body.select("div").data([data]);
div = body.select("div");
assert.strictEqual(div[0][0].__data__, data);
assert.isUndefined(document.body.__data__);
},
"returns null if no match is found": function(body) {
var span = body.select("span");
assert.equal(span[0][0], null);
assert.equal(span.length, 1);
assert.equal(span[0].length, 1);
},
"can select via function": function(body) {
body.append("foo");
var d = {}, dd = [], ii = [], tt = [],
s = body.data([d]).select(function(d, i) { dd.push(d); ii.push(i); tt.push(this); return this.firstChild; });
assert.deepEqual(dd, [d], "expected data, got {actual}");
assert.deepEqual(ii, [0], "expected index, got {actual}");
assert.domEqual(tt[0], document.body, "expected this, got {actual}");
assert.domEqual(s[0][0], document.body.firstChild);
delete document.body.__data__;
}
}
}
});
suite.addBatch({
"selectAll(div)": {
topic: function() {
var div = d3.select("body").html("").selectAll("div").data(d3.range(2)).enter().append("div");
topic: load("selection/selection").sandbox({
document: document,
window: window
}),
"on a simple page": {
topic: function(d3) {
var div = d3.select("body").html("").selectAll("div").data([0, 1]).enter().append("div");
div.append("span").attr("class", "first");
div.append("span").attr("class", "second");
return div;
},
"selects the first matching element": function(div) {
var span = div.select("span");
assert.isTrue(span[0][0].parentNode === div[0][0]);
assert.isTrue(span[0][1].parentNode === div[0][1]);
assert.equal(span.length, 1);
assert.equal(span[0].length, 2);
assert.equal(span.attr("class"), "first");
},
"propagates parent node to the selected elements": function(div) {
var span = div.select("span");
assert.isNotNull(span[0].parentNode);
assert.isTrue(span[0].parentNode === document.body);
assert.isTrue(span[0].parentNode === div[0].parentNode);
},
"propagates data to the selected elements": function(div) {
var data = new Object(), span = div.data([data]).select("span");
assert.strictEqual(span[0][0].__data__, data);
},
"does not propagate data if no data was specified": function(div) {
delete div[0][0].__data__;
delete div[0][1].__data__;
var a = new Object(), b = new Object(), span = div.select("span").data([a, b]);
span = div.select("span");
assert.strictEqual(span[0][0].__data__, a);
assert.strictEqual(span[0][1].__data__, b);
assert.isUndefined(div[0][0].__data__);
assert.isUndefined(div[0][1].__data__);
},
"returns null if no match is found": function(div) {
var div = div.select("div");
assert.equal(div[0][0], null);
assert.equal(div[0][1], null);
assert.equal(div.length, 1);
assert.equal(div[0].length, 2);
},
"can select via function": function(div) {
var dd = [], ii = [], tt = [],
s = div.data(["a", "b"]).select(function(d, i) { dd.push(d); ii.push(i); tt.push(this); return this.firstChild; });
assert.deepEqual(dd, ["a", "b"], "expected data, got {actual}");
assert.deepEqual(ii, [0, 1], "expected index, got {actual}");
assert.domEqual(tt[0], div[0][0], "expected this, got {actual}");
assert.domEqual(tt[1], div[0][1], "expected this, got {actual}");
assert.domEqual(s[0][0], div[0][0].firstChild);
assert.domEqual(s[0][1], div[0][1].firstChild);
}
},
"ignores null nodes": function(d3) {
var div = d3.select("body").html("").selectAll("div").data([0, 1]).enter().append("div");
div.append("span").attr("class", "first");
div.append("span").attr("class", "second");
return div;
},
"selects the first matching element": function(div) {
var span = div.select("span");
assert.isTrue(span[0][0].parentNode === div[0][0]);
assert.isTrue(span[0][1].parentNode === div[0][1]);
assert.equal(span.length, 1);
assert.equal(span[0].length, 2);
assert.equal(span.attr("class"), "first");
},
"propagates parent node to the selected elements": function(div) {
var span = div.select("span");
assert.isNotNull(span[0].parentNode);
assert.isTrue(span[0].parentNode === document.body);
assert.isTrue(span[0].parentNode === div[0].parentNode);
},
"propagates data to the selected elements": function(div) {
var data = new Object(), span = div.data([data]).select("span");
assert.strictEqual(span[0][0].__data__, data);
},
"does not propagate data if no data was specified": function(div) {
delete div[0][0].__data__;
delete div[0][1].__data__;
var a = new Object(), b = new Object(), span = div.select("span").data([a, b]);
span = div.select("span");
assert.strictEqual(span[0][0].__data__, a);
assert.strictEqual(span[0][1].__data__, b);
assert.isUndefined(div[0][0].__data__);
assert.isUndefined(div[0][1].__data__);
},
"returns null if no match is found": function(div) {
var div = div.select("div");
assert.equal(div[0][0], null);
assert.equal(div[0][1], null);
assert.equal(div.length, 1);
assert.equal(div[0].length, 2);
},
"ignores null nodes": function(div) {
var some = d3.selectAll("div");
some[0][1] = null;
var span = some.select("span");
@ -107,16 +134,6 @@ suite.addBatch({
assert.equal(span[0].length, 2);
assert.isTrue(span[0][0].parentNode === div[0][0]);
assert.isNull(span[0][1]);
},
"can select via function": function(div) {
var dd = [], ii = [], tt = [],
s = div.data(["a", "b"]).select(function(d, i) { dd.push(d); ii.push(i); tt.push(this); return this.firstChild; });
assert.deepEqual(dd, ["a", "b"], "expected data, got {actual}");
assert.deepEqual(ii, [0, 1], "expected index, got {actual}");
assert.domEqual(tt[0], div[0][0], "expected this, got {actual}");
assert.domEqual(tt[1], div[0][1], "expected this, got {actual}");
assert.domEqual(s[0][0], div[0][0].firstChild);
assert.domEqual(s[0][1], div[0][1].firstChild);
}
}
});

Просмотреть файл

@ -1,106 +1,135 @@
require("../env");
var vows = require("vows"),
assert = require("../env-assert");
d3 = require("../../"),
load = require("../load"),
assert = require("../env-assert"),
document = d3.selection().node()._ownerDocument,
window = document.defaultView;
var suite = vows.describe("selection.selectAll");
suite.addBatch({
"select(body)": {
topic: function() {
var body = d3.select("body").html("");
if ("__data__" in body[0][0]) delete body[0][0].__data__;
body.append("div").attr("class", "first");
body.append("div").attr("class", "second");
return body;
},
"selects all matching elements": function(body) {
var div = body.selectAll("div");
assert.isTrue(div[0][0] === document.body.firstChild);
assert.isTrue(div[0][1] === document.body.lastChild);
assert.equal(div.length, 1);
assert.equal(div[0].length, 2);
},
"propagates parent node to the selected elements": function(body) {
var div = body.selectAll("div");
assert.isNotNull(div[0].parentNode);
assert.isTrue(div[0].parentNode === document.body);
},
"does not propagate data if data was specified": function(body) {
var div = body.data([new Object()]).selectAll("div");
assert.isUndefined(div[0][0].__data__);
assert.isUndefined(div[0][1].__data__);
},
"does not propagate data if data was not specified": function(body) {
var div = body.selectAll("div").data([1, 2]);
div = body.data([new Object()]).selectAll("div");
assert.equal(div[0][0].__data__, 1);
assert.equal(div[0][1].__data__, 2);
},
"returns empty array if no match is found": function(body) {
var span = body.selectAll("span");
assert.equal(span.length, 1);
assert.equal(span[0].length, 0);
},
"can select via function": function(body) {
var d = {}, dd = [], ii = [], tt = [],
s = body.data([d]).selectAll(function(d, i) { dd.push(d); ii.push(i); tt.push(this); return this.childNodes; });
assert.deepEqual(dd, [d], "expected data, got {actual}");
assert.deepEqual(ii, [0], "expected index, got {actual}");
assert.domEqual(tt[0], document.body, "expected this, got {actual}");
assert.domEqual(s[0][0], document.body.firstChild);
assert.domEqual(s[0][1], document.body.lastChild);
delete document.body.__data__;
topic: load("selection/selection").sandbox({
document: document,
window: window
}),
"on a simple page": {
topic: function(d3) {
var body = d3.select("body").html("");
if ("__data__" in body[0][0]) delete body[0][0].__data__;
body.append("div").attr("class", "first");
body.append("div").attr("class", "second");
return body;
},
"selects all matching elements": function(body) {
var div = body.selectAll("div");
assert.isTrue(div[0][0] === document.body.firstChild);
assert.isTrue(div[0][1] === document.body.lastChild);
assert.equal(div.length, 1);
assert.equal(div[0].length, 2);
},
"propagates parent node to the selected elements": function(body) {
var div = body.selectAll("div");
assert.isNotNull(div[0].parentNode);
assert.isTrue(div[0].parentNode === document.body);
},
"does not propagate data if data was specified": function(body) {
var div = body.data([new Object()]).selectAll("div");
assert.isUndefined(div[0][0].__data__);
assert.isUndefined(div[0][1].__data__);
},
"does not propagate data if data was not specified": function(body) {
var div = body.selectAll("div").data([1, 2]);
div = body.data([new Object()]).selectAll("div");
assert.equal(div[0][0].__data__, 1);
assert.equal(div[0][1].__data__, 2);
},
"returns empty array if no match is found": function(body) {
var span = body.selectAll("span");
assert.equal(span.length, 1);
assert.equal(span[0].length, 0);
},
"can select via function": function(body) {
var d = {}, dd = [], ii = [], tt = [],
s = body.data([d]).selectAll(function(d, i) { dd.push(d); ii.push(i); tt.push(this); return this.childNodes; });
assert.deepEqual(dd, [d], "expected data, got {actual}");
assert.deepEqual(ii, [0], "expected index, got {actual}");
assert.domEqual(tt[0], document.body, "expected this, got {actual}");
assert.domEqual(s[0][0], document.body.firstChild);
assert.domEqual(s[0][1], document.body.lastChild);
delete document.body.__data__;
}
}
}
});
suite.addBatch({
"selectAll(div)": {
topic: function() {
var div = d3.select("body").html("").selectAll("div").data(d3.range(2)).enter().append("div");
topic: load("selection/selection").sandbox({
document: document,
window: window
}),
"on a simple page": {
topic: function(d3) {
var div = d3.select("body").html("").selectAll("div").data([0, 1]).enter().append("div");
div.append("span").attr("class", "first");
div.append("span").attr("class", "second");
return div;
},
"selects all matching elements": function(div) {
var span = div.selectAll("span");
assert.equal(span.length, 2);
assert.equal(span[0].length, 2);
assert.isTrue(span[0][0].parentNode === div[0][0]);
assert.isTrue(span[0][1].parentNode === div[0][0]);
assert.isTrue(span[1][0].parentNode === div[0][1]);
assert.isTrue(span[1][1].parentNode === div[0][1]);
},
"propagates parent node to the selected elements": function(div) {
var span = div.selectAll("span");
assert.isNotNull(span[0].parentNode);
assert.isTrue(span[0].parentNode === div[0][0]);
assert.isTrue(span[1].parentNode === div[0][1]);
},
"does not propagate data if data was specified": function(div) {
var span = div.selectAll("span");
delete span[0][0].__data__;
delete span[0][1].__data__;
span = div.data([new Object(), new Object()]).selectAll("span");
assert.isUndefined(span[0][0].__data__);
assert.isUndefined(span[0][1].__data__);
},
"does not propagate data if data was not specified": function(div) {
var a = new Object(), b = new Object(), span = div.selectAll("span").data([a, b]);
delete div[0][0].__data__;
delete div[0][1].__data__;
span = div.selectAll("span");
assert.equal(span[0][0].__data__, a);
assert.equal(span[0][1].__data__, b);
},
"returns empty array if no match is found": function(div) {
var div = div.selectAll("div");
assert.equal(div.length, 2);
assert.equal(div[0].length, 0);
assert.equal(div[1].length, 0);
},
"can select via function": function(div) {
var dd = [], ii = [], tt = [],
s = div.data(["a", "b"]).selectAll(function(d, i) { dd.push(d); ii.push(i); tt.push(this); return this.childNodes; });
assert.deepEqual(dd, ["a", "b"], "expected data, got {actual}");
assert.deepEqual(ii, [0, 1], "expected index, got {actual}");
assert.domEqual(tt[0], div[0][0], "expected this, got {actual}");
assert.domEqual(tt[1], div[0][1], "expected this, got {actual}");
assert.domEqual(s[0][0], div[0][0].firstChild);
assert.domEqual(s[0][1], div[0][0].lastChild);
assert.domEqual(s[1][0], div[0][1].firstChild);
assert.domEqual(s[1][1], div[0][1].lastChild);
}
},
"ignores null nodes": function(d3) {
var div = d3.select("body").html("").selectAll("div").data([0, 1]).enter().append("div");
div.append("span").attr("class", "first");
div.append("span").attr("class", "second");
return div;
},
"selects all matching elements": function(div) {
var span = div.selectAll("span");
assert.equal(span.length, 2);
assert.equal(span[0].length, 2);
assert.isTrue(span[0][0].parentNode === div[0][0]);
assert.isTrue(span[0][1].parentNode === div[0][0]);
assert.isTrue(span[1][0].parentNode === div[0][1]);
assert.isTrue(span[1][1].parentNode === div[0][1]);
},
"propagates parent node to the selected elements": function(div) {
var span = div.selectAll("span");
assert.isNotNull(span[0].parentNode);
assert.isTrue(span[0].parentNode === div[0][0]);
assert.isTrue(span[1].parentNode === div[0][1]);
},
"does not propagate data if data was specified": function(div) {
var span = div.selectAll("span");
delete span[0][0].__data__;
delete span[0][1].__data__;
span = div.data([new Object(), new Object()]).selectAll("span");
assert.isUndefined(span[0][0].__data__);
assert.isUndefined(span[0][1].__data__);
},
"does not propagate data if data was not specified": function(div) {
var a = new Object(), b = new Object(), span = div.selectAll("span").data([a, b]);
delete div[0][0].__data__;
delete div[0][1].__data__;
span = div.selectAll("span");
assert.equal(span[0][0].__data__, a);
assert.equal(span[0][1].__data__, b);
},
"returns empty array if no match is found": function(div) {
var div = div.selectAll("div");
assert.equal(div.length, 2);
assert.equal(div[0].length, 0);
assert.equal(div[1].length, 0);
},
"ignores null nodes": function(div) {
var some = d3.selectAll("div");
some[0][1] = null;
var span = some.selectAll("span");
@ -108,18 +137,6 @@ suite.addBatch({
assert.equal(span[0].length, 2);
assert.isTrue(span[0][0].parentNode === div[0][0]);
assert.isTrue(span[0][1].parentNode === div[0][0]);
},
"can select via function": function(div) {
var dd = [], ii = [], tt = [],
s = div.data(["a", "b"]).selectAll(function(d, i) { dd.push(d); ii.push(i); tt.push(this); return this.childNodes; });
assert.deepEqual(dd, ["a", "b"], "expected data, got {actual}");
assert.deepEqual(ii, [0, 1], "expected index, got {actual}");
assert.domEqual(tt[0], div[0][0], "expected this, got {actual}");
assert.domEqual(tt[1], div[0][1], "expected this, got {actual}");
assert.domEqual(s[0][0], div[0][0].firstChild);
assert.domEqual(s[0][1], div[0][0].lastChild);
assert.domEqual(s[1][0], div[0][1].firstChild);
assert.domEqual(s[1][1], div[0][1].lastChild);
}
}
});

Просмотреть файл

@ -1,79 +0,0 @@
require("../env");
var vows = require("vows"),
assert = require("../env-assert");
var suite = vows.describe("selection.sort");
suite.addBatch({
"selectAll(div).selectAll(span)": {
topic: function() {
return d3.select("body").html("").selectAll("div")
.data([1, 2, 10, 20])
.enter().append("div").selectAll("span")
.data(function(d) { return [20 + d, 2 + d, 10, 1]; })
.enter().append("span");
},
"sorts elements by natural order": function(span) {
span.sort();
assert.domNull(span[0][0].previousSibling);
assert.domEqual(span[0][1].previousSibling, span[0][0]);
assert.domEqual(span[0][2].previousSibling, span[0][1]);
assert.domEqual(span[0][3].previousSibling, span[0][2]);
assert.domNull(span[0][3].nextSibling);
},
"sorts each group independently": function(span) {
span.sort(d3.descending);
assert.deepEqual(span[0].map(data), [21, 10, 3, 1]);
assert.deepEqual(span[1].map(data), [22, 10, 4, 1]);
assert.deepEqual(span[2].map(data), [30, 12, 10, 1]);
assert.deepEqual(span[3].map(data), [40, 22, 10, 1]);
},
"sorts using the specified comparator": function(span) {
span.sort(function(a, b) { return d3.ascending(a+"", b+""); });
assert.deepEqual(span[0].map(data), [1, 10, 21, 3]);
assert.deepEqual(span[1].map(data), [1, 10, 22, 4]);
assert.deepEqual(span[2].map(data), [1, 10, 12, 30]);
assert.deepEqual(span[3].map(data), [1, 10, 22, 40]);
},
"sorts null nodes at the end of the selection": function() {
var span = d3.selectAll("div").selectAll("span"), nulls = 0;
d3.select(span[0][0]).remove();
span[0][0] = null;
span.sort(function(a, b) { if ((a === null) || (b === null)) ++nulls; return a - b; });
assert.equal(nulls, 0);
assert.isNull(span[0][3]);
assert.domNull(span[0][0].previousSibling);
assert.domEqual(span[0][1], span[0][0].nextSibling);
assert.domEqual(span[0][0], span[0][1].previousSibling);
assert.domEqual(span[0][2], span[0][1].nextSibling);
assert.domEqual(span[0][1], span[0][2].previousSibling);
assert.domNull(span[0][2].nextSibling);
assert.deepEqual(span[0].slice(0, -1).map(data), [3, 10, 21]);
for (var i = 1; i < 4; ++i) {
var d = span[i].parentNode.__data__;
assert.domNull(span[i][0].previousSibling);
assert.domEqual(span[i][1], span[i][0].nextSibling);
assert.domEqual(span[i][0], span[i][1].previousSibling);
assert.domEqual(span[i][2], span[i][1].nextSibling);
assert.domEqual(span[i][1], span[i][2].previousSibling);
assert.domEqual(span[i][3], span[i][2].nextSibling);
assert.domEqual(span[i][2], span[i][3].previousSibling);
assert.domNull(span[i][3].nextSibling);
assert.deepEqual(span[i].map(data), [1, 2 + d, 10, 20 + d].sort(d3.ascending));
}
},
"returns the current selection": function(span) {
span = d3.select("body"); // https://github.com/tmpvar/jsdom/issues/277
assert.isTrue(span.sort() === span);
}
}
});
function data(d) {
return d.__data__;
}
suite.export(module);

Просмотреть файл

@ -1,123 +0,0 @@
require("../env");
var vows = require("vows"),
assert = require("../env-assert");
var suite = vows.describe("selection.style");
suite.addBatch({
"select(body)": {
topic: function() {
return d3.select("body").html("");
},
"sets a property as a string": function(body) {
body.style("background-color", "red");
assert.equal(document.body.style.getPropertyValue("background-color"), "red");
},
"sets a property as a number": function(body) {
body.style("opacity", .3);
assert.equal(document.body.style.getPropertyValue("opacity"), "0.3");
},
"sets a property as a function": function(body) {
body.style("background-color", function() { return "orange"; });
assert.equal(document.body.style.getPropertyValue("background-color"), "orange");
},
"sets properties as a map of constants": function(body) {
body.style({"background-color": "white", opacity: .42});
assert.equal(document.body.style.getPropertyValue("background-color"), "white");
assert.equal(document.body.style.getPropertyValue("opacity"), "0.42");
},
"sets properties as a map of functions": function(body) {
body.data(["orange"]).style({"background-color": String, opacity: function(d, i) { return i; }});
assert.equal(document.body.style.getPropertyValue("background-color"), "orange");
assert.equal(document.body.style.getPropertyValue("opacity"), "0");
},
"gets a property value": function(body) {
document.body.style.setProperty("background-color", "yellow", "");
assert.equal(body.style("background-color"), "yellow");
},
"observes the specified priority": function(body) {
body.style("background-color", "green", "important");
assert.equal(document.body.style.getPropertyPriority("background-color"), "important");
body.style({opacity: .52}, "important");
assert.equal(document.body.style.getPropertyPriority("opacity"), "important");
body.style({visibility: function() { return "visible"; }}, "important");
assert.equal(document.body.style.getPropertyPriority("visibility"), "important");
},
"removes a property as null": function(body) {
body.style("background-color", "green").style("background-color", null);
assert.equal(body.style("background-color"), "");
},
"removes a property as a function": function(body) {
body.style("background-color", "green").style("background-color", function() { return null; });
assert.equal(body.style("background-color"), "");
},
"removes properties as a map of nulls": function(body) {
document.body.style.setProperty("background-color", "purple");
body.style({"background-color": null});
assert.equal(body.style("background-color"), "");
},
"removes properties as a map of functions that return null": function(body) {
document.body.style.setProperty("background-color", "purple");
body.style({"background-color": function() {}});
assert.equal(body.style("background-color"), "");
},
"returns the current selection": function(body) {
assert.isTrue(body.style("background-color", "green") === body);
}
}
});
suite.addBatch({
"selectAll(div)": {
topic: function() {
return d3.select("body").html("").selectAll("div").data(d3.range(2)).enter().append("div");
},
"sets a property as a string": function(div) {
div.style("background-color", "red");
assert.equal(div[0][0].style.getPropertyValue("background-color"), "red");
assert.equal(div[0][1].style.getPropertyValue("background-color"), "red");
},
"sets a property as a number": function(div) {
div.style("opacity", .5);
assert.equal(div[0][0].style.getPropertyValue("opacity"), "0.5");
assert.equal(div[0][1].style.getPropertyValue("opacity"), "0.5");
},
"sets a property as a function": function(div) {
div.style("background-color", d3.interpolateRgb("orange", "yellow"));
assert.equal(div[0][0].style.getPropertyValue("background-color"), "#ffa500");
assert.equal(div[0][1].style.getPropertyValue("background-color"), "#ffff00");
},
"gets a property value": function(div) {
div[0][0].style.setProperty("background-color", "green", "");
assert.equal(div.style("background-color"), "green");
},
"observes the specified priority": function(div) {
div.style("background-color", "blue", "important");
assert.equal(div[0][0].style.getPropertyPriority("background-color"), "important");
assert.equal(div[0][1].style.getPropertyPriority("background-color"), "important");
},
"removes a property as null": function(div) {
div.style("background-color", "red").style("background-color", null);
assert.equal(div[0][0].style.getPropertyValue("background-color"), "");
assert.equal(div[0][1].style.getPropertyValue("background-color"), "");
},
"removes a property as a function": function(div) {
div.style("background-color", "red").style("background-color", function() { return null; });
assert.equal(div[0][0].style.getPropertyValue("background-color"), "");
assert.equal(div[0][1].style.getPropertyValue("background-color"), "");
},
"ignores null nodes": function(div) {
var some = d3.selectAll("div");
some[0][1] = null;
some.style("background-color", null).style("background-color", "red");
assert.equal(div[0][0].style.getPropertyValue("background-color"), "red");
assert.equal(div[0][1].style.getPropertyValue("background-color"), "");
},
"returns the current selection": function(div) {
assert.isTrue(div.style("background-color", "green") === div);
}
}
});
suite.export(module);

Просмотреть файл

@ -1,30 +1,34 @@
require("../env");
var vows = require("vows"),
assert = require("../env-assert");
d3 = require("../../"),
load = require("../load"),
assert = require("../env-assert"),
document = d3.selection().node()._ownerDocument,
window = document.defaultView;
var suite = vows.describe("d3.selection");
suite.addBatch({
"selection": {
topic: function() {
return d3.selection();
},
"selects the document": function(selection) {
topic: load("selection/selection").sandbox({
document: document,
window: window
}),
"selects the document": function(d3) {
var selection = d3.selection();
assert.equal(selection.length, 1);
assert.equal(selection[0].length, 1);
assert.equal(selection[0][0], document);
},
"is an instanceof d3.selection": function(selection) {
assert.instanceOf(selection, d3.selection);
"is an instanceof d3.selection": function(d3) {
assert.instanceOf(d3.selection(), d3.selection);
},
"subselections are also instanceof d3.selection": function(selection) {
assert.instanceOf(selection.select("body"), d3.selection);
assert.instanceOf(selection.selectAll("body"), d3.selection);
"subselections are also instanceof d3.selection": function(d3) {
assert.instanceOf(d3.selection().select("body"), d3.selection);
assert.instanceOf(d3.selection().selectAll("body"), d3.selection);
},
"selection prototype can be extended": function(selection) {
"selection prototype can be extended": function(d3) {
d3.selection.prototype.foo = function(v) { return this.attr("foo", v); };
selection.select("body").foo(42);
d3.selection().select("body").foo(42);
assert.equal(document.body.getAttribute("foo"), "42");
delete d3.selection.prototype.foo;
}

Просмотреть файл

@ -1,113 +0,0 @@
require("../env");
var vows = require("vows"),
assert = require("../env-assert");
var suite = vows.describe("selection.text");
suite.addBatch({
"select(body)": {
topic: function() {
return d3.select("body").html("");
},
"sets the text content as a string": function(body) {
body.text("Hello, world!");
assert.equal(document.body.textContent, "Hello, world!");
},
"sets the text content as a number": function(body) {
body.text(42);
assert.equal(document.body.textContent, "42");
},
"sets the text content as a function": function(body) {
body.data(["Subject"]).text(function(d, i) { return "Hello, " + d + " " + i + "!"; });
assert.equal(document.body.textContent, "Hello, Subject 0!");
},
"escapes html content to text": function(body) {
body.text("<h1>Hello, world!</h1>");
assert.equal(document.body.textContent, "<h1>Hello, world!</h1>");
assert.equal(document.body.firstChild.nodeType, document.TEXT_NODE);
},
"clears the text content as null": function(body) {
body.text(null);
assert.equal(document.body.textContent, "");
},
"clears the text content as undefined": function(body) {
body.text(undefined);
assert.equal(document.body.textContent, "");
},
"clears the text content as a function returning null": function(body) {
body.text(function() { return null; });
assert.equal(document.body.textContent, "");
},
"clears the text content as a function returning undefined": function(body) {
body.text(function() { return undefined; });
assert.equal(document.body.textContent, "");
},
"ignores null nodes": function() {
var body = d3.select("body");
body[0][0] = null;
document.body.textContent = "foo";
body.text("bar");
assert.equal(document.body.textContent, "foo");
},
"returns the current selection": function(body) {
assert.isTrue(body.text("hello") === body);
}
}
});
suite.addBatch({
"selectAll(div)": {
topic: function() {
return d3.select("body").html("").selectAll("div").data(d3.range(2)).enter().append("div");
},
"sets the text content as a string": function(div) {
div.text("Hello, world!");
assert.equal(div[0][0].textContent, "Hello, world!");
assert.equal(div[0][1].textContent, "Hello, world!");
},
"sets the text content as a number": function(div) {
div.text(42);
assert.equal(div[0][0].textContent, "42");
assert.equal(div[0][1].textContent, "42");
},
"sets the text content as a function": function(div) {
div.data(["foo", "bar"]).text(function(d, i) { return "Hello, " + d + " " + i + "!"; });
assert.equal(div[0][0].textContent, "Hello, foo 0!");
assert.equal(div[0][1].textContent, "Hello, bar 1!");
},
"escapes html content to text": function(div) {
div.text("<h1>Hello, world!</h1>");
assert.equal(div[0][0].textContent, "<h1>Hello, world!</h1>");
assert.equal(div[0][1].textContent, "<h1>Hello, world!</h1>");
assert.equal(div[0][0].firstChild.nodeType, document.TEXT_NODE);
assert.equal(div[0][1].firstChild.nodeType, document.TEXT_NODE);
},
/*
https://github.com/tmpvar/jsdom/issues/276
"clears the text content as null": function(div) {
div.text(null);
assert.equal(div[0][0].textContent, "");
assert.equal(div[0][1].textContent, "");
},
"clears the text content as a function": function(div) {
div.text(function() { return null; });
assert.equal(dv[0][0].textContent, "");
assert.equal(dv[0][1].textContent, "");
},
*/
"ignores null nodes": function(div) {
div[0][0].textContent = "foo";
var some = d3.selectAll("div");
some[0][0] = null;
some.text("bar");
assert.equal(div[0][0].textContent, "foo");
assert.equal(div[0][1].textContent, "bar");
},
"returns the current selection": function(div) {
assert.isTrue(div.text("hello") === div);
}
}
});
suite.export(module);

Просмотреть файл

@ -0,0 +1,87 @@
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.sort");
suite.addBatch({
"selectAll(div).selectAll(span)": {
topic: load("selection/sort").sandbox({
document: document,
window: window
}),
"on a simple page": {
topic: function(d3) {
return d3.select("body").html("").selectAll("div")
.data([1, 2, 10, 20])
.enter().append("div").selectAll("span")
.data(function(d) { return [20 + d, 2 + d, 10, 1]; })
.enter().append("span");
},
"sorts elements by natural order": function(span) {
span.sort();
assert.domNull(span[0][0].previousSibling);
assert.domEqual(span[0][1].previousSibling, span[0][0]);
assert.domEqual(span[0][2].previousSibling, span[0][1]);
assert.domEqual(span[0][3].previousSibling, span[0][2]);
assert.domNull(span[0][3].nextSibling);
},
"sorts each group independently": function(span) {
span.sort(d3.descending);
assert.deepEqual(span[0].map(data), [21, 10, 3, 1]);
assert.deepEqual(span[1].map(data), [22, 10, 4, 1]);
assert.deepEqual(span[2].map(data), [30, 12, 10, 1]);
assert.deepEqual(span[3].map(data), [40, 22, 10, 1]);
},
"sorts using the specified comparator": function(span) {
span.sort(function(a, b) { return d3.ascending(a+"", b+""); });
assert.deepEqual(span[0].map(data), [1, 10, 21, 3]);
assert.deepEqual(span[1].map(data), [1, 10, 22, 4]);
assert.deepEqual(span[2].map(data), [1, 10, 12, 30]);
assert.deepEqual(span[3].map(data), [1, 10, 22, 40]);
},
"sorts null nodes at the end of the selection": function() {
var span = d3.selectAll("div").selectAll("span"), nulls = 0;
d3.select(span[0][0]).remove();
span[0][0] = null;
span.sort(function(a, b) { if ((a === null) || (b === null)) ++nulls; return a - b; });
assert.equal(nulls, 0);
assert.isNull(span[0][3]);
assert.domNull(span[0][0].previousSibling);
assert.domEqual(span[0][1], span[0][0].nextSibling);
assert.domEqual(span[0][0], span[0][1].previousSibling);
assert.domEqual(span[0][2], span[0][1].nextSibling);
assert.domEqual(span[0][1], span[0][2].previousSibling);
assert.domNull(span[0][2].nextSibling);
assert.deepEqual(span[0].slice(0, -1).map(data), [3, 10, 21]);
for (var i = 1; i < 4; ++i) {
var d = span[i].parentNode.__data__;
assert.domNull(span[i][0].previousSibling);
assert.domEqual(span[i][1], span[i][0].nextSibling);
assert.domEqual(span[i][0], span[i][1].previousSibling);
assert.domEqual(span[i][2], span[i][1].nextSibling);
assert.domEqual(span[i][1], span[i][2].previousSibling);
assert.domEqual(span[i][3], span[i][2].nextSibling);
assert.domEqual(span[i][2], span[i][3].previousSibling);
assert.domNull(span[i][3].nextSibling);
assert.deepEqual(span[i].map(data), [1, 2 + d, 10, 20 + d].sort(d3.ascending));
}
},
"returns the current selection": function(span) {
span = d3.select("body"); // https://github.com/tmpvar/jsdom/issues/277
assert.isTrue(span.sort() === span);
}
}
}
});
function data(d) {
return d.__data__;
}
suite.export(module);

Просмотреть файл

@ -0,0 +1,138 @@
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.style");
suite.addBatch({
"select(body)": {
topic: load("selection/style").sandbox({
document: document,
window: window
}),
"on a simple page": {
topic: function(d3) {
return d3.select("body").html("");
},
"sets a property as a string": function(body) {
body.style("background-color", "red");
assert.equal(document.body.style.getPropertyValue("background-color"), "red");
},
"sets a property as a number": function(body) {
body.style("opacity", .3);
assert.equal(document.body.style.getPropertyValue("opacity"), "0.3");
},
"sets a property as a function": function(body) {
body.style("background-color", function() { return "orange"; });
assert.equal(document.body.style.getPropertyValue("background-color"), "orange");
},
"sets properties as a map of constants": function(body) {
body.style({"background-color": "white", opacity: .42});
assert.equal(document.body.style.getPropertyValue("background-color"), "white");
assert.equal(document.body.style.getPropertyValue("opacity"), "0.42");
},
"sets properties as a map of functions": function(body) {
body.data(["orange"]).style({"background-color": String, opacity: function(d, i) { return i; }});
assert.equal(document.body.style.getPropertyValue("background-color"), "orange");
assert.equal(document.body.style.getPropertyValue("opacity"), "0");
},
"gets a property value": function(body) {
document.body.style.setProperty("background-color", "yellow", "");
assert.equal(body.style("background-color"), "yellow");
},
"observes the specified priority": function(body) {
body.style("background-color", "green", "important");
assert.equal(document.body.style.getPropertyPriority("background-color"), "important");
body.style({opacity: .52}, "important");
assert.equal(document.body.style.getPropertyPriority("opacity"), "important");
body.style({visibility: function() { return "visible"; }}, "important");
assert.equal(document.body.style.getPropertyPriority("visibility"), "important");
},
"removes a property as null": function(body) {
body.style("background-color", "green").style("background-color", null);
assert.equal(body.style("background-color"), "");
},
"removes a property as a function": function(body) {
body.style("background-color", "green").style("background-color", function() { return null; });
assert.equal(body.style("background-color"), "");
},
"removes properties as a map of nulls": function(body) {
document.body.style.setProperty("background-color", "purple");
body.style({"background-color": null});
assert.equal(body.style("background-color"), "");
},
"removes properties as a map of functions that return null": function(body) {
document.body.style.setProperty("background-color", "purple");
body.style({"background-color": function() {}});
assert.equal(body.style("background-color"), "");
},
"returns the current selection": function(body) {
assert.isTrue(body.style("background-color", "green") === body);
}
}
}
});
suite.addBatch({
"selectAll(div)": {
topic: load("selection/style").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 a property as a string": function(div) {
div.style("background-color", "red");
assert.equal(div[0][0].style.getPropertyValue("background-color"), "red");
assert.equal(div[0][1].style.getPropertyValue("background-color"), "red");
},
"sets a property as a number": function(div) {
div.style("opacity", .5);
assert.equal(div[0][0].style.getPropertyValue("opacity"), "0.5");
assert.equal(div[0][1].style.getPropertyValue("opacity"), "0.5");
},
"sets a property as a function": function(div) {
div.style("background-color", d3.interpolateRgb("orange", "yellow"));
assert.equal(div[0][0].style.getPropertyValue("background-color"), "#ffa500");
assert.equal(div[0][1].style.getPropertyValue("background-color"), "#ffff00");
},
"gets a property value": function(div) {
div[0][0].style.setProperty("background-color", "green", "");
assert.equal(div.style("background-color"), "green");
},
"observes the specified priority": function(div) {
div.style("background-color", "blue", "important");
assert.equal(div[0][0].style.getPropertyPriority("background-color"), "important");
assert.equal(div[0][1].style.getPropertyPriority("background-color"), "important");
},
"removes a property as null": function(div) {
div.style("background-color", "red").style("background-color", null);
assert.equal(div[0][0].style.getPropertyValue("background-color"), "");
assert.equal(div[0][1].style.getPropertyValue("background-color"), "");
},
"removes a property as a function": function(div) {
div.style("background-color", "red").style("background-color", function() { return null; });
assert.equal(div[0][0].style.getPropertyValue("background-color"), "");
assert.equal(div[0][1].style.getPropertyValue("background-color"), "");
},
"returns the current selection": function(div) {
assert.isTrue(div.style("background-color", "green") === 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.style("background-color", null).style("background-color", "red");
assert.equal(div[0][0].style.getPropertyValue("background-color"), "red");
assert.equal(div[0][1].style.getPropertyValue("background-color"), "");
}
}
});
suite.export(module);

128
test/selection/text-test.js Normal file
Просмотреть файл

@ -0,0 +1,128 @@
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.text");
suite.addBatch({
"select(body)": {
topic: load("selection/text").sandbox({
document: document,
window: window
}),
"on a simple page": {
topic: function(d3) {
return d3.select("body").html("");
},
"sets the text content as a string": function(body) {
body.text("Hello, world!");
assert.equal(document.body.textContent, "Hello, world!");
},
"sets the text content as a number": function(body) {
body.text(42);
assert.equal(document.body.textContent, "42");
},
"sets the text content as a function": function(body) {
body.data(["Subject"]).text(function(d, i) { return "Hello, " + d + " " + i + "!"; });
assert.equal(document.body.textContent, "Hello, Subject 0!");
},
"escapes html content to text": function(body) {
body.text("<h1>Hello, world!</h1>");
assert.equal(document.body.textContent, "<h1>Hello, world!</h1>");
assert.equal(document.body.firstChild.nodeType, document.TEXT_NODE);
},
"clears the text content as null": function(body) {
body.text(null);
assert.equal(document.body.textContent, "");
},
"clears the text content as undefined": function(body) {
body.text(undefined);
assert.equal(document.body.textContent, "");
},
"clears the text content as a function returning null": function(body) {
body.text(function() { return null; });
assert.equal(document.body.textContent, "");
},
"clears the text content as a function returning undefined": function(body) {
body.text(function() { return undefined; });
assert.equal(document.body.textContent, "");
},
"returns the current selection": function(body) {
assert.isTrue(body.text("hello") === body);
}
},
"ignores null nodes": function(d3) {
var body = d3.select("body");
body[0][0] = null;
document.body.textContent = "foo";
body.text("bar");
assert.equal(document.body.textContent, "foo");
}
}
});
suite.addBatch({
"selectAll(div)": {
topic: load("selection/text").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 the text content as a string": function(div) {
div.text("Hello, world!");
assert.equal(div[0][0].textContent, "Hello, world!");
assert.equal(div[0][1].textContent, "Hello, world!");
},
"sets the text content as a number": function(div) {
div.text(42);
assert.equal(div[0][0].textContent, "42");
assert.equal(div[0][1].textContent, "42");
},
"sets the text content as a function": function(div) {
div.data(["foo", "bar"]).text(function(d, i) { return "Hello, " + d + " " + i + "!"; });
assert.equal(div[0][0].textContent, "Hello, foo 0!");
assert.equal(div[0][1].textContent, "Hello, bar 1!");
},
"escapes html content to text": function(div) {
div.text("<h1>Hello, world!</h1>");
assert.equal(div[0][0].textContent, "<h1>Hello, world!</h1>");
assert.equal(div[0][1].textContent, "<h1>Hello, world!</h1>");
assert.equal(div[0][0].firstChild.nodeType, document.TEXT_NODE);
assert.equal(div[0][1].firstChild.nodeType, document.TEXT_NODE);
},
/*
https://github.com/tmpvar/jsdom/issues/276
"clears the text content as null": function(div) {
div.text(null);
assert.equal(div[0][0].textContent, "");
assert.equal(div[0][1].textContent, "");
},
"clears the text content as a function": function(div) {
div.text(function() { return null; });
assert.equal(dv[0][0].textContent, "");
assert.equal(dv[0][1].textContent, "");
},
*/
"returns the current selection": function(div) {
assert.isTrue(div.text("hello") === div);
}
},
"ignores null nodes": function(d3) {
var div = d3.select("body").html("").selectAll("div").data([0, 1]).enter().append("div");
div[0][0].textContent = "foo";
var some = d3.selectAll("div");
some[0][0] = null;
some.text("bar");
assert.equal(div[0][0].textContent, "foo");
assert.equal(div[0][1].textContent, "bar");
}
}
});
suite.export(module);