Skip to content

Commit

Permalink
75 unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Fil committed May 26, 2020
1 parent d8edd39 commit 46eba7c
Show file tree
Hide file tree
Showing 6 changed files with 1,296 additions and 388 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
},
"devDependencies": {
"eslint": "6",
"jsdom": "^16.2.2",
"rollup": "1",
"rollup-plugin-terser": "5",
"tape": "4"
Expand Down
5 changes: 5 additions & 0 deletions test/jsdom.js
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;
};
85 changes: 85 additions & 0 deletions test/zoom-callback-test.js
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();
});
61 changes: 61 additions & 0 deletions test/zoom-events-test.js
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();
});

139 changes: 139 additions & 0 deletions test/zoom-test.js
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();
});
Loading

0 comments on commit 46eba7c

Please sign in to comment.