Skip to content

Commit

Permalink
Tests for transition.on. #20
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Mar 2, 2016
1 parent 557d7bb commit 92abf1c
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions test/transition/on-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var tape = require("tape"),
jsdom = require("jsdom"),
d3_timer = require("d3-timer"),
d3_selection = require("d3-selection");

require("../../");
Expand All @@ -10,3 +11,67 @@ tape("transition.on(type, listener) throws an error if listener is not a functio
test.throws(function() { transition.on("start", 42); }, /Error/);
test.end();
});

tape("transition.on(typename) returns the listener with the specified typename, if any", function(test) {
var root = jsdom.jsdom().documentElement,
foo = function() {},
bar = function() {},
transition = d3_selection.select(root).transition().on("start", foo).on("start.bar", bar);
test.equal(transition.on("start"), foo);
test.equal(transition.on("start.foo"), undefined);
test.equal(transition.on("start.bar"), bar);
test.equal(transition.on("end"), undefined);
test.end();
});

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.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.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),
schedule = root.__transition[transition._id];

function started() {
test.equal(schedule.state, 2); // STARTING
test.end();
}
});

tape("transition.on(\"interrupt\", listener) registers a listener for the interrupt event", function(test) {
var root = jsdom.jsdom().documentElement,
selection = d3_selection.select(root),
transition = selection.transition().on("interrupt", interrupted),
schedule = root.__transition[transition._id];

function interrupted() {
test.equal(schedule.state, 5); // ENDED
test.end();
}

d3_timer.timeout(function() {
selection.interrupt();
});
});

tape("transition.on(\"end\", listener) registers a listener for the end event", function(test) {
var root = jsdom.jsdom().documentElement,
transition = d3_selection.select(root).transition().duration(50).on("end", ended),
schedule = root.__transition[transition._id];

function ended() {
test.equal(schedule.state, 5); // ENDED
test.end();
}
});

0 comments on commit 92abf1c

Please sign in to comment.