forked from angular/angular.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngModelOptionsSpec.js
940 lines (741 loc) · 37.3 KB
/
ngModelOptionsSpec.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
'use strict';
/* globals
generateInputCompilerHelper: false,
defaultModelOptions: false
*/
describe('ngModelOptions', function() {
describe('defaultModelOptions', function() {
it('should provide default values', function() {
expect(defaultModelOptions.getOption('updateOn')).toEqual('');
expect(defaultModelOptions.getOption('updateOnDefault')).toEqual(true);
expect(defaultModelOptions.getOption('debounce')).toBe(0);
expect(defaultModelOptions.getOption('getterSetter')).toBe(false);
expect(defaultModelOptions.getOption('allowInvalid')).toBe(false);
expect(defaultModelOptions.getOption('timezone')).toBe(null);
});
});
describe('directive', function() {
describe('basic usage', function() {
var helper = {}, $rootScope, $compile, $timeout, $q;
generateInputCompilerHelper(helper);
beforeEach(inject(function(_$compile_, _$rootScope_, _$timeout_, _$q_) {
$compile = _$compile_;
$rootScope = _$rootScope_;
$timeout = _$timeout_;
$q = _$q_;
}));
describe('should fall back to `defaultModelOptions`', function() {
it('if there is no `ngModelOptions` directive', function() {
var inputElm = helper.compileInput(
'<input type="text" ng-model="name" name="alias" />');
var inputOptions = $rootScope.form.alias.$options;
expect(inputOptions.getOption('updateOn')).toEqual(defaultModelOptions.getOption('updateOn'));
expect(inputOptions.getOption('updateOnDefault')).toEqual(defaultModelOptions.getOption('updateOnDefault'));
expect(inputOptions.getOption('debounce')).toEqual(defaultModelOptions.getOption('debounce'));
expect(inputOptions.getOption('getterSetter')).toEqual(defaultModelOptions.getOption('getterSetter'));
expect(inputOptions.getOption('allowInvalid')).toEqual(defaultModelOptions.getOption('allowInvalid'));
expect(inputOptions.getOption('timezone')).toEqual(defaultModelOptions.getOption('timezone'));
});
it('if `ngModelOptions` on the same element does not specify the option', function() {
var inputElm = helper.compileInput(
'<input type="text" ng-model="name" name="alias" ng-model-options="{ updateOn: \'blur\' }"/>');
var inputOptions = $rootScope.form.alias.$options;
expect(inputOptions.getOption('debounce')).toEqual(defaultModelOptions.getOption('debounce'));
expect(inputOptions.getOption('updateOnDefault')).toBe(false);
expect(inputOptions.getOption('updateOnDefault')).not.toEqual(defaultModelOptions.getOption('updateOnDefault'));
});
it('if the first `ngModelOptions` ancestor does not specify the option', function() {
var form = $compile('<form name="form" ng-model-options="{ updateOn: \'blur\' }">' +
'<input name="alias" ng-model="x">' +
'</form>')($rootScope);
var inputOptions = $rootScope.form.alias.$options;
expect(inputOptions.getOption('debounce')).toEqual(defaultModelOptions.getOption('debounce'));
expect(inputOptions.getOption('updateOnDefault')).toBe(false);
expect(inputOptions.getOption('updateOnDefault')).not.toEqual(defaultModelOptions.getOption('updateOnDefault'));
dealoc(form);
});
});
describe('sharing and inheritance', function() {
it('should not inherit options from ancestor `ngModelOptions` directives by default', function() {
var container = $compile(
'<div ng-model-options="{ allowInvalid: true }">' +
'<form ng-model-options="{ updateOn: \'blur\' }">' +
'<input ng-model-options="{ updateOn: \'default\' }">' +
'</form>' +
'</div>')($rootScope);
var form = container.find('form');
var input = container.find('input');
var containerOptions = container.controller('ngModelOptions').$options;
var formOptions = form.controller('ngModelOptions').$options;
var inputOptions = input.controller('ngModelOptions').$options;
expect(containerOptions.getOption('allowInvalid')).toEqual(true);
expect(formOptions.getOption('allowInvalid')).toEqual(false);
expect(inputOptions.getOption('allowInvalid')).toEqual(false);
expect(containerOptions.getOption('updateOn')).toEqual('');
expect(containerOptions.getOption('updateOnDefault')).toEqual(true);
expect(formOptions.getOption('updateOn')).toEqual('blur');
expect(formOptions.getOption('updateOnDefault')).toEqual(false);
expect(inputOptions.getOption('updateOn')).toEqual('');
expect(inputOptions.getOption('updateOnDefault')).toEqual(true);
dealoc(container);
});
it('should inherit options that are marked with "$inherit" from the nearest ancestor `ngModelOptions` directive', function() {
var container = $compile(
'<div ng-model-options="{ allowInvalid: true }">' +
'<form ng-model-options="{ updateOn: \'blur\', allowInvalid: \'$inherit\' }">' +
'<input ng-model-options="{ updateOn: \'default\' }">' +
'</form>' +
'</div>')($rootScope);
var form = container.find('form');
var input = container.find('input');
var containerOptions = container.controller('ngModelOptions').$options;
var formOptions = form.controller('ngModelOptions').$options;
var inputOptions = input.controller('ngModelOptions').$options;
expect(containerOptions.getOption('allowInvalid')).toEqual(true);
expect(formOptions.getOption('allowInvalid')).toEqual(true);
expect(inputOptions.getOption('allowInvalid')).toEqual(false);
expect(containerOptions.getOption('updateOn')).toEqual('');
expect(containerOptions.getOption('updateOnDefault')).toEqual(true);
expect(formOptions.getOption('updateOn')).toEqual('blur');
expect(formOptions.getOption('updateOnDefault')).toEqual(false);
expect(inputOptions.getOption('updateOn')).toEqual('');
expect(inputOptions.getOption('updateOnDefault')).toEqual(true);
dealoc(container);
});
it('should inherit all unspecified options if the options object contains a `"*"` property with value "$inherit"', function() {
var container = $compile(
'<div ng-model-options="{ allowInvalid: true, debounce: 100, updateOn: \'keyup\' }">' +
'<form ng-model-options="{ updateOn: \'blur\', \'*\': \'$inherit\' }">' +
'<input ng-model-options="{ updateOn: \'default\' }">' +
'</form>' +
'</div>')($rootScope);
var form = container.find('form');
var input = container.find('input');
var containerOptions = container.controller('ngModelOptions').$options;
var formOptions = form.controller('ngModelOptions').$options;
var inputOptions = input.controller('ngModelOptions').$options;
expect(containerOptions.getOption('allowInvalid')).toEqual(true);
expect(formOptions.getOption('allowInvalid')).toEqual(true);
expect(inputOptions.getOption('allowInvalid')).toEqual(false);
expect(containerOptions.getOption('debounce')).toEqual(100);
expect(formOptions.getOption('debounce')).toEqual(100);
expect(inputOptions.getOption('debounce')).toEqual(0);
expect(containerOptions.getOption('updateOn')).toEqual('keyup');
expect(containerOptions.getOption('updateOnDefault')).toEqual(false);
expect(formOptions.getOption('updateOn')).toEqual('blur');
expect(formOptions.getOption('updateOnDefault')).toEqual(false);
expect(inputOptions.getOption('updateOn')).toEqual('');
expect(inputOptions.getOption('updateOnDefault')).toEqual(true);
dealoc(container);
});
it('should correctly inherit default and another specified event for `updateOn`', function() {
var container = $compile(
'<div ng-model-options="{updateOn: \'default blur\'}">' +
'<input ng-model-options="{\'*\': \'$inherit\'}">' +
'</div>')($rootScope);
var input = container.find('input');
var inputOptions = input.controller('ngModelOptions').$options;
expect(inputOptions.getOption('updateOn')).toEqual('blur');
expect(inputOptions.getOption('updateOnDefault')).toEqual(true);
dealoc(container);
});
it('should `updateOnDefault` as well if we have `updateOn: "$inherit"`', function() {
var container = $compile(
'<div ng-model-options="{updateOn: \'keyup\'}">' +
'<input ng-model-options="{updateOn: \'$inherit\'}">' +
'<div ng-model-options="{updateOn: \'default blur\'}">' +
'<input ng-model-options="{updateOn: \'$inherit\'}">' +
'</div>' +
'</div>')($rootScope);
var input1 = container.find('input').eq(0);
var inputOptions1 = input1.controller('ngModelOptions').$options;
expect(inputOptions1.getOption('updateOn')).toEqual('keyup');
expect(inputOptions1.getOption('updateOnDefault')).toEqual(false);
var input2 = container.find('input').eq(1);
var inputOptions2 = input2.controller('ngModelOptions').$options;
expect(inputOptions2.getOption('updateOn')).toEqual('blur');
expect(inputOptions2.getOption('updateOnDefault')).toEqual(true);
dealoc(container);
});
it('should make a copy of the options object', function() {
$rootScope.options = {updateOn: 'default'};
var inputElm = helper.compileInput(
'<input type="text" ng-model="name" name="alias" ' +
'ng-model-options="options"' +
'/>');
expect($rootScope.options).toEqual({updateOn: 'default'});
expect($rootScope.form.alias.$options).not.toBe($rootScope.options);
});
it('should be retrieved from an ancestor element containing an `ngModelOptions` directive', function() {
var doc = $compile(
'<form name="test" ' +
'ng-model-options="{ debounce: 10000, updateOn: \'blur\' }" >' +
'<input type="text" ng-model="name" name="alias" />' +
'</form>')($rootScope);
$rootScope.$digest();
var inputElm = doc.find('input');
helper.changeGivenInputTo(inputElm, 'a');
expect($rootScope.name).toEqual(undefined);
browserTrigger(inputElm, 'blur');
expect($rootScope.name).toBeUndefined();
$timeout.flush(2000);
expect($rootScope.name).toBeUndefined();
$timeout.flush(9000);
expect($rootScope.name).toEqual('a');
dealoc(doc);
});
it('should allow sharing options between multiple inputs', function() {
$rootScope.options = {updateOn: 'default'};
var inputElm = helper.compileInput(
'<input type="text" ng-model="name1" name="alias1" ' +
'ng-model-options="options"' +
'/>' +
'<input type="text" ng-model="name2" name="alias2" ' +
'ng-model-options="options"' +
'/>');
helper.changeGivenInputTo(inputElm.eq(0), 'a');
helper.changeGivenInputTo(inputElm.eq(1), 'b');
expect($rootScope.name1).toEqual('a');
expect($rootScope.name2).toEqual('b');
});
});
describe('updateOn', function() {
it('should allow overriding the model update trigger event on text inputs', function() {
var inputElm = helper.compileInput(
'<input type="text" ng-model="name" name="alias" ' +
'ng-model-options="{ updateOn: \'blur\' }"' +
'/>');
helper.changeInputValueTo('a');
expect($rootScope.name).toBeUndefined();
browserTrigger(inputElm, 'blur');
expect($rootScope.name).toEqual('a');
});
it('should not dirty the input if nothing was changed before updateOn trigger', function() {
var inputElm = helper.compileInput(
'<input type="text" ng-model="name" name="alias" ' +
'ng-model-options="{ updateOn: \'blur\' }"' +
'/>');
browserTrigger(inputElm, 'blur');
expect($rootScope.form.alias.$pristine).toBeTruthy();
});
it('should allow overriding the model update trigger event on text areas', function() {
var inputElm = helper.compileInput(
'<textarea ng-model="name" name="alias" ' +
'ng-model-options="{ updateOn: \'blur\' }"' +
'/>');
helper.changeInputValueTo('a');
expect($rootScope.name).toBeUndefined();
browserTrigger(inputElm, 'blur');
expect($rootScope.name).toEqual('a');
});
it('should bind the element to a list of events', function() {
var inputElm = helper.compileInput(
'<input type="text" ng-model="name" name="alias" ' +
'ng-model-options="{ updateOn: \'blur mousemove\' }"' +
'/>');
helper.changeInputValueTo('a');
expect($rootScope.name).toBeUndefined();
browserTrigger(inputElm, 'blur');
expect($rootScope.name).toEqual('a');
helper.changeInputValueTo('b');
expect($rootScope.name).toEqual('a');
browserTrigger(inputElm, 'mousemove');
expect($rootScope.name).toEqual('b');
});
it('should allow keeping the default update behavior on text inputs', function() {
var inputElm = helper.compileInput(
'<input type="text" ng-model="name" name="alias" ' +
'ng-model-options="{ updateOn: \'default\' }"' +
'/>');
helper.changeInputValueTo('a');
expect($rootScope.name).toEqual('a');
});
it('should allow overriding the model update trigger event on checkboxes', function() {
var inputElm = helper.compileInput(
'<input type="checkbox" ng-model="checkbox" ' +
'ng-model-options="{ updateOn: \'blur\' }"' +
'/>');
browserTrigger(inputElm, 'click');
expect($rootScope.checkbox).toBeUndefined();
browserTrigger(inputElm, 'blur');
expect($rootScope.checkbox).toBe(true);
browserTrigger(inputElm, 'click');
expect($rootScope.checkbox).toBe(true);
});
it('should allow keeping the default update behavior on checkboxes', function() {
var inputElm = helper.compileInput(
'<input type="checkbox" ng-model="checkbox" ' +
'ng-model-options="{ updateOn: \'blur default\' }"' +
'/>');
browserTrigger(inputElm, 'click');
expect($rootScope.checkbox).toBe(true);
browserTrigger(inputElm, 'click');
expect($rootScope.checkbox).toBe(false);
});
it('should allow overriding the model update trigger event on radio buttons', function() {
var inputElm = helper.compileInput(
'<input type="radio" ng-model="color" value="white" ' +
'ng-model-options="{ updateOn: \'blur\'}"' +
'/>' +
'<input type="radio" ng-model="color" value="red" ' +
'ng-model-options="{ updateOn: \'blur\'}"' +
'/>' +
'<input type="radio" ng-model="color" value="blue" ' +
'ng-model-options="{ updateOn: \'blur\'}"' +
'/>');
$rootScope.$apply('color = \'white\'');
browserTrigger(inputElm[2], 'click');
expect($rootScope.color).toBe('white');
browserTrigger(inputElm[2], 'blur');
expect($rootScope.color).toBe('blue');
});
it('should allow keeping the default update behavior on radio buttons', function() {
var inputElm = helper.compileInput(
'<input type="radio" ng-model="color" value="white" ' +
'ng-model-options="{ updateOn: \'blur default\' }"' +
'/>' +
'<input type="radio" ng-model="color" value="red" ' +
'ng-model-options="{ updateOn: \'blur default\' }"' +
'/>' +
'<input type="radio" ng-model="color" value="blue" ' +
'ng-model-options="{ updateOn: \'blur default\' }"' +
'/>');
$rootScope.$apply('color = \'white\'');
browserTrigger(inputElm[2], 'click');
expect($rootScope.color).toBe('blue');
});
it('should re-set the trigger events when overridden with $overrideModelOptions', function() {
var inputElm = helper.compileInput(
'<input type="text" ng-model="name" name="alias" ' +
'ng-model-options="{ updateOn: \'blur click\' }"' +
'/>');
var ctrl = inputElm.controller('ngModel');
helper.changeInputValueTo('a');
expect($rootScope.name).toBeUndefined();
browserTrigger(inputElm, 'blur');
expect($rootScope.name).toEqual('a');
helper.changeInputValueTo('b');
expect($rootScope.name).toBe('a');
browserTrigger(inputElm, 'click');
expect($rootScope.name).toEqual('b');
$rootScope.$apply('name = undefined');
expect(inputElm.val()).toBe('');
ctrl.$overrideModelOptions({updateOn: 'blur mousedown'});
helper.changeInputValueTo('a');
expect($rootScope.name).toBeUndefined();
browserTrigger(inputElm, 'blur');
expect($rootScope.name).toEqual('a');
helper.changeInputValueTo('b');
expect($rootScope.name).toBe('a');
browserTrigger(inputElm, 'click');
expect($rootScope.name).toBe('a');
browserTrigger(inputElm, 'mousedown');
expect($rootScope.name).toEqual('b');
});
});
describe('debounce', function() {
it('should trigger only after timeout in text inputs', function() {
var inputElm = helper.compileInput(
'<input type="text" ng-model="name" name="alias" ' +
'ng-model-options="{ debounce: 10000 }"' +
'/>');
helper.changeInputValueTo('a');
helper.changeInputValueTo('b');
helper.changeInputValueTo('c');
expect($rootScope.name).toEqual(undefined);
$timeout.flush(2000);
expect($rootScope.name).toEqual(undefined);
$timeout.flush(9000);
expect($rootScope.name).toEqual('c');
});
it('should trigger only after timeout in checkboxes', function() {
var inputElm = helper.compileInput(
'<input type="checkbox" ng-model="checkbox" ' +
'ng-model-options="{ debounce: 10000 }"' +
'/>');
browserTrigger(inputElm, 'click');
expect($rootScope.checkbox).toBeUndefined();
$timeout.flush(2000);
expect($rootScope.checkbox).toBeUndefined();
$timeout.flush(9000);
expect($rootScope.checkbox).toBe(true);
});
it('should trigger only after timeout in radio buttons', function() {
var inputElm = helper.compileInput(
'<input type="radio" ng-model="color" value="white" />' +
'<input type="radio" ng-model="color" value="red" ' +
'ng-model-options="{ debounce: 20000 }"' +
'/>' +
'<input type="radio" ng-model="color" value="blue" ' +
'ng-model-options="{ debounce: 30000 }"' +
'/>');
browserTrigger(inputElm[0], 'click');
expect($rootScope.color).toBe('white');
browserTrigger(inputElm[1], 'click');
expect($rootScope.color).toBe('white');
$timeout.flush(12000);
expect($rootScope.color).toBe('white');
$timeout.flush(10000);
expect($rootScope.color).toBe('red');
});
it('should not trigger digest while debouncing', function() {
var inputElm = helper.compileInput(
'<input type="text" ng-model="name" name="alias" ' +
'ng-model-options="{ debounce: 10000 }"' +
'/>');
var watchSpy = jasmine.createSpy('watchSpy');
$rootScope.$watch(watchSpy);
helper.changeInputValueTo('a');
$timeout.flush(2000);
expect(watchSpy).not.toHaveBeenCalled();
helper.changeInputValueTo('b');
$timeout.flush(2000);
expect(watchSpy).not.toHaveBeenCalled();
helper.changeInputValueTo('c');
$timeout.flush(10000);
expect(watchSpy).toHaveBeenCalled();
});
it('should allow selecting different debounce timeouts for each event',
function() {
var inputElm = helper.compileInput(
'<input type="text" ng-model="name" name="alias" ' +
'ng-model-options="{' +
'updateOn: \'default blur mouseup\', ' +
'debounce: { default: 10000, blur: 5000 }' +
'}"' +
'/>');
helper.changeInputValueTo('a');
expect($rootScope.name).toBeUndefined();
$timeout.flush(6000);
expect($rootScope.name).toBeUndefined();
$timeout.flush(4000);
expect($rootScope.name).toEqual('a');
helper.changeInputValueTo('b');
browserTrigger(inputElm, 'blur');
$timeout.flush(4000);
expect($rootScope.name).toEqual('a');
$timeout.flush(2000);
expect($rootScope.name).toEqual('b');
helper.changeInputValueTo('c');
browserTrigger(helper.inputElm, 'mouseup');
// `default` in `debounce` only affects the event triggers that are not defined in updateOn
expect($rootScope.name).toEqual('c');
});
it('should use the value of * to debounce all unspecified events',
function() {
var inputElm = helper.compileInput(
'<input type="text" ng-model="name" name="alias" ' +
'ng-model-options="{' +
'updateOn: \'default blur mouseup\', ' +
'debounce: { default: 10000, blur: 5000, \'*\': 15000 }' +
'}"' +
'/>');
helper.changeInputValueTo('a');
expect($rootScope.name).toBeUndefined();
$timeout.flush(6000);
expect($rootScope.name).toBeUndefined();
$timeout.flush(4000);
expect($rootScope.name).toEqual('a');
helper.changeInputValueTo('b');
browserTrigger(inputElm, 'blur');
$timeout.flush(4000);
expect($rootScope.name).toEqual('a');
$timeout.flush(2000);
expect($rootScope.name).toEqual('b');
helper.changeInputValueTo('c');
browserTrigger(helper.inputElm, 'mouseup');
expect($rootScope.name).toEqual('b');
$timeout.flush(10000); // flush default
expect($rootScope.name).toEqual('b');
$timeout.flush(5000);
expect($rootScope.name).toEqual('c');
});
it('should trigger immediately for the event if not listed in the debounce list',
function() {
var inputElm = helper.compileInput(
'<input type="text" ng-model="name" name="alias" ' +
'ng-model-options="{' +
'updateOn: \'default blur foo\', ' +
'debounce: { blur: 5000 }' +
'}"' +
'/>');
helper.changeInputValueTo('a');
expect($rootScope.name).toEqual('a');
helper.changeInputValueTo('b');
browserTrigger(inputElm, 'foo');
expect($rootScope.name).toEqual('b');
});
it('should allow selecting different debounce timeouts for each event on checkboxes', function() {
var inputElm = helper.compileInput('<input type="checkbox" ng-model="checkbox" ' +
'ng-model-options="{ ' +
'updateOn: \'default blur\', debounce: { default: 10000, blur: 5000 } }"' +
'/>');
inputElm[0].checked = false;
browserTrigger(inputElm, 'click');
expect($rootScope.checkbox).toBeUndefined();
$timeout.flush(8000);
expect($rootScope.checkbox).toBeUndefined();
$timeout.flush(3000);
expect($rootScope.checkbox).toBe(true);
inputElm[0].checked = true;
browserTrigger(inputElm, 'click');
browserTrigger(inputElm, 'blur');
$timeout.flush(3000);
expect($rootScope.checkbox).toBe(true);
$timeout.flush(3000);
expect($rootScope.checkbox).toBe(false);
});
it('should allow selecting 0 for non-default debounce timeouts for each event on checkboxes', function() {
var inputElm = helper.compileInput('<input type="checkbox" ng-model="checkbox" ' +
'ng-model-options="{ ' +
'updateOn: \'default blur\', debounce: { default: 10000, blur: 0 } }"' +
'/>');
inputElm[0].checked = false;
browserTrigger(inputElm, 'click');
expect($rootScope.checkbox).toBeUndefined();
$timeout.flush(8000);
expect($rootScope.checkbox).toBeUndefined();
$timeout.flush(3000);
expect($rootScope.checkbox).toBe(true);
inputElm[0].checked = true;
browserTrigger(inputElm, 'click');
browserTrigger(inputElm, 'blur');
$timeout.flush(0);
expect($rootScope.checkbox).toBe(false);
});
it('should flush debounced events when calling $commitViewValue directly', function() {
var inputElm = helper.compileInput(
'<input type="text" ng-model="name" name="alias" ' +
'ng-model-options="{ debounce: 1000 }" />');
helper.changeInputValueTo('a');
expect($rootScope.name).toEqual(undefined);
$rootScope.form.alias.$commitViewValue();
expect($rootScope.name).toEqual('a');
});
it('should cancel debounced events when calling $commitViewValue', function() {
var inputElm = helper.compileInput(
'<input type="text" ng-model="name" name="alias" ' +
'ng-model-options="{ debounce: 1000 }"/>');
helper.changeInputValueTo('a');
$rootScope.form.alias.$commitViewValue();
expect($rootScope.name).toEqual('a');
$rootScope.form.alias.$setPristine();
$timeout.flush(1000);
expect($rootScope.form.alias.$pristine).toBeTruthy();
});
it('should reset input val if rollbackViewValue called during pending update', function() {
var inputElm = helper.compileInput(
'<input type="text" ng-model="name" name="alias" ' +
'ng-model-options="{ updateOn: \'blur\' }" />');
helper.changeInputValueTo('a');
expect(inputElm.val()).toBe('a');
$rootScope.form.alias.$rollbackViewValue();
expect(inputElm.val()).toBe('');
browserTrigger(inputElm, 'blur');
expect(inputElm.val()).toBe('');
});
it('should allow canceling pending updates', function() {
var inputElm = helper.compileInput(
'<input type="text" ng-model="name" name="alias" ' +
'ng-model-options="{ updateOn: \'blur\' }" />');
helper.changeInputValueTo('a');
expect($rootScope.name).toEqual(undefined);
$rootScope.form.alias.$rollbackViewValue();
expect($rootScope.name).toEqual(undefined);
browserTrigger(inputElm, 'blur');
expect($rootScope.name).toEqual(undefined);
});
it('should allow canceling debounced updates', function() {
var inputElm = helper.compileInput(
'<input type="text" ng-model="name" name="alias" ' +
'ng-model-options="{ debounce: 10000 }" />');
helper.changeInputValueTo('a');
expect($rootScope.name).toEqual(undefined);
$timeout.flush(2000);
$rootScope.form.alias.$rollbackViewValue();
expect($rootScope.name).toEqual(undefined);
$timeout.flush(10000);
expect($rootScope.name).toEqual(undefined);
});
it('should handle model updates correctly even if rollbackViewValue is not invoked', function() {
var inputElm = helper.compileInput(
'<input type="text" ng-model="name" name="alias" ' +
'ng-model-options="{ updateOn: \'blur\' }" />');
helper.changeInputValueTo('a');
$rootScope.$apply('name = \'b\'');
browserTrigger(inputElm, 'blur');
expect($rootScope.name).toBe('b');
});
it('should reset input val if rollbackViewValue called during debounce', function() {
var inputElm = helper.compileInput(
'<input type="text" ng-model="name" name="alias" ' +
'ng-model-options="{ debounce: 2000 }" />');
helper.changeInputValueTo('a');
expect(inputElm.val()).toBe('a');
$rootScope.form.alias.$rollbackViewValue();
expect(inputElm.val()).toBe('');
$timeout.flush(3000);
expect(inputElm.val()).toBe('');
});
});
describe('getterSetter', function() {
it('should not try to invoke a model if getterSetter is false', function() {
var inputElm = helper.compileInput(
'<input type="text" ng-model="name" ' +
'ng-model-options="{ getterSetter: false }" />');
var spy = $rootScope.name = jasmine.createSpy('setterSpy');
helper.changeInputValueTo('a');
expect(spy).not.toHaveBeenCalled();
expect(inputElm.val()).toBe('a');
});
it('should not try to invoke a model if getterSetter is not set', function() {
var inputElm = helper.compileInput('<input type="text" ng-model="name" />');
var spy = $rootScope.name = jasmine.createSpy('setterSpy');
helper.changeInputValueTo('a');
expect(spy).not.toHaveBeenCalled();
expect(inputElm.val()).toBe('a');
});
it('should try to invoke a function model if getterSetter is true', function() {
var inputElm = helper.compileInput(
'<input type="text" ng-model="name" ' +
'ng-model-options="{ getterSetter: true }" />');
var spy = $rootScope.name = jasmine.createSpy('setterSpy').and.callFake(function() {
return 'b';
});
$rootScope.$apply();
expect(inputElm.val()).toBe('b');
helper.changeInputValueTo('a');
expect(inputElm.val()).toBe('b');
expect(spy).toHaveBeenCalledWith('a');
expect($rootScope.name).toBe(spy);
});
it('should assign to non-function models if getterSetter is true', function() {
var inputElm = helper.compileInput(
'<input type="text" ng-model="name" ' +
'ng-model-options="{ getterSetter: true }" />');
$rootScope.name = 'c';
helper.changeInputValueTo('d');
expect(inputElm.val()).toBe('d');
expect($rootScope.name).toBe('d');
});
it('should fail on non-assignable model binding if getterSetter is false', function() {
expect(function() {
var inputElm = helper.compileInput('<input type="text" ng-model="accessor(user, \'name\')" />');
}).toThrowMinErr('ngModel', 'nonassign', 'Expression \'accessor(user, \'name\')\' is non-assignable.');
});
it('should not fail on non-assignable model binding if getterSetter is true', function() {
var inputElm = helper.compileInput(
'<input type="text" ng-model="accessor(user, \'name\')" ' +
'ng-model-options="{ getterSetter: true }" />');
});
it('should invoke a model in the correct context if getterSetter is true', function() {
var inputElm = helper.compileInput(
'<input type="text" ng-model="someService.getterSetter" ' +
'ng-model-options="{ getterSetter: true }" />');
$rootScope.someService = {
value: 'a',
getterSetter: function(newValue) {
this.value = newValue || this.value;
return this.value;
}
};
spyOn($rootScope.someService, 'getterSetter').and.callThrough();
$rootScope.$apply();
expect(inputElm.val()).toBe('a');
expect($rootScope.someService.getterSetter).toHaveBeenCalledWith();
expect($rootScope.someService.value).toBe('a');
helper.changeInputValueTo('b');
expect($rootScope.someService.getterSetter).toHaveBeenCalledWith('b');
expect($rootScope.someService.value).toBe('b');
$rootScope.someService.value = 'c';
$rootScope.$apply();
expect(inputElm.val()).toBe('c');
expect($rootScope.someService.getterSetter).toHaveBeenCalledWith();
});
});
describe('allowInvalid', function() {
it('should assign invalid values to the scope if allowInvalid is true', function() {
var inputElm = helper.compileInput('<input type="text" name="input" ng-model="value" maxlength="1" ' +
'ng-model-options="{allowInvalid: true}" />');
helper.changeInputValueTo('12345');
expect($rootScope.value).toBe('12345');
expect(inputElm).toBeInvalid();
});
it('should not assign not parsable values to the scope if allowInvalid is true', function() {
var inputElm = helper.compileInput('<input type="number" name="input" ng-model="value" ' +
'ng-model-options="{allowInvalid: true}" />', {
valid: false,
badInput: true
});
helper.changeInputValueTo('abcd');
expect($rootScope.value).toBeUndefined();
expect(inputElm).toBeInvalid();
});
it('should update the scope before async validators execute if allowInvalid is true', function() {
var inputElm = helper.compileInput('<input type="text" name="input" ng-model="value" ' +
'ng-model-options="{allowInvalid: true}" />');
var defer;
$rootScope.form.input.$asyncValidators.promiseValidator = function(value) {
defer = $q.defer();
return defer.promise;
};
helper.changeInputValueTo('12345');
expect($rootScope.value).toBe('12345');
expect($rootScope.form.input.$pending.promiseValidator).toBe(true);
defer.reject();
$rootScope.$digest();
expect($rootScope.value).toBe('12345');
expect(inputElm).toBeInvalid();
});
it('should update the view before async validators execute if allowInvalid is true', function() {
var inputElm = helper.compileInput('<input type="text" name="input" ng-model="value" ' +
'ng-model-options="{allowInvalid: true}" />');
var defer;
$rootScope.form.input.$asyncValidators.promiseValidator = function(value) {
defer = $q.defer();
return defer.promise;
};
$rootScope.$apply('value = \'12345\'');
expect(inputElm.val()).toBe('12345');
expect($rootScope.form.input.$pending.promiseValidator).toBe(true);
defer.reject();
$rootScope.$digest();
expect(inputElm.val()).toBe('12345');
expect(inputElm).toBeInvalid();
});
it('should not call ng-change listeners twice if the model did not change with allowInvalid', function() {
var inputElm = helper.compileInput('<input type="text" name="input" ng-model="value" ' +
'ng-model-options="{allowInvalid: true}" ng-change="changed()" />');
$rootScope.changed = jasmine.createSpy('changed');
$rootScope.form.input.$parsers.push(function(value) {
return 'modelValue';
});
helper.changeInputValueTo('input1');
expect($rootScope.value).toBe('modelValue');
expect($rootScope.changed).toHaveBeenCalledOnce();
helper.changeInputValueTo('input2');
expect($rootScope.value).toBe('modelValue');
expect($rootScope.changed).toHaveBeenCalledOnce();
});
});
});
describe('on directives with `replace: true`', function() {
var $rootScope, $compile;
beforeEach(module(function($compileProvider) {
$compileProvider.directive('foo', valueFn({
replace: true,
template: '<input type="text" ng-model-options="{debounce: 1000}" />'
}));
}));
beforeEach(inject(function(_$compile_, _$rootScope_) {
$compile = _$compile_;
$rootScope = _$rootScope_;
}));
it('should get initialized in time for `ngModel` on the original element', function() {
var inputElm = $compile('<foo ng-model="value"></foo>')($rootScope);
var ngModelCtrl = inputElm.controller('ngModel');
expect(ngModelCtrl.$options.getOption('debounce')).toBe(1000);
});
});
});
});