Skip to content

Commit

Permalink
Fix transition.on(typename, null).
Browse files Browse the repository at this point in the history
Also, tests for transition.remove. #20
  • Loading branch information
mbostock committed Mar 3, 2016
1 parent 516bb56 commit 79c8c0c
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 10 deletions.
1 change: 0 additions & 1 deletion src/transition/on.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ function start(name) {
}

function onFunction(id, name, listener) {
if (typeof listener !== "function") throw new Error;
var on0, on1, sit = start(name) ? init : set;
return function() {
var schedule = sit(this, id),
Expand Down
2 changes: 1 addition & 1 deletion test/transition/attrTween-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ tape("transition.attrTween(name, value) coerces the specified name to a string",
tape("transition.attrTween(name, value) throws an error if value is not null and not a function", function(test) {
var root = jsdom.jsdom().documentElement,
transition = d3_selection.select(root).transition();
test.throws(function() { transition.attrTween("foo", 42); }, /Error/);
test.throws(function() { transition.attrTween("foo", 42); });
test.end();
});

Expand Down
4 changes: 2 additions & 2 deletions test/transition/ease-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ tape("transition.ease() returns the easing function for the first non-null node"
tape("transition.ease(ease) throws an error if ease is not a function", function(test) {
var root = jsdom.jsdom().documentElement,
transition = d3_selection.select(root).transition();
test.throws(function() { transition.ease(42); }, /Error/);
test.throws(function() { transition.ease(null); }, /Error/);
test.throws(function() { transition.ease(42); });
test.throws(function() { transition.ease(null); });
test.end();
});

Expand Down
2 changes: 1 addition & 1 deletion test/transition/merge-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ tape("transition.merge(other) throws an error if the other transition has a diff
two = document.querySelector("#two"),
transition1 = d3_selection.selectAll([null, two]).transition(),
transition2 = d3_selection.selectAll([one, null]).transition();
test.throws(function() { transition1.merge(transition2); }, /Error/);
test.throws(function() { transition1.merge(transition2); });
test.end();
});
29 changes: 26 additions & 3 deletions test/transition/on-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require("../../");
tape("transition.on(type, listener) throws an error if listener is not a function", function(test) {
var root = jsdom.jsdom().documentElement,
transition = d3_selection.select(root).transition();
test.throws(function() { transition.on("start", 42); }, /Error/);
test.throws(function() { transition.on("start", 42); });
test.end();
});

Expand All @@ -27,17 +27,40 @@ tape("transition.on(typename) returns the listener with the specified typename,
tape("transition.on(typename) throws an error if the specified type is not supported", function(test) {
var root = jsdom.jsdom().documentElement,
transition = d3_selection.select(root).transition();
test.throws(function() { transition.on("foo"); }, /Error/);
test.throws(function() { transition.on("foo"); });
test.end();
});

tape("transition.on(typename, listener) throws an error if the specified type is not supported", function(test) {
var root = jsdom.jsdom().documentElement,
transition = d3_selection.select(root).transition();
test.throws(function() { transition.on("foo", function() {}); }, /Error/);
test.throws(function() { transition.on("foo", function() {}); });
test.end();
});

tape("transition.on(typename, listener) throws an error if the specified listener is not a function", function(test) {
var root = jsdom.jsdom().documentElement,
transition = d3_selection.select(root).transition();
test.throws(function() { transition.on("foo", 42); });
test.end();
});

tape("transition.on(typename, null) removes the listener with the specified typename, if any", function(test) {
var root = jsdom.jsdom().documentElement,
starts = 0,
transition = d3_selection.select(root).transition().on("start.foo", function() { ++starts; }),
schedule = root.__transition[transition._id];

test.equal(transition.on("start.foo", null), transition);
test.equal(transition.on("start.foo"), undefined);
test.equal(schedule.on.on("start.foo"), undefined);

d3_timer.timeout(function() {
test.equal(starts, 0);
test.end();
});
});

tape("transition.on(\"start\", listener) registers a listener for the start event", function(test) {
var root = jsdom.jsdom().documentElement,
transition = d3_selection.select(root).transition().on("start", started),
Expand Down
49 changes: 49 additions & 0 deletions test/transition/remove-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
var tape = require("tape"),
jsdom = require("jsdom"),
d3_timer = require("d3-timer"),
d3_selection = require("d3-selection");

require("../../");

tape("transition.remove() creates an end listener to remove the element", function(test) {
var document = jsdom.jsdom(),
root = document.documentElement,
body = document.body,
selection = d3_selection.select(body),
transition = selection.transition().remove().on("start", started).on("end", ended);

function started() {
test.equal(body.parentNode, root);
}

function ended() {
test.equal(body.parentNode, null);
test.end();
}

d3_timer.timeout(function(elapsed) {
test.equal(body.parentNode, root);
});
});

tape("transition.remove() creates an end listener named end.remove", function(test) {
var document = jsdom.jsdom(),
root = document.documentElement,
body = document.body,
selection = d3_selection.select(body),
transition = selection.transition().remove().on("start", started).on("end", ended);

transition.on("end.remove").call(body);
test.equal(body.parentNode, null);
transition.on("end.remove", null);
root.appendChild(body);

function started() {
test.equal(body.parentNode, root);
}

function ended() {
test.equal(body.parentNode, root);
test.end();
}
});
2 changes: 1 addition & 1 deletion test/transition/styleTween-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ tape("transition.styleTween(name, value) coerces the specified name to a string"
tape("transition.styleTween(name, value) throws an error if value is not null and not a function", function(test) {
var root = jsdom.jsdom().documentElement,
transition = d3_selection.select(root).transition();
test.throws(function() { transition.styleTween("color", 42); }, /Error/);
test.throws(function() { transition.styleTween("color", 42); });
test.end();
});

Expand Down
2 changes: 1 addition & 1 deletion test/transition/tween-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ tape("transition.tween(name) coerces the specified name to a string", function(t
tape("transition.tween(name, value) throws an error if value is not null and not a function", function(test) {
var root = jsdom.jsdom().documentElement,
transition = d3_selection.select(root).transition();
test.throws(function() { transition.tween("foo", 42); }, /Error/);
test.throws(function() { transition.tween("foo", 42); });
test.end();
});

Expand Down

0 comments on commit 79c8c0c

Please sign in to comment.