diff --git a/package.json b/package.json index bdb25ca..1b2f925 100644 --- a/package.json +++ b/package.json @@ -30,9 +30,11 @@ }, "devDependencies": { "d3-scale": "1", + "d3-selection": "^1.1.0", "eslint": "3", - "package-preamble": "0.0", - "rollup": "0.41", + "jsdom": "11", + "package-preamble": "0.1", + "rollup": "0.42", "tape": "4", "uglify-js": "^2.8.11" } diff --git a/test/axis-left-500.html b/test/axis-left-500.html new file mode 100644 index 0000000..f713f13 --- /dev/null +++ b/test/axis-left-500.html @@ -0,0 +1,50 @@ + + + + + + + 0.0 + + + + 0.1 + + + + 0.2 + + + + 0.3 + + + + 0.4 + + + + 0.5 + + + + 0.6 + + + + 0.7 + + + + 0.8 + + + + 0.9 + + + + 1.0 + + + diff --git a/test/axis-left.html b/test/axis-left.html new file mode 100644 index 0000000..01160c3 --- /dev/null +++ b/test/axis-left.html @@ -0,0 +1,50 @@ + + + + + + + 0.0 + + + + 0.1 + + + + 0.2 + + + + 0.3 + + + + 0.4 + + + + 0.5 + + + + 0.6 + + + + 0.7 + + + + 0.8 + + + + 0.9 + + + + 1.0 + + + diff --git a/test/axis-test.js b/test/axis-test.js index 7068d73..8bb80b9 100644 --- a/test/axis-test.js +++ b/test/axis-test.js @@ -1,10 +1,12 @@ -var tape = require("tape"), - scale = require("d3-scale"), - axis = require("../"); +var fs = require("fs"), + path = require("path"), + tape = require("tape"), + jsdom = require("jsdom"), + d3 = Object.assign({}, require("d3-scale"), require("d3-selection"), require("../")); tape("axisLeft(scale) has the expected defaults", function(test) { - var s = scale.scaleLinear(), - a = axis.axisLeft(s); + var s = d3.scaleLinear(), + a = d3.axisLeft(s); test.equal(a.scale(), s); test.deepEqual(a.tickArguments(), []); test.equal(a.tickValues(), null); @@ -17,7 +19,7 @@ tape("axisLeft(scale) has the expected defaults", function(test) { }); tape("axis.ticks(arguments…) sets the tick arguments", function(test) { - var a = axis.axisLeft(scale.scaleLinear()).ticks(20); + var a = d3.axisLeft(d3.scaleLinear()).ticks(20); test.deepEqual(a.tickArguments(), [20]); a.ticks(); test.deepEqual(a.tickArguments(), []); @@ -25,13 +27,13 @@ tape("axis.ticks(arguments…) sets the tick arguments", function(test) { }); tape("axis.tickArguments(null) sets the tick arguments to the empty array", function(test) { - var a = axis.axisLeft(scale.scaleLinear()).tickArguments(null); + var a = d3.axisLeft(d3.scaleLinear()).tickArguments(null); test.deepEqual(a.tickArguments(), []); test.end(); }); tape("axis.tickArguments() makes a defensive copy of the tick arguments", function(test) { - var a = axis.axisLeft(scale.scaleLinear()).tickArguments([20]), + var a = d3.axisLeft(d3.scaleLinear()).tickArguments([20]), v = a.tickArguments(); v.push(10); test.deepEqual(a.tickArguments(), [20]); @@ -39,7 +41,7 @@ tape("axis.tickArguments() makes a defensive copy of the tick arguments", functi }); tape("axis.tickValues(null) clears any explicitly-set tick values", function(test) { - var a = axis.axisLeft(scale.scaleLinear()).tickValues([1, 2, 3]); + var a = d3.axisLeft(d3.scaleLinear()).tickValues([1, 2, 3]); test.deepEqual(a.tickValues(), [1, 2, 3]); a.tickValues([]); test.deepEqual(a.tickValues(), []); @@ -49,23 +51,43 @@ tape("axis.tickValues(null) clears any explicitly-set tick values", function(tes }); tape("axis.tickValues(values) sets the tick values explicitly", function(test) { - var a = axis.axisLeft(scale.scaleLinear()).tickValues([1, 2, 3]); + var a = d3.axisLeft(d3.scaleLinear()).tickValues([1, 2, 3]); test.deepEqual(a.tickValues(), [1, 2, 3]); test.end(); }); tape("axis.tickValues(values) makes a defensive copy of the specified tick values", function(test) { var v = [1, 2, 3], - a = axis.axisLeft(scale.scaleLinear()).tickValues(v); + a = d3.axisLeft(d3.scaleLinear()).tickValues(v); v.push(4); test.deepEqual(a.tickValues(), [1, 2, 3]); test.end(); }); tape("axis.tickValues() makes a defensive copy of the tick values", function(test) { - var a = axis.axisLeft(scale.scaleLinear()).tickValues([1, 2, 3]), + var a = d3.axisLeft(d3.scaleLinear()).tickValues([1, 2, 3]), v = a.tickValues(); v.push(4); test.deepEqual(a.tickValues(), [1, 2, 3]); test.end(); }); + +tape("axisLeft(selection) produces the expected result", function(test) { + var bodyActual = (new jsdom.JSDOM("")).window.document.body, + bodyExpected = (new jsdom.JSDOM(file("axis-left.html"))).window.document.body; + d3.select(bodyActual).select("g").call(d3.axisLeft(d3.scaleLinear())); + test.equal(bodyActual.outerHTML, bodyExpected.outerHTML); + test.end(); +}); + +tape("axisLeft.scale(nonNumericRangeScale)(selection) produces the expected result", function(test) { + var bodyActual = (new jsdom.JSDOM("")).window.document.body, + bodyExpected = (new jsdom.JSDOM(file("axis-left-500.html"))).window.document.body; + d3.select(bodyActual).select("g").call(d3.axisLeft(d3.scaleLinear().range([0, "500"]))); + test.equal(bodyActual.outerHTML, bodyExpected.outerHTML); + test.end(); +}); + +function file(file) { + return fs.readFileSync(path.join(__dirname, file), "utf8").replace(/\n\s*/mg, ""); +}