-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
1,296 additions
and
388 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
var jsdom = require("jsdom"); | ||
|
||
module.exports = function(html) { | ||
return (new jsdom.JSDOM(html)).window.document; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
const tape = require("tape"), | ||
jsdom = require("./jsdom"), | ||
d3 = Object.assign(require("../"), require("d3-selection"), require("d3-transition")); | ||
|
||
// temporary fix (while d3-transition still requests d3-selection@1) | ||
d3.selection.prototype.interrupt = function(name) { | ||
return this.each(function() { | ||
d3.interrupt(this, name); | ||
}); | ||
}; | ||
|
||
// d3-zoom expects global navigator and SVGElement to exist | ||
global.navigator = {}; | ||
global.SVGElement = function(){}; | ||
|
||
const document = jsdom("<body>"), | ||
div = d3.select(document.body).append("div").datum("hello"), | ||
zoom = d3.zoom(), | ||
identity = d3.zoomIdentity; | ||
|
||
div.call(zoom); | ||
|
||
tape("zoom.on('zoom') callback", function(test) { | ||
let a; | ||
zoom.on("zoom", function(event, d) { a = {event, d, that: this}; }); | ||
div.call(zoom.transform, identity); | ||
const event = { type: "zoom", sourceEvent: null, target: zoom, transform: {k: 1, x: 0, y: 0}}; | ||
test.deepEqual(a.event, event); | ||
test.equal(a.d, "hello"); | ||
test.equal(a.that, div.node()); | ||
|
||
a = {}; | ||
zoom.on("zoom", null); | ||
div.call(zoom.transform, identity); | ||
test.deepEqual(a.event, undefined); | ||
test.equal(a.d, undefined); | ||
test.equal(a.that, undefined); | ||
|
||
test.end(); | ||
}); | ||
|
||
tape("zoom.on('start') callback", function(test) { | ||
let a; | ||
zoom.on("start", function(event, d) { a = {event, d, that: this}; }); | ||
div.call(zoom.transform, identity); | ||
const event = { type: "start", sourceEvent: null, target: zoom, transform: {k: 1, x: 0, y: 0}}; | ||
test.deepEqual(a.event, event); | ||
test.equal(a.d, "hello"); | ||
test.equal(a.that, div.node()); | ||
|
||
a = {}; | ||
zoom.on("start", null); | ||
test.deepEqual(a.event, undefined); | ||
test.equal(a.d, undefined); | ||
test.equal(a.that, undefined); | ||
|
||
test.end(); | ||
}); | ||
|
||
tape("zoom.on('end') callback", function(test) { | ||
let a; | ||
zoom.on("end", function(event, d) { a = {event, d, that: this}; }); | ||
div.call(zoom.transform, identity); | ||
const event = { type: "end", sourceEvent: null, target: zoom, transform: {k: 1, x: 0, y: 0}}; | ||
test.deepEqual(a.event, event); | ||
test.equal(a.d, "hello"); | ||
test.equal(a.that, div.node()); | ||
|
||
a = {}; | ||
zoom.on("end", null); | ||
test.deepEqual(a.event, undefined); | ||
test.equal(a.d, undefined); | ||
test.equal(a.that, undefined); | ||
|
||
test.end(); | ||
}); | ||
|
||
tape("zoom.on('start zoom end') callback order", function(test) { | ||
let a = []; | ||
zoom.on("start zoom end", function(event) { a.push(event.type); }); | ||
div.call(zoom.transform, identity); | ||
test.deepEqual(a, ["start", "zoom", "end"]); | ||
zoom.on("start zoom end", null); | ||
test.end(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
const tape = require("tape"), | ||
jsdom = require("./jsdom"), | ||
d3 = Object.assign(require("../"), require("d3-selection"), require("d3-transition")); | ||
|
||
// temporary fix (while d3-transition still requests d3-selection@1) | ||
d3.selection.prototype.interrupt = function(name) { | ||
return this.each(function() { | ||
d3.interrupt(this, name); | ||
}); | ||
}; | ||
|
||
// d3-zoom expects global navigator and SVGElement to exist | ||
global.navigator = {}; | ||
global.SVGElement = function(){}; | ||
|
||
const document = jsdom("<body>"), | ||
div = d3.select(document.body).append("div").datum("hello"), | ||
zoom = d3.zoom(), | ||
identity = d3.zoomIdentity; | ||
|
||
div.call(zoom); | ||
|
||
tape("zoom.filter receives (event, d) and filters", function(test) { | ||
div.call(zoom.transform, identity); | ||
const filter = zoom.filter(), | ||
event = { bubbles: true, cancelable: true, detail: { type: "fake" } }; | ||
let a, b; | ||
zoom | ||
.on("zoom", function() { b = arguments; }) | ||
.filter(function() { a = arguments; }); | ||
div.dispatch("dblclick", event); | ||
test.equal(a[0].detail.type, "fake"); | ||
test.equal(a[1], "hello"); | ||
test.equal(b, undefined); // our fake dblclick was rejected | ||
|
||
// temporary: avoid a crash due to starting a transition | ||
zoom.duration(0); | ||
zoom.filter(function() { return true; }); | ||
div.dispatch("dblclick", event); | ||
test.notEqual(b, undefined); // our fake dblclick was accepted | ||
|
||
zoom.filter(filter); | ||
zoom.on("zoom", null); | ||
test.end(); | ||
}); | ||
|
||
tape("zoom.extent receives (d)", function(test) { | ||
div.call(zoom.transform, identity); | ||
const extent = zoom.extent(), | ||
event = { bubbles: true, cancelable: true, detail: { type: "fake" } }; | ||
let a, b; | ||
zoom.extent(function() { | ||
a = arguments; a[-1]= this; return extent.apply(this, arguments); } | ||
}); | ||
div.dispatch("dblclick", event); | ||
test.equal(a[0], "hello") | ||
test.equal(a[-1], div.node()); | ||
zoom.extent(extent); | ||
test.end(); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
const tape = require("tape"), | ||
jsdom = require("./jsdom"), | ||
d3 = Object.assign(require("../"), require("d3-selection"), require("d3-transition")); | ||
|
||
// d3-zoom expects global navigator and SVGElement to exist | ||
global.navigator = {}; | ||
global.SVGElement = function(){}; | ||
|
||
const document = jsdom("<body>"), | ||
div = d3.select(document.body).append("div").datum("hello"), | ||
zoom = d3.zoom(), | ||
identity = d3.zoomIdentity; | ||
|
||
div.call(zoom); | ||
|
||
tape("d3.zoom initiates a zooming behavior", function(test) { | ||
div.call(zoom.transform, identity); | ||
test.deepEqual(div.node().__zoom, { k: 1, x: 0, y: 0 }); | ||
div.call(zoom.transform, d3.zoomIdentity.scale(2).translate(1,-3)); | ||
test.deepEqual(div.node().__zoom, { k: 2, x: 2, y: -6 }); | ||
test.end(); | ||
}); | ||
|
||
tape("zoomTransform returns the node’s current transform", function(test) { | ||
div.call(zoom.transform, identity); | ||
test.deepEqual(d3.zoomTransform(div.node()), { k: 1, x: 0, y: 0 }); | ||
div.call(zoom.translateBy, 10, 10); | ||
test.deepEqual(d3.zoomTransform(div.node()), { k: 1, x: 10, y: 10 }); | ||
|
||
// or an ancestor's… | ||
test.deepEqual(d3.zoomTransform(div.append("span").node()), { k: 1, x: 10, y: 10 }); | ||
|
||
// or zoomIdentity | ||
test.deepEqual(d3.zoomTransform(document.body), d3.zoomIdentity); | ||
|
||
div.html(""); | ||
test.end(); | ||
}); | ||
|
||
tape("zoom.scaleBy zooms", function(test) { | ||
div.call(zoom.transform, identity); | ||
div.call(zoom.scaleBy, 2, [0, 0]); | ||
test.deepEqual(div.node().__zoom, { k: 2, x: 0, y: 0 }); | ||
div.call(zoom.scaleBy, 2, [2, -2]); | ||
test.deepEqual(div.node().__zoom, { k: 4, x: -2, y: 2 }); | ||
div.call(zoom.scaleBy, 1/4, [2, -2]); | ||
test.deepEqual(div.node().__zoom, { k: 1, x: 1, y: -1 }); | ||
test.end(); | ||
}); | ||
|
||
tape("zoom.scaleTo zooms", function(test) { | ||
div.call(zoom.transform, identity); | ||
div.call(zoom.scaleTo, 2); | ||
test.deepEqual(div.node().__zoom, { k: 2, x: 0, y: 0 }); | ||
div.call(zoom.scaleTo, 2); | ||
test.deepEqual(div.node().__zoom, { k: 2, x: 0, y: 0 }); | ||
div.call(zoom.scaleTo, 1); | ||
test.deepEqual(div.node().__zoom, { k: 1, x: 0, y: 0 }); | ||
test.end(); | ||
}); | ||
|
||
tape("zoom.translateBy translates", function(test) { | ||
div.call(zoom.transform, identity); | ||
div.call(zoom.translateBy, 10, 10); | ||
test.deepEqual(div.node().__zoom, { k: 1, x: 10, y: 10 }); | ||
div.call(zoom.scaleBy, 2); | ||
div.call(zoom.translateBy, -10, -10); | ||
test.deepEqual(div.node().__zoom, { k: 2, x: 0, y: 0 }); | ||
test.end(); | ||
}); | ||
|
||
tape("zoom.scaleBy arguments can be functions passed (datum, index)", function(test) { | ||
div.call(zoom.transform, identity); | ||
let a, b, c, d; | ||
div.call(zoom.scaleBy, | ||
function() { a = arguments; b = this; return 2; }, | ||
function() { c = arguments; d = this; return [0, 0]; } | ||
); | ||
test.deepEqual(div.node().__zoom, { k: 2, x: 0, y: 0 }); | ||
test.deepEqual(a[0], "hello"); | ||
test.deepEqual(a[1], 0); | ||
test.deepEqual(b, div.node()); | ||
test.deepEqual(c[0], "hello"); | ||
test.deepEqual(c[1], 0); | ||
test.deepEqual(d, div.node()); | ||
test.end(); | ||
}); | ||
|
||
tape("zoom.scaleTo arguments can be functions passed (datum, index)", function(test) { | ||
div.call(zoom.transform, identity); | ||
let a, b, c, d; | ||
div.call(zoom.scaleTo, | ||
function() { a = arguments; b = this; return 2; }, | ||
function() { c = arguments; d = this; return [0, 0]; } | ||
); | ||
test.deepEqual(div.node().__zoom, { k: 2, x: 0, y: 0 }); | ||
test.deepEqual(a[0], "hello"); | ||
test.deepEqual(a[1], 0); | ||
test.deepEqual(b, div.node()); | ||
test.deepEqual(c[0], "hello"); | ||
test.deepEqual(c[1], 0); | ||
test.deepEqual(d, div.node()); | ||
test.end(); | ||
}); | ||
|
||
tape("zoom.translateBy arguments can be functions passed (datum, index)", function(test) { | ||
div.call(zoom.transform, identity); | ||
let a, b, c, d; | ||
div.call(zoom.translateBy, | ||
function() { a = arguments; b = this; return 2; }, | ||
function() { c = arguments; d = this; return 3; } | ||
); | ||
test.deepEqual(div.node().__zoom, { k: 1, x: 2, y: 3 }); | ||
test.deepEqual(a[0], "hello"); | ||
test.deepEqual(a[1], 0); | ||
test.deepEqual(b, div.node()); | ||
test.deepEqual(c[0], "hello"); | ||
test.deepEqual(c[1], 0); | ||
test.deepEqual(d, div.node()); | ||
test.end(); | ||
}); | ||
|
||
|
||
tape("zoom.constrain receives (transform, extent, translateExtent)", function(test) { | ||
div.call(zoom.transform, identity); | ||
const constrain = zoom.constrain(); | ||
let a, b; | ||
zoom.constrain(function() { | ||
a = arguments; | ||
return b = constrain.apply(this, arguments); | ||
}); | ||
div.call(zoom.translateBy, 10, 10); | ||
test.deepEqual(a[0], b); | ||
test.deepEqual(a[0], { k: 1, x: 10, y: 10 }); | ||
test.deepEqual(a[1], [ [ 0, 0 ], [ 0, 0 ] ]); | ||
test.equal(a[2][0][0], -Infinity); | ||
zoom.constrain(constrain); | ||
test.end(); | ||
}); |
Oops, something went wrong.