|
1 require("../env"); |
|
2 require("../../d3"); |
|
3 |
|
4 var vows = require("vows"), |
|
5 assert = require("assert"); |
|
6 |
|
7 var suite = vows.describe("selection.on"); |
|
8 |
|
9 suite.addBatch({ |
|
10 "select(body)": { |
|
11 topic: function() { |
|
12 return d3.select("body").html(""); |
|
13 }, |
|
14 "registers an event listener for the specified type": function(body) { |
|
15 var form = body.append("form"), count = 0; |
|
16 form.on("submit", function() { ++count; }); // jsdom has spotty event support |
|
17 form.append("input").attr("type", "submit").node().click(); |
|
18 assert.equal(count, 1); |
|
19 }, |
|
20 "replaces an existing event listener for the same type": function(body) { |
|
21 var form = body.append("form"), count = 0, fail = 0; |
|
22 form.on("submit", function() { ++fail; }); |
|
23 form.on("submit", function() { ++count; }); |
|
24 form.append("input").attr("type", "submit").node().click(); |
|
25 assert.equal(count, 1); |
|
26 assert.equal(fail, 0); |
|
27 }, |
|
28 "removes an existing event listener": function(body) { |
|
29 var form = body.append("form"), fail = 0; |
|
30 form.on("submit", function() { ++fail; }); |
|
31 form.on("submit", null); |
|
32 form.append("input").attr("type", "submit").node().click(); |
|
33 assert.equal(fail, 0); |
|
34 }, |
|
35 "ignores removal of non-matching event listener": function(body) { |
|
36 body.append("form").on("submit", null); |
|
37 }, |
|
38 "observes the specified namespace": function(body) { |
|
39 var form = body.append("form"), foo = 0, bar = 0; |
|
40 form.on("submit.foo", function() { ++foo; }); |
|
41 form.on("submit.bar", function() { ++bar; }); |
|
42 form.append("input").attr("type", "submit").node().click(); |
|
43 assert.equal(foo, 1); |
|
44 assert.equal(bar, 1); |
|
45 }, |
|
46 /* |
|
47 Not really sure how to test this one⦠|
|
48 "observes the specified capture flag": function(body) { |
|
49 }, |
|
50 */ |
|
51 "passes the current data and index to the event listener": function(body) { |
|
52 var forms = body.html("").selectAll("form").data(["a", "b"]).enter().append("form"), dd, ii, data = new Object(); |
|
53 forms.on("submit", function(d, i) { dd = d; ii = i; }); |
|
54 forms.append("input").attr("type", "submit")[0][1].click(); |
|
55 assert.equal(dd, "b"); |
|
56 assert.equal(ii, 1); |
|
57 forms[0][1].__data__ = data; |
|
58 forms.append("input").attr("type", "submit")[0][1].click(); |
|
59 assert.equal(dd, data); |
|
60 assert.equal(ii, 1); |
|
61 }, |
|
62 "uses the current element as the context": function(body) { |
|
63 var forms = body.html("").selectAll("form").data(["a", "b"]).enter().append("form"), context; |
|
64 forms.on("submit", function() { context = this; }); |
|
65 forms.append("input").attr("type", "submit")[0][1].click(); |
|
66 assert.domEqual(context, forms[0][1]); |
|
67 }, |
|
68 "sets the current event as d3.event": function(body) { |
|
69 var form = body.append("form"), event; |
|
70 form.on("submit", function() { event = d3.event; }); |
|
71 form.append("input").attr("type", "submit").node().click(); |
|
72 assert.equal(event.type, "submit"); |
|
73 assert.domEqual(event.target, form[0][0]); |
|
74 }, |
|
75 "restores the original event after notifying listeners": function(body) { |
|
76 var form = body.append("form"), event = d3.event = new Object(); |
|
77 form.on("submit", function() {}); |
|
78 form.append("input").attr("type", "submit").node().click(); |
|
79 assert.equal(d3.event, event); |
|
80 }, |
|
81 "returns the current selection": function(body) { |
|
82 assert.isTrue(body.on("submit", function() {}) === body); |
|
83 }, |
|
84 "returns the assigned listener if called with one argument": function(body) { |
|
85 body.on("mouseover", f).on("click.foo", f); |
|
86 function f() {} |
|
87 assert.equal(body.on("mouseover"), f); |
|
88 assert.equal(body.on("click.foo"), f); |
|
89 assert.isUndefined(body.on("click")); |
|
90 assert.isUndefined(body.on("mouseover.foo")); |
|
91 } |
|
92 } |
|
93 }); |
|
94 |
|
95 suite.export(module); |