1
YUI.add('event-valuechange-test', function (Y) {
4
ArrayAssert = Y.ArrayAssert,
6
ignoreFocus = Y.UA.ie && Y.UA.ie < 10,
7
suite = new Y.Test.Suite('Y.ValueChange');
9
// -- Basic Subscriptions ------------------------------------------------------
10
suite.add(new Y.Test.Case({
15
// IE doesn't simulate focus/blur events properly, so these tests
16
// fail. Have to rely on manual testing.
17
'valuechange should stop polling on blur': ignoreFocus,
18
'valuechange should start polling on focus': ignoreFocus,
19
'valuechange should not report stale changes that occurred while a node was not focused': ignoreFocus
24
this.textArea = Y.Node.create('<textarea></textarea>');
25
this.textInput = Y.Node.create('<input type="text">');
27
Y.one('#test').append(this.textArea).append(this.textInput);
30
tearDown: function () {
31
this.textArea.remove().destroy(true);
32
this.textInput.remove().destroy(true);
35
'valuechange event should start polling on mousedown and fire an event when the value changes': function () {
38
this.textInput.once('valuechange', function (e) {
39
test.resume(function () {
40
Assert.areSame('', e.prevVal, 'prevVal should be ""');
41
Assert.areSame('foo', e.newVal, 'newVal should be "foo"');
42
Assert.areSame(test.textInput, e.currentTarget, 'currentTarget should be the input node');
43
Assert.areSame(test.textInput, e.target, 'target should be the input node');
47
this.textInput.simulate('mousedown');
48
this.textInput.set('value', 'foo');
53
'valuechange should support textareas as well': function () {
56
this.textArea.once('valuechange', function (e) {
57
test.resume(function () {
58
Assert.areSame('', e.prevVal, 'prevVal should be ""');
59
Assert.areSame('foo', e.newVal, 'newVal should be "foo"');
60
Assert.areSame(test.textArea, e.currentTarget, 'currentTarget should be the textarea node');
61
Assert.areSame(test.textArea, e.target, 'target should be the textarea node');
65
this.textArea.simulate('mousedown');
66
this.textArea.set('value', 'foo');
71
'valuechange should start polling on keydown': function () {
74
this.textInput.once('valuechange', function (e) {
78
this.textInput.simulate('keydown');
79
this.textInput.set('value', 'foo');
84
'valuechange should stop polling on blur': function () {
87
this.textInput.on('valuechange', function (e) {
91
this.textInput.simulate('mousedown');
92
this.textInput.set('value', 'foo');
94
this.wait(function () {
98
this.textInput.simulate('blur');
99
this.textInput.set('value', 'bar');
101
this.wait(function () {
102
Assert.isFalse(fired);
107
'valuechange should start polling on focus': function () {
110
this.textInput.once('valuechange', function (e) {
114
this.textInput.simulate('focus');
115
this.textInput.set('value', 'foo');
120
'valuechange should not report stale changes that occurred while a node was not focused': function () {
123
this.textInput.simulate('mousedown');
124
this.textInput.set('value', 'foo');
126
this.textInput.on('valuechange', function (e) {
130
this.textInput.simulate('blur');
131
this.textInput.set('value', 'bar');
132
this.textInput.simulate('focus');
133
this.textInput.simulate('mousedown');
135
this.wait(function () {
136
Assert.isFalse(fired);
140
'valuechange should start polling on keyup for IME keystrokes': function () {
143
this.textInput.on('valuechange', function (e) {
147
this.textInput.simulate('keyup', {keyCode: 123}); // should not trigger IME behavior
148
this.textInput.set('value', 'foo');
150
this.wait(function () {
151
Assert.isFalse(fired);
153
this.textInput.simulate('blur'); // stop polling
154
this.textInput.simulate('keyup', {keyCode: 197});
155
this.textInput.set('value', 'bar');
157
this.wait(function () {
158
Assert.isTrue(fired);
162
this.textInput.simulate('blur');
163
this.textInput.simulate('keyup', {keyCode: 229});
164
this.textInput.set('value', 'baz');
166
this.wait(function () {
167
Assert.isTrue(fired);
173
'valuechange should stop polling after timeout': function () {
174
var oldTimeout = Y.ValueChange.TIMEOUT,
177
this.textInput.on('valuechange', function (e) {
181
Y.ValueChange.TIMEOUT = 70;
183
this.textInput.simulate('mousedown');
184
this.textInput.set('value', 'foo');
186
this.wait(function () {
187
Assert.isTrue(fired);
190
this.wait(function () {
191
this.textInput.set('value', 'bar');
193
this.wait(function () {
194
Assert.isFalse(fired);
195
Y.ValueChange.TIMEOUT = oldTimeout;
201
'valueChange should be an alias for valuechange for backcompat': function () {
204
this.textInput.on('valueChange', function () {
208
this.textInput.simulate('mousedown');
209
this.textInput.set('value', 'monkeys');
215
// -- Delegation ---------------------------------------------------------------
217
suite.add(new Y.Test.Case({
221
this.container = Y.one('#test')
222
.append('<input type="text" id="vc-delegate-a" class="odd">')
223
.append('<input type="text" id="vc-delegate-b" class="even">')
224
.append('<input type="text" id="vc-delegate-c" class="odd">')
225
.append('<textarea id="vc-delegate-d" class="even"></textarea>')
226
.append('<textarea id="vc-delegate-e" class="odd"></textarea>')
227
.append('<textarea id="vc-delegate-f" class="even"></textarea>');
229
this.a = Y.one('#vc-delegate-a');
230
this.b = Y.one('#vc-delegate-b');
231
this.c = Y.one('#vc-delegate-c');
232
this.d = Y.one('#vc-delegate-d');
233
this.e = Y.one('#vc-delegate-e');
234
this.f = Y.one('#vc-delegate-f');
237
tearDown: function () {
238
this.container.purge().empty();
241
'delegation should be supported on input nodes': function () {
244
this.container.delegate('valuechange', function (e) {
245
test.resume(function () {
246
Assert.areSame('', e.prevVal, 'prevVal should be ""');
247
Assert.areSame('foo', e.newVal, 'newVal should be "foo"');
248
Assert.areSame('input', e.currentTarget.get('nodeName').toLowerCase(), 'currentTarget should be the input node');
249
Assert.areSame('input', e.target.get('nodeName').toLowerCase(), 'target should be the input node');
253
this.a.simulate('mousedown');
254
this.a.set('value', 'foo');
259
'delegation should be supported on textareas': function () {
262
this.container.delegate('valuechange', function (e) {
263
test.resume(function () {
264
Assert.areSame('', e.prevVal, 'prevVal should be ""');
265
Assert.areSame('foo', e.newVal, 'newVal should be "foo"');
266
Assert.areSame('textarea', e.currentTarget.get('nodeName').toLowerCase(), 'currentTarget should be the textarea node');
267
Assert.areSame('textarea', e.target.get('nodeName').toLowerCase(), 'target should be the textarea node');
271
this.f.simulate('mousedown');
272
this.f.set('value', 'foo');
277
'delegate filters should work properly': function () {
280
this.container.delegate('valuechange', function (e) {
284
this.container.delegate('valuechange', function (e) {
285
test.resume(function () {
286
Assert.fail('.odd handler should not be called');
290
this.b.simulate('mousedown');
291
this.b.set('value', 'foo');
296
'multiple delegated handlers should be supported': function () {
300
this.container.delegate('valuechange', function (e) {
304
this.container.delegate('valuechange', function (e) {
305
test.resume(function () {
306
Assert.fail('.even handler should not be called');
310
this.container.delegate('valuechange', function (e) {
314
this.container.delegate('valuechange', function (e) {
318
this.c.simulate('mousedown');
319
this.c.set('value', 'foo');
321
this.wait(function () {
322
ArrayAssert.itemsAreSame(['one', 'two', 'three'], calls, 'delegated handlers should all be called in the correct order');
327
Y.Test.Runner.add(suite);
329
}, '@VERSION@', {requires:['event-valuechange', 'node-base', 'node-event-simulate', 'test']});