-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
helpers_test.js
1028 lines (801 loc) · 28.5 KB
/
helpers_test.js
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import Route from 'ember-routing/system/route';
import Controller from 'ember-runtime/controllers/controller';
import run from 'ember-metal/run_loop';
import EmberObject from 'ember-runtime/system/object';
import RSVP from 'ember-runtime/ext/rsvp';
import jQuery from 'ember-views/system/jquery';
import Component from 'ember-templates/component';
import Test from 'ember-testing/test';
import 'ember-testing/helpers'; // ensure that the helpers are loaded
import 'ember-testing/initializers'; // ensure the initializer is setup
import setupForTesting from 'ember-testing/setup_for_testing';
import EmberRouter from 'ember-routing/system/router';
import EmberRoute from 'ember-routing/system/route';
import EmberApplication from 'ember-application/system/application';
import { compile } from 'ember-template-compiler/tests/utils/helpers';
import { setTemplates, set as setTemplate } from 'ember-templates/template_registry';
import {
pendingRequests,
incrementPendingRequests,
clearPendingRequests
} from 'ember-testing/test/pending_requests';
import {
setAdapter,
getAdapter
} from 'ember-testing/test/adapter';
import {
registerWaiter,
unregisterWaiter
} from 'ember-testing/test/waiters';
var App;
var originalAdapter = getAdapter();
function cleanup() {
// Teardown setupForTesting
setAdapter(originalAdapter);
run(function() {
jQuery(document).off('ajaxSend');
jQuery(document).off('ajaxComplete');
});
clearPendingRequests();
// Test.waiters = null;
// Other cleanup
if (App) {
run(App, App.destroy);
App.removeTestHelpers();
App = null;
}
setTemplates({});
}
function assertHelpers(application, helperContainer, expected) {
if (!helperContainer) { helperContainer = window; }
if (expected === undefined) { expected = true; }
function checkHelperPresent(helper, expected) {
var presentInHelperContainer = !!helperContainer[helper];
var presentInTestHelpers = !!application.testHelpers[helper];
ok(presentInHelperContainer === expected, 'Expected \'' + helper + '\' to be present in the helper container (defaults to window).');
ok(presentInTestHelpers === expected, 'Expected \'' + helper + '\' to be present in App.testHelpers.');
}
checkHelperPresent('visit', expected);
checkHelperPresent('click', expected);
checkHelperPresent('keyEvent', expected);
checkHelperPresent('fillIn', expected);
checkHelperPresent('wait', expected);
checkHelperPresent('triggerEvent', expected);
}
function assertNoHelpers(application, helperContainer) {
assertHelpers(application, helperContainer, false);
}
function currentRouteName(app) {
return app.testHelpers.currentRouteName();
}
function currentPath(app) {
return app.testHelpers.currentPath();
}
function currentURL(app) {
return app.testHelpers.currentURL();
}
function setupApp() {
run(function() {
App = EmberApplication.create();
App.setupForTesting();
App.injectTestHelpers();
});
}
QUnit.module('ember-testing: Helper setup', {
setup() { cleanup(); },
teardown() { cleanup(); }
});
function registerHelper() {
Test.registerHelper('LeakyMcLeakLeak', function(app) {
});
}
QUnit.test('Ember.Application#injectTestHelpers/#removeTestHelpers', function() {
App = run(EmberApplication, EmberApplication.create);
assertNoHelpers(App);
registerHelper();
App.injectTestHelpers();
assertHelpers(App);
ok(Test.Promise.prototype.LeakyMcLeakLeak, 'helper in question SHOULD be present');
App.removeTestHelpers();
assertNoHelpers(App);
equal(Test.Promise.prototype.LeakyMcLeakLeak, undefined, 'should NOT leak test promise extensions');
});
QUnit.test('Ember.Application#setupForTesting', function() {
run(function() {
App = EmberApplication.create();
App.setupForTesting();
});
equal(App.__container__.lookup('router:main').location, 'none');
});
QUnit.test('Ember.Application.setupForTesting sets the application to `testing`.', function() {
run(function() {
App = EmberApplication.create();
App.setupForTesting();
});
equal(App.testing, true, 'Application instance is set to testing.');
});
QUnit.test('Ember.Application.setupForTesting leaves the system in a deferred state.', function() {
run(function() {
App = EmberApplication.create();
App.setupForTesting();
});
equal(App._readinessDeferrals, 1, 'App is in deferred state after setupForTesting.');
});
QUnit.test('App.reset() after Application.setupForTesting leaves the system in a deferred state.', function() {
run(function() {
App = EmberApplication.create();
App.setupForTesting();
});
equal(App._readinessDeferrals, 1, 'App is in deferred state after setupForTesting.');
App.reset();
equal(App._readinessDeferrals, 1, 'App is in deferred state after setupForTesting.');
});
QUnit.test('Ember.Application#setupForTesting attaches ajax listeners', function() {
var documentEvents;
documentEvents = jQuery._data(document, 'events');
if (!documentEvents) {
documentEvents = {};
}
ok(documentEvents['ajaxSend'] === undefined, 'there are no ajaxSend listers setup prior to calling injectTestHelpers');
ok(documentEvents['ajaxComplete'] === undefined, 'there are no ajaxComplete listers setup prior to calling injectTestHelpers');
run(function() {
setupForTesting();
});
documentEvents = jQuery._data(document, 'events');
equal(documentEvents['ajaxSend'].length, 1, 'calling injectTestHelpers registers an ajaxSend handler');
equal(documentEvents['ajaxComplete'].length, 1, 'calling injectTestHelpers registers an ajaxComplete handler');
});
QUnit.test('Ember.Application#setupForTesting attaches ajax listeners only once', function() {
var documentEvents;
documentEvents = jQuery._data(document, 'events');
if (!documentEvents) {
documentEvents = {};
}
ok(documentEvents['ajaxSend'] === undefined, 'there are no ajaxSend listeners setup prior to calling injectTestHelpers');
ok(documentEvents['ajaxComplete'] === undefined, 'there are no ajaxComplete listeners setup prior to calling injectTestHelpers');
run(function() {
setupForTesting();
});
run(function() {
setupForTesting();
});
documentEvents = jQuery._data(document, 'events');
equal(documentEvents['ajaxSend'].length, 1, 'calling injectTestHelpers registers an ajaxSend handler');
equal(documentEvents['ajaxComplete'].length, 1, 'calling injectTestHelpers registers an ajaxComplete handler');
});
QUnit.test('Ember.Application#injectTestHelpers calls callbacks registered with onInjectHelpers', function() {
var injected = 0;
Test.onInjectHelpers(function() {
injected++;
});
run(function() {
App = EmberApplication.create();
App.setupForTesting();
});
equal(injected, 0, 'onInjectHelpers are not called before injectTestHelpers');
App.injectTestHelpers();
equal(injected, 1, 'onInjectHelpers are called after injectTestHelpers');
});
QUnit.test('Ember.Application#injectTestHelpers adds helpers to provided object.', function() {
var helpers = {};
run(function() {
App = EmberApplication.create();
App.setupForTesting();
});
App.injectTestHelpers(helpers);
assertHelpers(App, helpers);
App.removeTestHelpers();
assertNoHelpers(App, helpers);
});
QUnit.test('Ember.Application#removeTestHelpers resets the helperContainer\'s original values', function() {
var helpers = { visit: 'snazzleflabber' };
run(function() {
App = EmberApplication.create();
App.setupForTesting();
});
App.injectTestHelpers(helpers);
ok(helpers.visit !== 'snazzleflabber', 'helper added to container');
App.removeTestHelpers();
ok(helpers.visit === 'snazzleflabber', 'original value added back to container');
});
QUnit.module('ember-testing: Helper methods', {
setup() {
setupApp();
},
teardown() {
cleanup();
}
});
QUnit.test('`wait` respects registerWaiters', function(assert) {
assert.expect(3);
let done = assert.async();
let counter = 0;
function waiter() {
return ++counter > 2;
}
let other = 0;
function otherWaiter() {
return ++other > 2;
}
run(App, App.advanceReadiness);
registerWaiter(waiter);
registerWaiter(otherWaiter);
App.testHelpers.wait()
.then(function() {
equal(waiter(), true, 'should not resolve until our waiter is ready');
unregisterWaiter(waiter);
counter = 0;
return App.testHelpers.wait();
})
.then(function() {
equal(counter, 0, 'unregistered waiter was not checked');
equal(otherWaiter(), true, 'other waiter is still registered');
})
.finally(done);
});
QUnit.test('`visit` advances readiness.', function() {
expect(2);
equal(App._readinessDeferrals, 1, 'App is in deferred state after setupForTesting.');
return App.testHelpers.visit('/').then(function() {
equal(App._readinessDeferrals, 0, 'App\'s readiness was advanced by visit.');
});
});
QUnit.test('`wait` helper can be passed a resolution value', function() {
expect(4);
var promise, wait;
promise = new RSVP.Promise(function(resolve) {
run(null, resolve, 'promise');
});
run(App, App.advanceReadiness);
wait = App.testHelpers.wait;
return wait('text').then(function(val) {
equal(val, 'text', 'can resolve to a string');
return wait(1);
}).then(function(val) {
equal(val, 1, 'can resolve to an integer');
return wait({ age: 10 });
}).then(function(val) {
deepEqual(val, { age: 10 }, 'can resolve to an object');
return wait(promise);
}).then(function(val) {
equal(val, 'promise', 'can resolve to a promise resolution value');
});
});
import { test } from 'internal-test-helpers/tests/skip-if-glimmer';
test('`click` triggers appropriate events in order', function() {
expect(5);
var click, wait, events;
App.IndexWrapperComponent = Component.extend({
classNames: 'index-wrapper',
didInsertElement() {
this.$().on('mousedown focusin mouseup click', function(e) {
events.push(e.type);
});
}
});
App.XCheckboxComponent = Component.extend({
tagName: 'input',
attributeBindings: ['type'],
type: 'checkbox',
click() {
events.push('click:' + this.get('checked'));
},
change() {
events.push('change:' + this.get('checked'));
}
});
setTemplate('index', compile('{{#index-wrapper}}{{input type="text"}} {{x-checkbox type="checkbox"}} {{textarea}} <div contenteditable="true"> </div>{{/index-wrapper}}'));
run(App, App.advanceReadiness);
click = App.testHelpers.click;
wait = App.testHelpers.wait;
return wait().then(function() {
events = [];
return click('.index-wrapper');
}).then(function() {
deepEqual(events,
['mousedown', 'mouseup', 'click'],
'fires events in order');
}).then(function() {
events = [];
return click('.index-wrapper input[type=text]');
}).then(function() {
deepEqual(events,
['mousedown', 'focusin', 'mouseup', 'click'],
'fires focus events on inputs');
}).then(function() {
events = [];
return click('.index-wrapper textarea');
}).then(function() {
deepEqual(events,
['mousedown', 'focusin', 'mouseup', 'click'],
'fires focus events on textareas');
}).then(function() {
events = [];
return click('.index-wrapper div');
}).then(function() {
deepEqual(events,
['mousedown', 'focusin', 'mouseup', 'click'],
'fires focus events on contenteditable');
}).then(function() {
events = [];
return click('.index-wrapper input[type=checkbox]');
}).then(function() {
// i.e. mousedown, mouseup, change:true, click, click:true
// Firefox differs so we can't assert the exact ordering here.
// See https://bugzilla.mozilla.org/show_bug.cgi?id=843554.
equal(events.length, 5, 'fires click and change on checkboxes');
});
});
test('`click` triggers native events with simulated X/Y coordinates', function() {
expect(15);
var click, wait, events;
App.IndexWrapperComponent = Component.extend({
classNames: 'index-wrapper',
didInsertElement() {
let pushEvent = e => events.push(e);
this.element.addEventListener('mousedown', pushEvent);
this.element.addEventListener('mouseup', pushEvent);
this.element.addEventListener('click', pushEvent);
}
});
setTemplate('index', compile('{{#index-wrapper}}some text{{/index-wrapper}}'));
run(App, App.advanceReadiness);
click = App.testHelpers.click;
wait = App.testHelpers.wait;
return wait().then(function() {
events = [];
return click('.index-wrapper');
}).then(function() {
events.forEach(e => {
ok(e instanceof window.Event, 'The event is an instance of MouseEvent');
ok(typeof e.screenX === 'number' && e.screenX > 0, 'screenX is correct');
ok(typeof e.screenY === 'number' && e.screenY > 0, 'screenY is correct');
ok(typeof e.clientX === 'number' && e.clientX > 0, 'clientX is correct');
ok(typeof e.clientY === 'number' && e.clientY > 0, 'clientY is correct');
});
});
});
test('`triggerEvent` with mouseenter triggers native events with simulated X/Y coordinates', function() {
expect(5);
var triggerEvent, wait, evt;
App.IndexWrapperComponent = Component.extend({
classNames: 'index-wrapper',
didInsertElement() {
this.element.addEventListener('mouseenter', e => evt = e);
}
});
setTemplate('index', compile('{{#index-wrapper}}some text{{/index-wrapper}}'));
run(App, App.advanceReadiness);
triggerEvent = App.testHelpers.triggerEvent;
wait = App.testHelpers.wait;
return wait().then(function() {
return triggerEvent('.index-wrapper', 'mouseenter');
}).then(function() {
ok(evt instanceof window.Event, 'The event is an instance of MouseEvent');
ok(typeof evt.screenX === 'number' && evt.screenX > 0, 'screenX is correct');
ok(typeof evt.screenY === 'number' && evt.screenY > 0, 'screenY is correct');
ok(typeof evt.clientX === 'number' && evt.clientX > 0, 'clientX is correct');
ok(typeof evt.clientY === 'number' && evt.clientY > 0, 'clientY is correct');
});
});
QUnit.test('`wait` waits for outstanding timers', function() {
expect(1);
var wait_done = false;
run(App, App.advanceReadiness);
run.later(this, function() {
wait_done = true;
}, 500);
return App.testHelpers.wait().then(function() {
equal(wait_done, true, 'should wait for the timer to be fired.');
});
});
QUnit.test('`wait` respects registerWaiters with optional context', function() {
expect(3);
let obj = {
counter: 0,
ready() {
return ++this.counter > 2;
}
};
let other = 0;
function otherWaiter() {
return ++other > 2;
}
run(App, App.advanceReadiness);
registerWaiter(obj, obj.ready);
registerWaiter(otherWaiter);
return App.testHelpers.wait().then(function() {
equal(obj.ready(), true, 'should not resolve until our waiter is ready');
unregisterWaiter(obj, obj.ready);
obj.counter = 0;
return App.testHelpers.wait();
}).then(function() {
equal(obj.counter, 0, 'the unregistered waiter should still be at 0');
equal(otherWaiter(), true, 'other waiter should still be registered');
});
});
QUnit.test('`wait` does not error if routing has not begun', function() {
expect(1);
return App.testHelpers.wait().then(function() {
ok(true, 'should not error without `visit`');
});
});
test('`triggerEvent accepts an optional options hash without context', function() {
expect(3);
var triggerEvent, wait, event;
App.IndexWrapperComponent = Component.extend({
didInsertElement() {
this.$('.input').on('keydown change', function(e) {
event = e;
});
}
});
setTemplate('index', compile('{{index-wrapper}}'));
setTemplate('components/index-wrapper', compile('{{input type="text" id="scope" class="input"}}'));
run(App, App.advanceReadiness);
triggerEvent = App.testHelpers.triggerEvent;
wait = App.testHelpers.wait;
return wait().then(function() {
return triggerEvent('.input', 'keydown', { keyCode: 13 });
}).then(function() {
equal(event.keyCode, 13, 'options were passed');
equal(event.type, 'keydown', 'correct event was triggered');
equal(event.target.getAttribute('id'), 'scope', 'triggered on the correct element');
});
});
test('`triggerEvent can limit searching for a selector to a scope', function() {
expect(2);
var triggerEvent, wait, event;
App.IndexWrapperComponent = Component.extend({
didInsertElement() {
this.$('.input').on('blur change', function(e) {
event = e;
});
}
});
setTemplate('components/index-wrapper', compile('{{input type="text" id="outside-scope" class="input"}}<div id="limited">{{input type="text" id="inside-scope" class="input"}}</div>'));
setTemplate('index', compile('{{index-wrapper}}'));
run(App, App.advanceReadiness);
triggerEvent = App.testHelpers.triggerEvent;
wait = App.testHelpers.wait;
return wait().then(function() {
return triggerEvent('.input', '#limited', 'blur');
}).then(function() {
equal(event.type, 'blur', 'correct event was triggered');
equal(event.target.getAttribute('id'), 'inside-scope', 'triggered on the correct element');
});
});
test('`triggerEvent` can be used to trigger arbitrary events', function() {
expect(2);
var triggerEvent, wait, event;
App.IndexWrapperComponent = Component.extend({
didInsertElement() {
this.$('#foo').on('blur change', function(e) {
event = e;
});
}
});
setTemplate('components/index-wrapper', compile('{{input type="text" id="foo"}}'));
setTemplate('index', compile('{{index-wrapper}}'));
run(App, App.advanceReadiness);
triggerEvent = App.testHelpers.triggerEvent;
wait = App.testHelpers.wait;
return wait().then(function() {
return triggerEvent('#foo', 'blur');
}).then(function() {
equal(event.type, 'blur', 'correct event was triggered');
equal(event.target.getAttribute('id'), 'foo', 'triggered on the correct element');
});
});
test('`fillIn` takes context into consideration', function() {
expect(2);
var fillIn, find, visit, andThen, wait;
setTemplate('index', compile('<div id="parent">{{input type="text" id="first" class="current"}}</div>{{input type="text" id="second" class="current"}}'));
run(App, App.advanceReadiness);
fillIn = App.testHelpers.fillIn;
find = App.testHelpers.find;
visit = App.testHelpers.visit;
andThen = App.testHelpers.andThen;
wait = App.testHelpers.wait;
visit('/');
fillIn('.current', '#parent', 'current value');
andThen(function() {
equal(find('#first').val(), 'current value');
equal(find('#second').val(), '');
});
return wait();
});
test('`fillIn` focuses on the element', function() {
expect(2);
var fillIn, find, visit, andThen, wait;
App.ApplicationRoute = Route.extend({
actions: {
wasFocused() {
ok(true, 'focusIn event was triggered');
}
}
});
setTemplate('index', compile('<div id="parent">{{input type="text" id="first" focus-in="wasFocused"}}</div>'));
run(App, App.advanceReadiness);
fillIn = App.testHelpers.fillIn;
find = App.testHelpers.find;
visit = App.testHelpers.visit;
andThen = App.testHelpers.andThen;
wait = App.testHelpers.wait;
visit('/');
fillIn('#first', 'current value');
andThen(function() {
equal(find('#first').val(), 'current value');
});
return wait();
});
test('`fillIn` fires `input` and `change` events in the proper order', function() {
expect(1);
var fillIn, visit, andThen, wait;
var events = [];
App.IndexController = Controller.extend({
actions: {
oninputHandler(e) {
events.push(e.type);
},
onchangeHandler(e) {
events.push(e.type);
}
}
});
setTemplate('index', compile('<input type="text" id="first" oninput={{action "oninputHandler"}} onchange={{action "onchangeHandler"}}>'));
run(App, App.advanceReadiness);
fillIn = App.testHelpers.fillIn;
visit = App.testHelpers.visit;
andThen = App.testHelpers.andThen;
wait = App.testHelpers.wait;
visit('/');
fillIn('#first', 'current value');
andThen(function() {
deepEqual(events, ['input', 'change'], '`input` and `change` events are fired in the proper order');
});
return wait();
});
test('`triggerEvent accepts an optional options hash and context', function() {
expect(3);
var triggerEvent, wait, event;
App.IndexWrapperComponent = Component.extend({
didInsertElement() {
this.$('.input').on('keydown change', function(e) {
event = e;
});
}
});
setTemplate('components/index-wrapper', compile('{{input type="text" id="outside-scope" class="input"}}<div id="limited">{{input type="text" id="inside-scope" class="input"}}</div>'));
setTemplate('index', compile('{{index-wrapper}}'));
run(App, App.advanceReadiness);
triggerEvent = App.testHelpers.triggerEvent;
wait = App.testHelpers.wait;
return wait()
.then(function() {
return triggerEvent('.input', '#limited', 'keydown', { keyCode: 13 });
})
.then(function() {
equal(event.keyCode, 13, 'options were passed');
equal(event.type, 'keydown', 'correct event was triggered');
equal(event.target.getAttribute('id'), 'inside-scope', 'triggered on the correct element');
});
});
QUnit.module('ember-testing debugging helpers', {
setup() {
setupApp();
run(function() {
App.Router = EmberRouter.extend({
location: 'none'
});
});
run(App, 'advanceReadiness');
},
teardown() {
cleanup();
}
});
QUnit.test('pauseTest pauses', function() {
expect(1);
function fakeAdapterAsyncStart() {
ok(true, 'Async start should be called');
}
Test.adapter.asyncStart = fakeAdapterAsyncStart;
App.testHelpers.pauseTest();
});
QUnit.module('ember-testing routing helpers', {
setup() {
run(function() {
App = EmberApplication.create();
App.setupForTesting();
App.injectTestHelpers();
App.Router = EmberRouter.extend({
location: 'none'
});
App.Router.map(function() {
this.route('posts', { resetNamespace: true }, function() {
this.route('new');
});
});
});
run(App, 'advanceReadiness');
},
teardown() {
cleanup();
}
});
QUnit.test('currentRouteName for \'/\'', function() {
expect(3);
return App.testHelpers.visit('/').then(function() {
equal(App.testHelpers.currentRouteName(), 'index', 'should equal \'index\'.');
equal(App.testHelpers.currentPath(), 'index', 'should equal \'index\'.');
equal(App.testHelpers.currentURL(), '/', 'should equal \'/\'.');
});
});
QUnit.test('currentRouteName for \'/posts\'', function() {
expect(3);
return App.testHelpers.visit('/posts').then(function() {
equal(App.testHelpers.currentRouteName(), 'posts.index', 'should equal \'posts.index\'.');
equal(App.testHelpers.currentPath(), 'posts.index', 'should equal \'posts.index\'.');
equal(App.testHelpers.currentURL(), '/posts', 'should equal \'/posts\'.');
});
});
QUnit.test('currentRouteName for \'/posts/new\'', function() {
expect(3);
return App.testHelpers.visit('/posts/new').then(function() {
equal(App.testHelpers.currentRouteName(), 'posts.new', 'should equal \'posts.new\'.');
equal(App.testHelpers.currentPath(), 'posts.new', 'should equal \'posts.new\'.');
equal(App.testHelpers.currentURL(), '/posts/new', 'should equal \'/posts/new\'.');
});
});
QUnit.module('ember-testing pendingRequests', {
setup() {
setupApp();
},
teardown() {
cleanup();
}
});
QUnit.test('pendingRequests is maintained for ajaxSend and ajaxComplete events', function() {
equal(pendingRequests(), 0);
var xhr = { some: 'xhr' };
jQuery(document).trigger('ajaxSend', xhr);
equal(pendingRequests(), 1, 'Ember.Test.pendingRequests was incremented');
jQuery(document).trigger('ajaxComplete', xhr);
equal(pendingRequests(), 0, 'Ember.Test.pendingRequests was decremented');
});
QUnit.test('pendingRequests is ignores ajaxComplete events from past setupForTesting calls', function() {
equal(pendingRequests(), 0);
var xhr = { some: 'xhr' };
jQuery(document).trigger('ajaxSend', xhr);
equal(pendingRequests(), 1, 'Ember.Test.pendingRequests was incremented');
run(function() {
setupForTesting();
});
equal(pendingRequests(), 0, 'Ember.Test.pendingRequests was reset');
var altXhr = { some: 'more xhr' };
jQuery(document).trigger('ajaxSend', altXhr);
equal(pendingRequests(), 1, 'Ember.Test.pendingRequests was incremented');
jQuery(document).trigger('ajaxComplete', xhr);
equal(pendingRequests(), 1, 'Ember.Test.pendingRequests is not impressed with your unexpected complete');
});
QUnit.test('pendingRequests is reset by setupForTesting', function() {
incrementPendingRequests();
run(function() {
setupForTesting();
});
equal(pendingRequests(), 0, 'pendingRequests is reset');
});
QUnit.module('ember-testing async router', {
setup() {
cleanup();
run(function() {
App = EmberApplication.create();
App.Router = EmberRouter.extend({
location: 'none'
});
App.Router.map(function() {
this.route('user', { resetNamespace: true }, function() {
this.route('profile');
this.route('edit');
});
});
App.UserRoute = EmberRoute.extend({
model() {
return resolveLater();
}
});
App.UserProfileRoute = EmberRoute.extend({
beforeModel() {
var self = this;
return resolveLater().then(function() {
self.transitionTo('user.edit');
});
}
});
// Emulates a long-running unscheduled async operation.
function resolveLater() {
var promise;
run(function() {
promise = new RSVP.Promise(function(resolve) {
// The wait() helper has a 10ms tick. We should resolve() after at least one tick
// to test whether wait() held off while the async router was still loading. 20ms
// should be enough.
setTimeout(function() {
run(function() {
resolve(EmberObject.create({ firstName: 'Tom' }));
});
}, 20);
});
});
return promise;
}
App.setupForTesting();
});
App.injectTestHelpers();
run(App, 'advanceReadiness');
},
teardown() {
cleanup();
}
});
QUnit.test('currentRouteName for \'/user\'', function() {
expect(4);
return App.testHelpers.visit('/user').then(function() {
equal(currentRouteName(App), 'user.index', 'should equal \'user.index\'.');
equal(currentPath(App), 'user.index', 'should equal \'user.index\'.');
equal(currentURL(App), '/user', 'should equal \'/user\'.');
equal(App.__container__.lookup('route:user').get('controller.model.firstName'), 'Tom', 'should equal \'Tom\'.');
});
});
QUnit.test('currentRouteName for \'/user/profile\'', function() {
expect(4);
return App.testHelpers.visit('/user/profile').then(function() {
equal(currentRouteName(App), 'user.edit', 'should equal \'user.edit\'.');
equal(currentPath(App), 'user.edit', 'should equal \'user.edit\'.');
equal(currentURL(App), '/user/edit', 'should equal \'/user/edit\'.');
equal(App.__container__.lookup('route:user').get('controller.model.firstName'), 'Tom', 'should equal \'Tom\'.');
});
});
var originalVisitHelper, originalFindHelper, originalWaitHelper;
QUnit.module('can override built-in helpers', {
setup() {
originalVisitHelper = Test._helpers.visit;
originalFindHelper = Test._helpers.find;
originalWaitHelper = Test._helpers.wait;
jQuery('<style>#ember-testing-container { position: absolute; background: white; bottom: 0; right: 0; width: 640px; height: 384px; overflow: auto; z-index: 9999; border: 1px solid #ccc; } #ember-testing { zoom: 50%; }</style>').appendTo('head');
jQuery('<div id="ember-testing-container"><div id="ember-testing"></div></div>').appendTo('body');
run(function() {
App = EmberApplication.create({
rootElement: '#ember-testing'
});
App.setupForTesting();
});
},
teardown() {
App.removeTestHelpers();
jQuery('#ember-testing-container, #ember-testing').remove();
run(App, App.destroy);
App = null;
Test._helpers.visit = originalVisitHelper;
Test._helpers.find = originalFindHelper;
Test._helpers.wait = originalWaitHelper;