Commit ecbf93d9 authored by Mike Bostock's avatar Mike Bostock
Browse files

Standardize test environment.

parent 4c3469d3
var vows = require("vows"),
d3 = require("../../"),
load = require("../load"),
xhr = require("../env-xhr"),
assert = require("../env-assert");
var suite = vows.describe("d3.csv");
suite.addBatch({
"csv": {
topic: load("dsv/csv")
.expression("d3.csv")
.sandbox({XMLHttpRequest: xhr, document: {}, window: {}}),
topic: load("dsv/csv").expression("d3.csv").document(),
"on a sample file": {
topic: function(csv) {
......@@ -20,7 +16,7 @@ suite.addBatch({
assert.deepEqual(csv, [{"Hello":"42","World":"\"fish\""}]);
},
"overrides the mime type to text/csv": function(csv) {
assert.equal(xhr._last._info.mimeType, "text/csv");
assert.equal(XMLHttpRequest._last._info.mimeType, "text/csv");
}
},
......
var vows = require("vows"),
d3 = require("../../"),
load = require("../load"),
xhr = require("../env-xhr"),
assert = require("../env-assert");
var suite = vows.describe("d3.tsv");
suite.addBatch({
"tsv": {
topic: load("dsv/tsv")
.expression("d3.tsv")
.sandbox({XMLHttpRequest: xhr, document: {}, window: {}}),
topic: load("dsv/tsv").expression("d3.tsv").document(),
"on a sample file": {
topic: function(tsv) {
......@@ -20,7 +16,7 @@ suite.addBatch({
assert.deepEqual(tsv, [{"Hello":42,"World":"\"fish\""}]);
},
"overrides the mime type to text/tab-separated-values": function(tsv) {
assert.equal(xhr._last._info.mimeType, "text/tab-separated-values");
assert.equal(XMLHttpRequest._last._info.mimeType, "text/tab-separated-values");
}
},
......
var fs = require("fs");
module.exports = function XMLHttpRequest() {
global.XMLHttpRequest = function XMLHttpRequest() {
var self = this,
info = self._info = {},
headers = {},
......
......@@ -6,9 +6,7 @@ var suite = vows.describe("d3.timer");
suite.addBatch({
"timer": {
topic: load("event/timer")
.expression("d3.timer")
.sandbox({document: {}, window: {}, setTimeout: setTimeout, clearTimeout: clearTimeout}),
topic: load("event/timer").expression("d3.timer").document(),
"with no delay": {
topic: delay(),
......
......@@ -6,10 +6,7 @@ var suite = vows.describe("d3.interpolateArray");
suite.addBatch({
"interpolateArray": {
topic: load("interpolate/array")
.expression("d3.interpolateArray")
.sandbox({document: null, window: null}),
topic: load("interpolate/array").expression("d3.interpolateArray").document(),
"interpolates defined elements": function(interpolate) {
assert.deepEqual(interpolate([2, 12], [4, 24])(.5), [3, 18]);
},
......
......@@ -6,9 +6,7 @@ var suite = vows.describe("d3.interpolate");
suite.addBatch({
"interpolate": {
topic: load("interpolate/interpolate")
.sandbox({document: null, window: null}),
topic: load("interpolate/interpolate").document(),
"interpolates numbers": function(d3) {
assert.equal(d3.interpolate(2, 12)(.4), 6);
assert.equal(d3.interpolate("2px", 12)(.4), 6);
......@@ -53,9 +51,7 @@ suite.addBatch({
}
},
"interpolators": {
topic: load("interpolate/interpolate")
.sandbox({document: null, window: null}),
topic: load("interpolate/interpolate").document(),
"can register a custom interpolator": function(d3) {
d3.interpolators.push(function(a, b) {
return a == "one" && b == "two" && d3.interpolateNumber(1, 2);
......
......@@ -6,10 +6,7 @@ var suite = vows.describe("d3.interpolateObject");
suite.addBatch({
"interpolateObject": {
topic: load("interpolate/object")
.expression("d3.interpolateObject")
.sandbox({document: null, window: null}),
topic: load("interpolate/object").expression("d3.interpolateObject").document(),
"interpolates defined properties": function(interpolate) {
assert.deepEqual(interpolate({a: 2, b: 12}, {a: 4, b: 24})(.5), {a: 3, b: 18});
},
......
......@@ -6,9 +6,7 @@ var suite = vows.describe("d3.layout.force");
suite.addBatch({
"force": {
topic: load("layout/force")
.expression("d3.layout.force")
.sandbox({document: {}, window: {}, setTimeout: setTimeout, clearTimeout: clearTimeout}),
topic: load("layout/force").expression("d3.layout.force").document(),
"default instance": {
topic: function(force) {
......
var smash = require("smash"),
jsdom = require("jsdom");
jsdom = require("jsdom"),
xhr = require("./env-xhr");
module.exports = function() {
var files = [].slice.call(arguments).map(function(d) { return "src/" + d; }),
......@@ -25,7 +26,22 @@ module.exports = function() {
topic.document = function(_) {
var document = jsdom.jsdom("<html><head></head><body></body></html>");
sandbox = {document: document, window: document.createWindow()};
// Monkey-patch createRange support to JSDOM.
document.createRange = function() {
return {
selectNode: function() {},
createContextualFragment: jsdom.jsdom
};
};
sandbox = {
XMLHttpRequest: XMLHttpRequest,
document: document,
window: document.createWindow(),
setTimeout: setTimeout,
clearTimeout: clearTimeout
};
return topic;
};
......
......@@ -6,9 +6,7 @@ var suite = vows.describe("d3.scale.identity");
suite.addBatch({
"identity": {
topic: load("scale/identity")
.expression("d3.scale.identity")
.sandbox({document: null, window: null}),
topic: load("scale/identity").expression("d3.scale.identity").document(),
"domain and range": {
"are identical": function(identity) {
......
......@@ -6,8 +6,7 @@ var suite = vows.describe("d3.scale.linear");
suite.addBatch({
"linear": {
topic: load("scale/linear", "interpolate/hsl") // beware instanceof d3_Color
.sandbox({document: null, window: null}),
topic: load("scale/linear", "interpolate/hsl").document(), // beware instanceof d3_Color
"domain": {
"defaults to [0, 1]": function(d3) {
......
......@@ -6,8 +6,7 @@ var suite = vows.describe("d3.scale.log");
suite.addBatch({
"log": {
topic: load("scale/log", "interpolate/hsl") // beware instanceof d3_Color
.sandbox({document: null, window: null}),
topic: load("scale/log", "interpolate/hsl").document(), // beware instanceof d3_Color
"domain": {
"defaults to [1, 10]": function(d3) {
......
......@@ -6,8 +6,7 @@ var suite = vows.describe("d3.scale.pow");
suite.addBatch({
"pow": {
topic: load("scale/pow", "interpolate/hsl") // beware instance of d3_Colorr
.sandbox({document: null, window: null}),
topic: load("scale/pow", "interpolate/hsl").document(), // beware instance of d3_Colorr
"domain": {
"defaults to [0, 1]": function(d3) {
......
......@@ -6,8 +6,7 @@ var suite = vows.describe("d3.scale.sqrt");
suite.addBatch({
"sqrt": {
topic: load("scale/sqrt", "interpolate/hsl") // beware instanceof d3_Color
.sandbox({document: null, window: null}),
topic: load("scale/sqrt", "interpolate/hsl").document(), // beware instanceof d3_Color
"domain": {
"defaults to [0, 1]": function(d3) {
......
var vows = require("vows"),
d3 = require("../../"),
ordinal = require("../../").scale.ordinal,
load = require("../load"),
assert = require("../env-assert"),
document = d3.selection().node()._ownerDocument,
window = document.defaultView;
assert = require("../env-assert");
var suite = vows.describe("d3.svg.axis");
suite.addBatch({
"axis": {
topic: load("svg/axis")
.expression("d3.svg.axis")
.sandbox({document: document, window: window}),
topic: load("svg/axis").document(),
"scale": {
"defaults to a linear scale": function(axis) {
var a = axis(), x = a.scale();
"defaults to a linear scale": function(d3) {
var a = d3.svg.axis(), x = a.scale();
assert.deepEqual(x.domain(), [0, 1]);
assert.deepEqual(x.range(), [0, 1]);
assert.equal(x(0.5), 0.5);
},
"can be defined as a scale object": function(axis) {
var x = d3.scale.linear(), a = axis().scale(x);
"can be defined as a scale object": function(d3) {
var x = d3.scale.linear(), a = d3.svg.axis().scale(x);
assert.equal(a.scale(), x);
},
"can be a polylinear scale": function(axis) {
var a = axis().scale(d3.scale.linear().domain([0, 1, 10]).range([2, 20, 200])),
"can be a polylinear scale": function(d3) {
var a = d3.svg.axis().scale(d3.scale.linear().domain([0, 1, 10]).range([2, 20, 200])),
g = d3.select("body").html("").append("svg:g").call(a),
path = g.selectAll("path");
assert.equal(path.attr("d"), "M2,6V0H200V6");
},
"can be an ordinal scale": function(axis) {
var a = axis().scale(d3.scale.ordinal().domain(["A", "B", "C"]).rangeBands([10, 90])),
"can be an ordinal scale": function(d3) {
var a = d3.svg.axis().scale(ordinal().domain(["A", "B", "C"]).rangeBands([10, 90])),
g = d3.select("body").html("").append("svg:g").call(a),
path = g.selectAll("path");
assert.equal(path.attr("d"), "M10,6V0H90V6");
},
"can be an ordinal scale with explicit range": function(axis) {
var a = axis().scale(d3.scale.ordinal().domain(["A", "B", "C"]).range([10, 50, 90])),
"can be an ordinal scale with explicit range": function(d3) {
var a = d3.svg.axis().scale(ordinal().domain(["A", "B", "C"]).range([10, 50, 90])),
g = d3.select("body").html("").append("svg:g").call(a),
path = g.selectAll("path");
assert.equal(path.attr("d"), "M10,6V0H90V6");
......@@ -45,20 +41,20 @@ suite.addBatch({
},
"orient": {
"defaults to bottom": function(axis) {
var a = axis();
"defaults to bottom": function(d3) {
var a = d3.svg.axis();
assert.equal(a.orient(), "bottom");
},
"defaults to bottom when an invalid orientation is specified": function(axis) {
var a = axis().orient("invalid");
"defaults to bottom when an invalid orientation is specified": function(d3) {
var a = d3.svg.axis().orient("invalid");
assert.equal(a.orient(), "bottom");
},
"coerces to a string": function(axis) {
var a = axis().orient({toString: function() { return "left"; }});
"coerces to a string": function(d3) {
var a = d3.svg.axis().orient({toString: function() { return "left"; }});
assert.equal(a.orient(), "left");
},
"supports top orientation": function(axis) {
var a = axis().orient("top"),
"supports top orientation": function(d3) {
var a = d3.svg.axis().orient("top"),
g = d3.select("body").html("").append("svg:g").call(a),
tick = g.select("g:nth-child(3)"),
text = tick.select("text"),
......@@ -72,8 +68,8 @@ suite.addBatch({
assert.equal(line.attr("y2"), -6);
assert.equal(path.attr("d"), "M0,-6V0H1V-6");
},
"supports right orientation": function(axis) {
var a = axis().orient("right"),
"supports right orientation": function(d3) {
var a = d3.svg.axis().orient("right"),
g = d3.select("body").html("").append("svg:g").call(a),
tick = g.select("g:nth-child(3)"),
text = tick.select("text"),
......@@ -87,8 +83,8 @@ suite.addBatch({
assert.equal(line.attr("x2"), 6);
assert.equal(path.attr("d"), "M6,0H0V1H6");
},
"supports bottom orientation": function(axis) {
var a = axis().orient("bottom"),
"supports bottom orientation": function(d3) {
var a = d3.svg.axis().orient("bottom"),
g = d3.select("body").html("").append("svg:g").call(a),
tick = g.select("g:nth-child(3)"),
text = tick.select("text"),
......@@ -102,8 +98,8 @@ suite.addBatch({
assert.equal(line.attr("y2"), 6);
assert.equal(path.attr("d"), "M0,6V0H1V6");
},
"supports left orientation": function(axis) {
var a = axis().orient("left"),
"supports left orientation": function(d3) {
var a = d3.svg.axis().orient("left"),
g = d3.select("body").html("").append("svg:g").call(a),
tick = g.select("g:nth-child(3)"),
text = tick.select("text"),
......@@ -120,34 +116,34 @@ suite.addBatch({
},
"tickSize": {
"defaults to six pixels": function(axis) {
var a = axis();
"defaults to six pixels": function(d3) {
var a = d3.svg.axis();
assert.equal(a.tickSize(), 6);
},
"can be defined as a number": function(axis) {
var a = axis().tickSize(3);
"can be defined as a number": function(d3) {
var a = d3.svg.axis().tickSize(3);
assert.equal(a.tickSize(), 3);
},
"coerces input value to a number": function(axis) {
var a = axis().tickSize("3");
"coerces input value to a number": function(d3) {
var a = d3.svg.axis().tickSize("3");
assert.strictEqual(a.tickSize(), 3);
},
"affects the generated domain path": function(axis) {
var a = axis().tickSize(3),
"affects the generated domain path": function(d3) {
var a = d3.svg.axis().tickSize(3),
g = d3.select("body").html("").append("svg:g").call(a),
path = g.select("path.domain");
assert.equal(path.attr("d"), "M0,3V0H1V3");
},
"affects the generated tick lines": function(axis) {
var a = axis().tickSize(3),
"affects the generated tick lines": function(d3) {
var a = d3.svg.axis().tickSize(3),
g = d3.select("body").html("").append("svg:g").call(a),
line = g.selectAll("g line");
line.each(function() {
assert.equal(d3.select(this).attr("y2"), 3);
});
},
"if negative, labels are placed on the opposite end": function(axis) {
var a = axis().tickSize(-80),
"if negative, labels are placed on the opposite end": function(d3) {
var a = d3.svg.axis().tickSize(-80),
g = d3.select("body").html("").append("svg:g").call(a),
line = g.selectAll("g line"),
text = g.selectAll("g text");
......@@ -158,14 +154,14 @@ suite.addBatch({
assert.equal(d3.select(this).attr("y"), 3);
});
},
"with two arguments, specifies end tick size": function(axis) {
var a = axis().tickSize(6, 3),
"with two arguments, specifies end tick size": function(d3) {
var a = d3.svg.axis().tickSize(6, 3),
g = d3.select("body").html("").append("svg:g").call(a),
path = g.selectAll("path");
assert.equal(path.attr("d"), "M0,3V0H1V3");
},
"with three arguments, specifies end and minor tick sizes": function(axis) {
var a = axis().tickSubdivide(3).tickSize(6, 3, 9),
"with three arguments, specifies end and minor tick sizes": function(d3) {
var a = d3.svg.axis().tickSubdivide(3).tickSize(6, 3, 9),
g = d3.select("body").html("").append("svg:g").call(a),
path = g.selectAll("path"),
line = g.select(".minor");
......@@ -175,20 +171,20 @@ suite.addBatch({
},
"tickPadding": {
"defaults to three pixels": function(axis) {
var a = axis();
"defaults to three pixels": function(d3) {
var a = d3.svg.axis();
assert.equal(a.tickPadding(), 3);
},
"can be defined as a number": function(axis) {
var a = axis().tickPadding(6);
"can be defined as a number": function(d3) {
var a = d3.svg.axis().tickPadding(6);
assert.equal(a.tickPadding(), 6);
},
"coerces input value to a number": function(axis) {
var a = axis().tickPadding("6");
"coerces input value to a number": function(d3) {
var a = d3.svg.axis().tickPadding("6");
assert.strictEqual(a.tickPadding(), 6);
},
"affects the generated tick labels": function(axis) {
var a = axis().tickSize(2).tickPadding(7),
"affects the generated tick labels": function(d3) {
var a = d3.svg.axis().tickSize(2).tickPadding(7),
g = d3.select("body").html("").append("svg:g").call(a),
text = g.selectAll("g text");
text.each(function() {
......@@ -198,18 +194,18 @@ suite.addBatch({
},
"ticks": {
"defaults to [10]": function(axis) {
var a = axis();
"defaults to [10]": function(d3) {
var a = d3.svg.axis();
assert.deepEqual(a.ticks(), [10]);
},
"can be defined as any arguments": function(axis) {
var b = {}, a = axis().ticks(b, 42), t = a.ticks();
"can be defined as any arguments": function(d3) {
var b = {}, a = d3.svg.axis().ticks(b, 42), t = a.ticks();
assert.equal(t[0], b);
assert.equal(t[1], 42);
assert.equal(t.length, 2);
},
"passes any arguments to the scale's ticks function": function(axis) {
var x = d3.scale.linear(), b = {}, a = axis().ticks(b, "%").scale(x), aa = [],
"passes any arguments to the scale's ticks function": function(d3) {
var x = d3.scale.linear(), b = {}, a = d3.svg.axis().ticks(b, "%").scale(x), aa = [],
g = d3.select("body").html("").append("svg:g");
x.ticks = function() { aa.push(arguments); return [42]; };
g.call(a);
......@@ -218,10 +214,10 @@ suite.addBatch({
assert.equal(aa[0][0], b);
assert.equal(aa[0][1], "%");
},
"passes any arguments to the scale's tickFormat function": function(axis) {
"passes any arguments to the scale's tickFormat function": function(d3) {
var b = {},
x = d3.scale.linear(),
a = axis().scale(x).ticks(b, "%"),
a = d3.svg.axis().scale(x).ticks(b, "%"),
g = d3.select("body").html("").append("svg:g"),
aa = [];
......@@ -236,15 +232,15 @@ suite.addBatch({
assert.equal(aa[0][0], b);
assert.equal(aa[0][1], "%");
},
"affects the generated ticks": function(axis) {
var a = axis().ticks(20, "%"),
"affects the generated ticks": function(d3) {
var a = d3.svg.axis().ticks(20, "%"),
g = d3.select("body").html("").append("svg:g").call(a),
t = g.selectAll("g");
assert.equal(t[0].length, 21);
assert.equal(t[0][0].textContent, "0%");
},
"only substitutes precision if not specified": function(axis) {
var a = axis().ticks(20, ".5%"),
"only substitutes precision if not specified": function(d3) {
var a = d3.svg.axis().ticks(20, ".5%"),
g = d3.select("body").html("").append("svg:g").call(a),
t = g.selectAll("g");
assert.equal(t[0].length, 21);
......@@ -253,24 +249,24 @@ suite.addBatch({
},
"tickValues": {
"defaults to null": function(axis) {
var a = axis().tickValues();
"defaults to null": function(d3) {
var a = d3.svg.axis().tickValues();
assert.isNull(a);
},
"can be given as array of positions": function(axis) {
var l = [1, 2.5, 3], a = axis().tickValues(l), t = a.tickValues();
"can be given as array of positions": function(d3) {
var l = [1, 2.5, 3], a = d3.svg.axis().tickValues(l), t = a.tickValues();
assert.equal(t, l);
assert.equal(t.length, 3);
},
"does not change the tick arguments": function(axis) {
var b = {}, a = axis().ticks(b, 42).tickValues([10]), t = a.ticks();
"does not change the tick arguments": function(d3) {
var b = {}, a = d3.svg.axis().ticks(b, 42).tickValues([10]), t = a.ticks();
assert.equal(t[0], b);
assert.equal(t[1], 42);
assert.equal(t.length, 2);
},
"does not change the arguments passed to the scale's tickFormat function": function(axis) {
"does not change the arguments passed to the scale's tickFormat function": function(d3) {
var x = d3.scale.linear(),
a = axis().scale(x).ticks(10).tickValues([1, 2, 3]),
a = d3.svg.axis().scale(x).ticks(10).tickValues([1, 2, 3]),
g = d3.select("body").html("").append("svg:g"),
aa = [];
......@@ -284,8 +280,8 @@ suite.addBatch({
assert.equal(aa[0].length, 1);
assert.equal(aa[0][0], 10);
},
"affects the generated ticks": function(axis) {
var a = axis().ticks(20),
"affects the generated ticks": function(d3) {
var a = d3.svg.axis().ticks(20),
g = d3.select("body").html("").append("svg:g").call(a),
t = g.selectAll("g");
assert.equal(t[0].length, 21);
......@@ -293,20 +289,20 @@ suite.addBatch({
},
"tickSubdivide": {
"defaults to zero": function(axis) {
var a = axis();
"defaults to zero": function(d3) {
var a = d3.svg.axis();
assert.equal(a.tickSubdivide(), 0);
},
"coerces input value to a number": function(axis) {
var a = axis().tickSubdivide(true);
"coerces input value to a number": function(d3) {
var a = d3.svg.axis().tickSubdivide(true);
assert.strictEqual(a.tickSubdivide(), 1);
},
"does not generate minor ticks when zero": function(axis) {
var g = d3.select("body").html("").append("svg:g").call(axis());
"does not generate minor ticks when zero": function(d3) {
var g = d3.select("body").html("").append("svg:g").call(d3.svg.axis());
assert.isTrue(g.selectAll(".minor").empty());
},
"affects the generated minor ticks": function(axis) {
var a = axis().tickSubdivide(3),
"affects the generated minor ticks": function(d3) {
var a = d3.svg.axis().tickSubdivide(3),
g = d3.select("body").html("").append("svg:g").call(a),
t = g.selectAll("line.tick.minor");
assert.equal(t[0].length, 30);
......@@ -315,12 +311,12 @@ suite.addBatch({
},
"tickFormat": {
"defaults to null": function(axis) {
var a = axis();
"defaults to null": function(d3) {
var a = d3.svg.axis();
assert.isTrue(a.tickFormat() == null);
},
"when null, uses the scale's tick format": function(axis) {
var x = d3.scale.linear(), a = axis().scale(x),
"when null, uses the scale's tick format": function(d3) {
var x = d3.scale.linear(), a = d3.svg.axis().scale(x),
g = d3.select("body").html("").append("svg:g");