forked from d3/d3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathon-test.js
More file actions
135 lines (132 loc) · 5.63 KB
/
Copy pathon-test.js
File metadata and controls
135 lines (132 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
var vows = require("vows"),
load = require("../load"),
assert = require("../assert");
var suite = vows.describe("selection.on");
suite.addBatch({
"select(body)": {
topic: load("selection/on").document(),
"on a simple page": {
topic: function(d3) {
return d3.select("body");
},
"registers an event listener for the specified type": function(body) {
var form = body.append("form"), count = 0;
form.on("submit", function() { ++count; }); // jsdom has spotty event support
form.append("input").attr("type", "submit").node().click();
assert.equal(count, 1);
},
"replaces an existing event listener for the same type": function(body) {
var form = body.append("form"), count = 0, fail = 0;
form.on("submit", function() { ++fail; });
form.on("submit", function() { ++count; });
form.append("input").attr("type", "submit").node().click();
assert.equal(count, 1);
assert.equal(fail, 0);
},
"removes an existing event listener": function(body) {
var form = body.append("form"), fail = 0;
form.on("submit", function() { ++fail; });
form.on("submit", null);
form.append("input").attr("type", "submit").node().click();
assert.equal(fail, 0);
assert.isUndefined(form.on("submit"));
},
/* Regrettably, JSDOM ignores the capture flag, so we can't test this yet…
"removing a listener doesn't require the capture flag": function(body) {
var form = body.append("form"), fail = 0;
form.on("submit", function() { ++fail; }, true);
form.on("submit", null);
form.append("input").attr("type", "submit").node().click();
assert.equal(fail, 0);
assert.isUndefined(form.on("submit"));
},
*/
"ignores removal of non-matching event listener": function(body) {
body.append("form").on("submit", null);
},
"observes the specified namespace": function(body) {
var form = body.append("form"), foo = 0, bar = 0;
form.on("submit.foo", function() { ++foo; });
form.on({"submit.bar": function() { ++bar; }});
form.append("input").attr("type", "submit").node().click();
assert.equal(foo, 1);
assert.equal(bar, 1);
},
"can register listeners as a map": function(body) {
var form = body.append("form"), count = 0, fail = 0;
form.on({submit: function() { ++fail; }});
form.on({submit: function() { ++count; }});
form.append("input").attr("type", "submit").node().click();
assert.equal(count, 1);
assert.equal(fail, 0);
form.on({submit: null});
assert.isUndefined(form.on("submit"));
},
/* Not really sure how to test this one…
"observes the specified capture flag": function(body) {
},
*/
"passes the current data and index to the event listener": function(body) {
var forms = body.html("").selectAll("form").data(["a", "b"]).enter().append("form"), dd, ii, data = new Object();
forms.on("submit", function(d, i) { dd = d; ii = i; });
forms.append("input").attr("type", "submit")[0][1].click();
assert.equal(dd, "b");
assert.equal(ii, 1);
forms[0][1].__data__ = data;
forms.append("input").attr("type", "submit")[0][1].click();
assert.equal(dd, data);
assert.equal(ii, 1);
},
"uses the current element as the context": function(body) {
var forms = body.html("").selectAll("form").data(["a", "b"]).enter().append("form"), context;
forms.on("submit", function() { context = this; });
forms.append("input").attr("type", "submit")[0][1].click();
assert.domEqual(context, forms[0][1]);
},
"returns the current selection": function(body) {
assert.isTrue(body.on("submit", function() {}) === body);
},
"returns the assigned listener if called with one argument": function(body) {
body.on("mouseover", f).on("click.foo", f);
function f() {}
assert.equal(body.on("mouseover"), f);
assert.equal(body.on("click.foo"), f);
assert.isUndefined(body.on("click"));
assert.isUndefined(body.on("mouseover.foo"));
},
"omitting the event type": {
"returns undefined when retrieving a listener": function(body) {
assert.isUndefined(body.on(".foo"));
},
"null removes all named event listeners": function(body) {
body.on("mouseover.foo", f)
.on("click.foo", f)
.on("click.foobar", f)
.on(".foo", null);
function f() {}
assert.isUndefined(body.on("mouseover.foo"));
assert.isUndefined(body.on("click.foo"));
assert.equal(body.on("click.foobar"), f);
},
"no-op when setting a listener": function(body) {
assert.isTrue(body.on(".foo", function() {}) === body);
assert.isUndefined(body.on(".foo"));
}
}
},
"sets the current event as d3.event": function(d3) {
var form = d3.select("body").append("form"), event;
form.on("submit", function() { event = d3.event; });
form.append("input").attr("type", "submit").node().click();
assert.equal(event.type, "submit");
assert.domEqual(event.target, form[0][0]);
},
"restores the original event after notifying listeners": function(d3) {
var form = d3.select("body").append("form"), event = d3.event = new Object();
form.on("submit", function() {});
form.append("input").attr("type", "submit").node().click();
assert.equal(d3.event, event);
}
}
});
suite.export(module);