Commit af25181d authored by Mike Bostock's avatar Mike Bostock
Browse files

Refactor event tests for minimal loading.

parent 8e9db961
......@@ -6,18 +6,18 @@ var suite = vows.describe("d3.dispatch");
suite.addBatch({
"dispatch": {
topic: load("event/dispatch"),
"returns a map of dispatchers for each event type": function(d3) {
assert.deepEqual(d3.dispatch(), {});
var d = d3.dispatch("foo");
topic: load("event/dispatch").expression("d3.dispatch"),
"returns a map of dispatchers for each event type": function(dispatch) {
assert.deepEqual(dispatch(), {});
var d = dispatch("foo");
assert.isTrue("foo" in d);
assert.isFalse("bar" in d);
var d = d3.dispatch("foo", "bar");
var d = dispatch("foo", "bar");
assert.isTrue("foo" in d);
assert.isTrue("bar" in d);
},
"added listeners receive subsequent events": function(d3) {
var d = d3.dispatch("foo"), events = 0;
"added listeners receive subsequent events": function(dispatch) {
var d = dispatch("foo"), events = 0;
d.on("foo", function() { ++events; });
d.foo();
assert.equal(events, 1);
......@@ -25,8 +25,8 @@ suite.addBatch({
d.foo();
assert.equal(events, 3);
},
"the listener is passed any arguments to dispatch": function(d3) {
var d = d3.dispatch("foo"), a = {}, b = {}, aa, bb;
"the listener is passed any arguments to dispatch": function(dispatch) {
var d = dispatch("foo"), a = {}, b = {}, aa, bb;
d.on("foo", function(a, b) { aa = a; bb = b; });
d.foo(a, b);
assert.equal(aa, a);
......@@ -35,16 +35,16 @@ suite.addBatch({
assert.equal(aa, 1);
assert.equal(bb, "foo");
},
"the listener's context is the same as dispatch's": function(d3) {
var d = d3.dispatch("foo"), a = {}, b = {}, that;
"the listener's context is the same as dispatch's": function(dispatch) {
var d = dispatch("foo"), a = {}, b = {}, that;
d.on("foo", function() { that = this; });
d.foo.call(a);
assert.equal(that, a);
d.foo.call(b);
assert.equal(that, b);
},
"listeners are notified in the order they are added": function(d3) {
var d = d3.dispatch("foo"), a = {}, b = {}, those = [];
"listeners are notified in the order they are added": function(dispatch) {
var d = dispatch("foo"), a = {}, b = {}, those = [];
function A() { those.push(a); }
function B() { those.push(b); }
d.on("foo.a", A).on("foo.b", B);
......@@ -55,17 +55,17 @@ suite.addBatch({
d.foo();
assert.deepEqual(those, [b, a]);
},
"notifying listeners returns the dispatch object": function(d3) {
var d = d3.dispatch("foo");
"notifying listeners returns the dispatch object": function(dispatch) {
var d = dispatch("foo");
assert.equal(d.foo(), d);
},
"adding a listener returns the dispatch object": function(d3) {
var d = d3.dispatch("foo");
"adding a listener returns the dispatch object": function(dispatch) {
var d = dispatch("foo");
function A() {}
assert.equal(d.on("foo", A), d);
},
"removed listeners do not receive subsequent events": function(d3) {
var d = d3.dispatch("foo"), a = {}, b = {}, those = [];
"removed listeners do not receive subsequent events": function(dispatch) {
var d = dispatch("foo"), a = {}, b = {}, those = [];
function A() { those.push(a); }
function B() { those.push(b); }
d.on("foo.a", A).on("foo.b", B);
......@@ -75,8 +75,8 @@ suite.addBatch({
d.foo();
assert.deepEqual(those, [b]);
},
"removing a shared listener only affects the intended event": function(d3) {
var d = d3.dispatch("foo", "bar"), a = 0;
"removing a shared listener only affects the intended event": function(dispatch) {
var d = dispatch("foo", "bar"), a = 0;
function A() { ++a; }
d.on("foo", A).on("bar", A);
d.foo();
......@@ -86,8 +86,8 @@ suite.addBatch({
d.bar();
assert.equal(a, 3);
},
"adding an existing listener has no effect": function(d3) {
var d = d3.dispatch("foo"), events = 0;
"adding an existing listener has no effect": function(dispatch) {
var d = dispatch("foo"), events = 0;
function A() { ++events; }
d.on("foo.a", A);
d.foo();
......@@ -95,31 +95,31 @@ suite.addBatch({
d.foo();
assert.equal(events, 2);
},
"removing a missing listener has no effect": function(d3) {
var d = d3.dispatch("foo"), events = 0;
"removing a missing listener has no effect": function(dispatch) {
var d = dispatch("foo"), events = 0;
function A() { ++events; }
d.on("foo.a", null).on("foo", A).on("foo", null).on("foo", null);
d.foo();
assert.equal(events, 0);
},
"adding a listener does not affect the current event": function(d3) {
var d = d3.dispatch("foo"), a = {}, b = {}, those = [];
"adding a listener does not affect the current event": function(dispatch) {
var d = dispatch("foo"), a = {}, b = {}, those = [];
function A() { d.on("foo.b", B); those.push(a); }
function B() { those.push(b); }
d.on("foo.a", A);
d.foo();
assert.deepEqual(those, [a]);
},
"removing a listener does affect the current event": function(d3) {
var d = d3.dispatch("foo"), a = {}, b = {}, those = [];
"removing a listener does affect the current event": function(dispatch) {
var d = dispatch("foo"), a = {}, b = {}, those = [];
function A() { d.on("foo.b", null); those.push(a); }
function B() { those.push(b); }
d.on("foo.a", A).on("foo.b", B);
d.foo();
assert.deepEqual(those, [a]);
},
"getting a listener returns the correct listener": function(d3) {
var d = d3.dispatch("foo");
"getting a listener returns the correct listener": function(dispatch) {
var d = dispatch("foo");
function A() {}
function B() {}
function C() {}
......@@ -129,12 +129,12 @@ suite.addBatch({
assert.equal(d.on("foo"), C);
},
"omitting the event type": {
"returns undefined when retrieving a listener": function(d3) {
var d = d3.dispatch("foo");
"returns undefined when retrieving a listener": function(dispatch) {
var d = dispatch("foo");
assert.isUndefined(d.on(".a"));
},
"null removes all named event listeners": function(d3) {
var d = d3.dispatch("foo", "bar"), a = {}, b = {}, c = {}, those = [];
"null removes all named event listeners": function(dispatch) {
var d = dispatch("foo", "bar"), a = {}, b = {}, c = {}, those = [];
function A() { those.push(a); }
function B() { those.push(b); }
function C() { those.push(c); }
......@@ -143,8 +143,8 @@ suite.addBatch({
d.bar();
assert.deepEqual(those, [c]);
},
"no-op when setting a listener": function(d3) {
var d = d3.dispatch("foo", "bar"), a = {}, b = {}, those = [];
"no-op when setting a listener": function(dispatch) {
var d = dispatch("foo", "bar"), a = {}, b = {}, those = [];
function A() { those.push(a); }
function B() { those.push(b); }
d
......
......@@ -6,15 +6,12 @@ var suite = vows.describe("d3.timer");
suite.addBatch({
"timer": {
topic: load("event/timer").sandbox({
document: {},
window: {},
setTimeout: setTimeout,
clearTimeout: clearTimeout
}),
topic: load("event/timer")
.expression("d3.timer")
.sandbox({document: {}, window: {}, setTimeout: setTimeout, clearTimeout: clearTimeout}),
"with no delay": {
topic: timer(),
topic: delay(),
"first calls after 17 ms or less": function(info) {
assert.inDelta(info.start - info.scheduled, 17, 20);
},
......@@ -27,7 +24,7 @@ suite.addBatch({
},
"with a specified delay": {
topic: timer(250),
topic: delay(250),
"first calls after the delay": function(info) {
assert.inDelta(info.start - info.scheduled, 250, 20);
},
......@@ -41,9 +38,9 @@ suite.addBatch({
}
});
function timer(delay) {
function delay(delay) {
var args = Array.prototype.slice.call(arguments);
return function(d3) {
return function(timer) {
var cb = this.callback,
info = {scheduled: Date.now(), count: 0};
......@@ -58,7 +55,7 @@ function timer(delay) {
}
});
d3.timer.apply(this, args);
timer.apply(this, args);
};
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment