toolkit/javascript/d3/test/core/dispatch-test.js
changeset 47 c0b4a8b5a012
equal deleted inserted replaced
46:efd9c589177a 47:c0b4a8b5a012
       
     1 require("../env");
       
     2 require("../../d3");
       
     3 
       
     4 var vows = require("vows"),
       
     5     assert = require("assert");
       
     6 
       
     7 var suite = vows.describe("d3.dispatch");
       
     8 
       
     9 suite.addBatch({
       
    10   "dispatch": {
       
    11     topic: function() {
       
    12       return d3.dispatch;
       
    13     },
       
    14     "returns a map of dispatchers for each event type": function(dispatch) {
       
    15       assert.deepEqual(dispatch(), {});
       
    16       var d = dispatch("foo");
       
    17       assert.isTrue("foo" in d);
       
    18       assert.isFalse("bar" in d);
       
    19       var d = dispatch("foo", "bar");
       
    20       assert.isTrue("foo" in d);
       
    21       assert.isTrue("bar" in d);
       
    22     },
       
    23     "added listeners receive subsequent events": function(dispatch) {
       
    24       var d = dispatch("foo"), events = 0;
       
    25       d.on("foo", function() { ++events; });
       
    26       d.foo();
       
    27       assert.equal(events, 1);
       
    28       d.foo();
       
    29       d.foo();
       
    30       assert.equal(events, 3);
       
    31     },
       
    32     "the listener is passed any arguments to dispatch": function(dispatch) {
       
    33       var d = dispatch("foo"), a = {}, b = {}, aa, bb;
       
    34       d.on("foo", function(a, b) { aa = a; bb = b; });
       
    35       d.foo(a, b);
       
    36       assert.equal(aa, a);
       
    37       assert.equal(bb, b);
       
    38       d.foo(1, "foo");
       
    39       assert.equal(aa, 1);
       
    40       assert.equal(bb, "foo");
       
    41     },
       
    42     "the listener's context is the same as dispatch's": function(dispatch) {
       
    43       var d = dispatch("foo"), a = {}, b = {}, that;
       
    44       d.on("foo", function() { that = this; });
       
    45       d.foo.call(a);
       
    46       assert.equal(that, a);
       
    47       d.foo.call(b);
       
    48       assert.equal(that, b);
       
    49     },
       
    50     "listeners are notified in the order they are added": function(dispatch) {
       
    51       var d = dispatch("foo"), a = {}, b = {}, those = [];
       
    52       function A() { those.push(a); }
       
    53       function B() { those.push(b); }
       
    54       d.on("foo.a", A);
       
    55       d.on("foo.b", B);
       
    56       d.foo();
       
    57       assert.deepEqual(those, [a, b]);
       
    58       those = [];
       
    59       d.on("foo.a", A); // move to the end
       
    60       d.foo();
       
    61       assert.deepEqual(those, [b, a]);
       
    62     },
       
    63     "removed listeners do not receive subsequent events": function(dispatch) {
       
    64       var d = dispatch("foo"), a = {}, b = {}, those = [];
       
    65       function A() { those.push(a); }
       
    66       function B() { those.push(b); }
       
    67       d.on("foo.a", A);
       
    68       d.on("foo.b", B);
       
    69       d.foo();
       
    70       those = [];
       
    71       d.on("foo.a", null);
       
    72       d.foo();
       
    73       assert.deepEqual(those, [b]);
       
    74     },
       
    75     "adding an existing listener has no effect": function(dispatch) {
       
    76       var d = dispatch("foo"), events = 0;
       
    77       function A() { ++events; }
       
    78       d.on("foo.a", A);
       
    79       d.foo();
       
    80       d.on("foo.a", A);
       
    81       d.on("foo.a", A);
       
    82       d.foo();
       
    83       assert.equal(events, 2);
       
    84     },
       
    85     "removing a missing listener has no effect": function(dispatch) {
       
    86       var d = dispatch("foo"), events = 0;
       
    87       function A() { ++events; }
       
    88       d.on("foo.a", null);
       
    89       d.on("foo", A);
       
    90       d.on("foo", null);
       
    91       d.on("foo", null);
       
    92       d.foo();
       
    93       assert.equal(events, 0);
       
    94     },
       
    95     "adding a listener does not affect the current event": function(dispatch) {
       
    96       var d = dispatch("foo"), a = {}, b = {}, those = [];
       
    97       function A() { d.on("foo.b", B); those.push(a); }
       
    98       function B() { those.push(b); }
       
    99       d.on("foo.a", A);
       
   100       d.foo();
       
   101       assert.deepEqual(those, [a]);
       
   102     },
       
   103     "removing a listener does affect the current event": function(dispatch) {
       
   104       var d = dispatch("foo"), a = {}, b = {}, those = [];
       
   105       function A() { d.on("foo.b", null); those.push(a); }
       
   106       function B() { those.push(b); }
       
   107       d.on("foo.a", A);
       
   108       d.on("foo.b", B);
       
   109       d.foo();
       
   110       assert.deepEqual(those, [a]);
       
   111     }
       
   112   }
       
   113 });
       
   114 
       
   115 suite.export(module);