This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27.5k
/
Copy pathariaSpec.js
1247 lines (968 loc) · 45.1 KB
/
ariaSpec.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
'use strict';
/* globals nativeAriaNodeNames false */
describe('$aria', function() {
var scope, $compile, element;
beforeEach(module('ngAria'));
afterEach(function() {
dealoc(element);
});
describe('with `ngAriaDisable`', function() {
beforeEach(injectScopeAndCompiler);
beforeEach(function() {
jasmine.addMatchers({
toHaveAttribute: function toHaveAttributeMatcher() {
return {
compare: function toHaveAttributeCompare(element, attr) {
var node = element[0];
var pass = node.hasAttribute(attr);
var message = 'Expected `' + node.outerHTML + '` ' + (pass ? 'not ' : '') +
'to have attribute `' + attr + '`.';
return {
pass: pass,
message: message
};
}
};
}
});
});
// ariaChecked
it('should not attach aria-checked to custom checkbox', function() {
compileElement('<div role="checkbox" ng-model="val" ng-aria-disable></div>');
scope.$apply('val = false');
expect(element).not.toHaveAttribute('aria-checked');
scope.$apply('val = true');
expect(element).not.toHaveAttribute('aria-checked');
});
it('should not attach aria-checked to custom radio controls', function() {
compileElement(
'<div role="radio" ng-model="val" value="one" ng-aria-disable></div>' +
'<div role="radio" ng-model="val" value="two" ng-aria-disable></div>');
var radio1 = element.eq(0);
var radio2 = element.eq(1);
scope.$apply('val = "one"');
expect(radio1).not.toHaveAttribute('aria-checked');
expect(radio2).not.toHaveAttribute('aria-checked');
scope.$apply('val = "two"');
expect(radio1).not.toHaveAttribute('aria-checked');
expect(radio2).not.toHaveAttribute('aria-checked');
});
// ariaDisabled
it('should not attach aria-disabled to custom controls', function() {
compileElement('<div ng-disabled="val" ng-aria-disable></div>');
scope.$apply('val = false');
expect(element).not.toHaveAttribute('aria-disabled');
scope.$apply('val = true');
expect(element).not.toHaveAttribute('aria-disabled');
});
// ariaHidden
it('should not attach aria-hidden to `ngShow`', function() {
compileElement('<div ng-show="val" ng-aria-disable></div>');
scope.$apply('val = false');
expect(element).not.toHaveAttribute('aria-hidden');
scope.$apply('val = true');
expect(element).not.toHaveAttribute('aria-hidden');
});
it('should not attach aria-hidden to `ngHide`', function() {
compileElement('<div ng-hide="val" ng-aria-disable></div>');
scope.$apply('val = false');
expect(element).not.toHaveAttribute('aria-hidden');
scope.$apply('val = true');
expect(element).not.toHaveAttribute('aria-hidden');
});
// ariaInvalid
it('should not attach aria-invalid to input', function() {
compileElement('<input ng-model="val" ng-minlength="10" ng-aria-disable />');
scope.$apply('val = "lt 10"');
expect(element).not.toHaveAttribute('aria-invalid');
scope.$apply('val = "gt 10 characters"');
expect(element).not.toHaveAttribute('aria-invalid');
});
it('should not attach aria-invalid to custom controls', function() {
compileElement('<div role="textbox" ng-model="val" ng-minlength="10" ng-aria-disable></div>');
scope.$apply('val = "lt 10"');
expect(element).not.toHaveAttribute('aria-invalid');
scope.$apply('val = "gt 10 characters"');
expect(element).not.toHaveAttribute('aria-invalid');
});
// ariaLive
it('should not attach aria-live to `ngMessages`', function() {
compileElement('<div ng-messages="val" ng-aria-disable>');
expect(element).not.toHaveAttribute('aria-live');
});
// ariaReadonly
it('should not attach aria-readonly to custom controls', function() {
compileElement('<div ng-readonly="val" ng-aria-disable></div>');
scope.$apply('val = false');
expect(element).not.toHaveAttribute('aria-readonly');
scope.$apply('val = true');
expect(element).not.toHaveAttribute('aria-readonly');
});
// ariaRequired
it('should not attach aria-required to custom controls with `required`', function() {
compileElement('<div ng-model="val" required ng-aria-disable></div>');
expect(element).not.toHaveAttribute('aria-required');
});
it('should not attach aria-required to custom controls with `ngRequired`', function() {
compileElement('<div ng-model="val" ng-required="val" ng-aria-disable></div>');
scope.$apply('val = false');
expect(element).not.toHaveAttribute('aria-required');
scope.$apply('val = true');
expect(element).not.toHaveAttribute('aria-required');
});
// ariaValue
it('should not attach aria-value* to input[range]', function() {
compileElement('<input type="range" ng-model="val" min="0" max="100" ng-aria-disable />');
expect(element).not.toHaveAttribute('aria-valuemax');
expect(element).not.toHaveAttribute('aria-valuemin');
expect(element).not.toHaveAttribute('aria-valuenow');
scope.$apply('val = 50');
expect(element).not.toHaveAttribute('aria-valuemax');
expect(element).not.toHaveAttribute('aria-valuemin');
expect(element).not.toHaveAttribute('aria-valuenow');
scope.$apply('val = 150');
expect(element).not.toHaveAttribute('aria-valuemax');
expect(element).not.toHaveAttribute('aria-valuemin');
expect(element).not.toHaveAttribute('aria-valuenow');
});
it('should not attach aria-value* to custom controls', function() {
compileElement(
'<div role="progressbar" ng-model="val" min="0" max="100" ng-aria-disable></div>' +
'<div role="slider" ng-model="val" min="0" max="100" ng-aria-disable></div>');
var progressbar = element.eq(0);
var slider = element.eq(1);
['aria-valuemax', 'aria-valuemin', 'aria-valuenow'].forEach(function(attr) {
expect(progressbar).not.toHaveAttribute(attr);
expect(slider).not.toHaveAttribute(attr);
});
scope.$apply('val = 50');
['aria-valuemax', 'aria-valuemin', 'aria-valuenow'].forEach(function(attr) {
expect(progressbar).not.toHaveAttribute(attr);
expect(slider).not.toHaveAttribute(attr);
});
scope.$apply('val = 150');
['aria-valuemax', 'aria-valuemin', 'aria-valuenow'].forEach(function(attr) {
expect(progressbar).not.toHaveAttribute(attr);
expect(slider).not.toHaveAttribute(attr);
});
});
// bindKeypress
it('should not bind keypress to `ngClick`', function() {
scope.onClick = jasmine.createSpy('onClick');
compileElement(
'<div ng-click="onClick()" tabindex="0" ng-aria-disable></div>' +
'<ul><li ng-click="onClick()" tabindex="0" ng-aria-disable></li></ul>');
var div = element.find('div');
var li = element.find('li');
div.triggerHandler({type: 'keypress', keyCode: 32});
li.triggerHandler({type: 'keypress', keyCode: 32});
expect(scope.onClick).not.toHaveBeenCalled();
});
// bindRoleForClick
it('should not attach role to custom controls', function() {
compileElement(
'<div ng-click="onClick()" ng-aria-disable></div>' +
'<div type="checkbox" ng-model="val" ng-aria-disable></div>' +
'<div type="radio" ng-model="val" ng-aria-disable></div>' +
'<div type="range" ng-model="val" ng-aria-disable></div>');
expect(element.eq(0)).not.toHaveAttribute('role');
expect(element.eq(1)).not.toHaveAttribute('role');
expect(element.eq(2)).not.toHaveAttribute('role');
expect(element.eq(3)).not.toHaveAttribute('role');
});
// tabindex
it('should not attach tabindex to custom controls', function() {
compileElement(
'<div role="checkbox" ng-model="val" ng-aria-disable></div>' +
'<div role="slider" ng-model="val" ng-aria-disable></div>');
expect(element.eq(0)).not.toHaveAttribute('tabindex');
expect(element.eq(1)).not.toHaveAttribute('tabindex');
});
it('should not attach tabindex to `ngClick` or `ngDblclick`', function() {
compileElement(
'<div ng-click="onClick()" ng-aria-disable></div>' +
'<div ng-dblclick="onDblclick()" ng-aria-disable></div>');
expect(element.eq(0)).not.toHaveAttribute('tabindex');
expect(element.eq(1)).not.toHaveAttribute('tabindex');
});
});
describe('aria-hidden', function() {
beforeEach(injectScopeAndCompiler);
it('should attach aria-hidden to ng-show', function() {
compileElement('<div ng-show="val"></div>');
scope.$apply('val = false');
expect(element.attr('aria-hidden')).toBe('true');
scope.$apply('val = true');
expect(element.attr('aria-hidden')).toBe('false');
});
it('should attach aria-hidden to ng-hide', function() {
compileElement('<div ng-hide="val"></div>');
scope.$apply('val = false');
expect(element.attr('aria-hidden')).toBe('false');
scope.$apply('val = true');
expect(element.attr('aria-hidden')).toBe('true');
});
it('should not change aria-hidden if it is already present on ng-show', function() {
compileElement('<div ng-show="val" aria-hidden="userSetValue"></div>');
expect(element.attr('aria-hidden')).toBe('userSetValue');
scope.$apply('val = true');
expect(element.attr('aria-hidden')).toBe('userSetValue');
});
it('should not change aria-hidden if it is already present on ng-hide', function() {
compileElement('<div ng-hide="val" aria-hidden="userSetValue"></div>');
expect(element.attr('aria-hidden')).toBe('userSetValue');
scope.$apply('val = true');
expect(element.attr('aria-hidden')).toBe('userSetValue');
});
it('should always set aria-hidden to a boolean value', function() {
compileElement('<div ng-hide="val"></div>');
scope.$apply('val = "test angular"');
expect(element.attr('aria-hidden')).toBe('true');
scope.$apply('val = null');
expect(element.attr('aria-hidden')).toBe('false');
scope.$apply('val = {}');
expect(element.attr('aria-hidden')).toBe('true');
compileElement('<div ng-show="val"></div>');
scope.$apply('val = "test angular"');
expect(element.attr('aria-hidden')).toBe('false');
scope.$apply('val = null');
expect(element.attr('aria-hidden')).toBe('true');
scope.$apply('val = {}');
expect(element.attr('aria-hidden')).toBe('false');
});
});
describe('aria-hidden when disabled', function() {
beforeEach(configAriaProvider({
ariaHidden: false
}));
beforeEach(injectScopeAndCompiler);
it('should not attach aria-hidden', function() {
scope.$apply('val = false');
compileElement('<div ng-show="val"></div>');
expect(element.attr('aria-hidden')).toBeUndefined();
compileElement('<div ng-hide="val"></div>');
expect(element.attr('aria-hidden')).toBeUndefined();
});
});
describe('aria-checked', function() {
beforeEach(injectScopeAndCompiler);
it('should not attach itself to native input type="checkbox"', function() {
compileElement('<input type="checkbox" ng-model="val">');
scope.$apply('val = true');
expect(element.attr('aria-checked')).toBeUndefined();
scope.$apply('val = false');
expect(element.attr('aria-checked')).toBeUndefined();
});
it('should attach itself to custom checkbox', function() {
compileElement('<div role="checkbox" ng-model="val"></div>');
scope.$apply('val = "checked"');
expect(element.attr('aria-checked')).toBe('true');
scope.$apply('val = null');
expect(element.attr('aria-checked')).toBe('false');
});
it('should use `$isEmpty()` to determine if the checkbox is checked',
function() {
compileElement('<div role="checkbox" ng-model="val"></div>');
var ctrl = element.controller('ngModel');
ctrl.$isEmpty = function(value) {
return value === 'not-checked';
};
scope.$apply('val = true');
expect(ctrl.$modelValue).toBe(true);
expect(element.attr('aria-checked')).toBe('true');
scope.$apply('val = false');
expect(ctrl.$modelValue).toBe(false);
expect(element.attr('aria-checked')).toBe('true');
scope.$apply('val = "not-checked"');
expect(ctrl.$modelValue).toBe('not-checked');
expect(element.attr('aria-checked')).toBe('false');
scope.$apply('val = "checked"');
expect(ctrl.$modelValue).toBe('checked');
expect(element.attr('aria-checked')).toBe('true');
}
);
it('should not handle native checkbox with ngChecked', function() {
var element = $compile('<input type="checkbox" ng-checked="val">')(scope);
scope.$apply('val = true');
expect(element.attr('aria-checked')).toBeUndefined();
scope.$apply('val = false');
expect(element.attr('aria-checked')).toBeUndefined();
});
it('should handle custom checkbox with ngChecked', function() {
var element = $compile('<div role="checkbox" ng-checked="val">')(scope);
scope.$apply('val = true');
expect(element.eq(0).attr('aria-checked')).toBe('true');
scope.$apply('val = false');
expect(element.eq(0).attr('aria-checked')).toBe('false');
});
it('should not attach to native input type="radio"', function() {
var element = $compile('<input type="radio" ng-model="val" value="one">' +
'<input type="radio" ng-model="val" value="two">')(scope);
scope.$apply('val=\'one\'');
expect(element.eq(0).attr('aria-checked')).toBeUndefined();
expect(element.eq(1).attr('aria-checked')).toBeUndefined();
scope.$apply('val=\'two\'');
expect(element.eq(0).attr('aria-checked')).toBeUndefined();
expect(element.eq(1).attr('aria-checked')).toBeUndefined();
});
it('should attach to custom radio controls', function() {
var element = $compile('<div role="radio" ng-model="val" value="one"></div>' +
'<div role="radio" ng-model="val" value="two"></div>')(scope);
scope.$apply('val=\'one\'');
expect(element.eq(0).attr('aria-checked')).toBe('true');
expect(element.eq(1).attr('aria-checked')).toBe('false');
scope.$apply('val=\'two\'');
expect(element.eq(0).attr('aria-checked')).toBe('false');
expect(element.eq(1).attr('aria-checked')).toBe('true');
});
it('should handle custom radios with integer model values', function() {
var element = $compile('<div role="radio" ng-model="val" value="0"></div>' +
'<div role="radio" ng-model="val" value="1"></div>')(scope);
scope.$apply('val=0');
expect(element.eq(0).attr('aria-checked')).toBe('true');
expect(element.eq(1).attr('aria-checked')).toBe('false');
scope.$apply('val=1');
expect(element.eq(0).attr('aria-checked')).toBe('false');
expect(element.eq(1).attr('aria-checked')).toBe('true');
});
it('should handle radios with boolean model values using ngValue', function() {
var element = $compile('<div role="radio" ng-model="val" ng-value="valExp"></div>' +
'<div role="radio" ng-model="val" ng-value="valExp2"></div>')(scope);
scope.$apply(function() {
scope.valExp = true;
scope.valExp2 = false;
scope.val = true;
});
expect(element.eq(0).attr('aria-checked')).toBe('true');
expect(element.eq(1).attr('aria-checked')).toBe('false');
scope.$apply('val = false');
expect(element.eq(0).attr('aria-checked')).toBe('false');
expect(element.eq(1).attr('aria-checked')).toBe('true');
});
it('should attach itself to role="menuitemradio"', function() {
scope.val = 'one';
compileElement('<div role="menuitemradio" ng-model="val" value="one"></div>');
expect(element.attr('aria-checked')).toBe('true');
scope.$apply('val = \'two\'');
expect(element.attr('aria-checked')).toBe('false');
});
it('should attach itself to role="menuitemcheckbox"', function() {
compileElement('<div role="menuitemcheckbox" ng-model="val"></div>');
scope.$apply('val = "checked"');
expect(element.attr('aria-checked')).toBe('true');
scope.$apply('val = null');
expect(element.attr('aria-checked')).toBe('false');
});
it('should not attach itself if an aria-checked value is already present', function() {
var element = [
$compile('<div role=\'radio\' ng-model=\'val\' value=\'{{val3}}\' aria-checked=\'userSetValue\'></div>')(scope),
$compile('<div role=\'menuitemradio\' ng-model=\'val\' value=\'{{val3}}\' aria-checked=\'userSetValue\'></div>')(scope),
$compile('<div role=\'checkbox\' checked=\'checked\' aria-checked=\'userSetValue\'></div>')(scope),
$compile('<div role=\'menuitemcheckbox\' checked=\'checked\' aria-checked=\'userSetValue\'></div>')(scope)
];
scope.$apply('val1=true;val2=\'one\';val3=\'1\'');
expectAriaAttrOnEachElement(element, 'aria-checked', 'userSetValue');
});
});
describe('roles for custom inputs', function() {
beforeEach(injectScopeAndCompiler);
it('should add missing role="button" to custom input', function() {
compileElement('<div ng-click="someFunction()"></div>');
expect(element.attr('role')).toBe('button');
});
it('should not add role="button" to anchor', function() {
compileElement('<a ng-click="someFunction()"></a>');
expect(element.attr('role')).not.toBe('button');
});
it('should add missing role="checkbox" to custom input', function() {
compileElement('<div type="checkbox" ng-model="val"></div>');
expect(element.attr('role')).toBe('checkbox');
});
it('should not add a role to a native checkbox', function() {
compileElement('<input type="checkbox" ng-model="val"/>');
expect(element.attr('role')).toBeUndefined();
});
it('should add missing role="radio" to custom input', function() {
compileElement('<div type="radio" ng-model="val"></div>');
expect(element.attr('role')).toBe('radio');
});
it('should not add a role to a native radio button', function() {
compileElement('<input type="radio" ng-model="val"/>');
expect(element.attr('role')).toBeUndefined();
});
it('should add missing role="slider" to custom input', function() {
compileElement('<div type="range" ng-model="val"></div>');
expect(element.attr('role')).toBe('slider');
});
it('should not add a role to a native range input', function() {
compileElement('<input type="range" ng-model="val"/>');
expect(element.attr('role')).toBeUndefined();
});
they('should not add role to native $prop controls', {
input: '<input type="text" ng-model="val">',
select: '<select type="checkbox" ng-model="val"></select>',
textarea: '<textarea type="checkbox" ng-model="val"></textarea>',
button: '<button ng-click="doClick()"></button>',
summary: '<summary ng-click="doClick()"></summary>',
details: '<details ng-click="doClick()"></details>',
a: '<a ng-click="doClick()"></a>'
}, function(tmpl) {
var element = $compile(tmpl)(scope);
expect(element.attr('role')).toBeUndefined();
});
});
describe('aria-checked when disabled', function() {
beforeEach(configAriaProvider({
ariaChecked: false
}));
beforeEach(injectScopeAndCompiler);
it('should not attach aria-checked', function() {
compileElement('<div role=\'radio\' ng-model=\'val\' value=\'{{val}}\'></div>');
expect(element.attr('aria-checked')).toBeUndefined();
compileElement('<div role=\'menuitemradio\' ng-model=\'val\' value=\'{{val}}\'></div>');
expect(element.attr('aria-checked')).toBeUndefined();
compileElement('<div role=\'checkbox\' checked=\'checked\'></div>');
expect(element.attr('aria-checked')).toBeUndefined();
compileElement('<div role=\'menuitemcheckbox\' checked=\'checked\'></div>');
expect(element.attr('aria-checked')).toBeUndefined();
});
});
describe('aria-disabled', function() {
beforeEach(injectScopeAndCompiler);
they('should not attach itself to native $prop controls', {
input: '<input ng-disabled="val">',
textarea: '<textarea ng-disabled="val"></textarea>',
select: '<select ng-disabled="val"></select>',
button: '<button ng-disabled="val"></button>'
}, function(tmpl) {
var element = $compile(tmpl)(scope);
scope.$apply('val = true');
expect(element.attr('disabled')).toBeDefined();
expect(element.attr('aria-disabled')).toBeUndefined();
});
it('should attach itself to custom controls', function() {
compileElement('<div ng-disabled="val"></div>');
expect(element.attr('aria-disabled')).toBe('false');
scope.$apply('val = true');
expect(element.attr('aria-disabled')).toBe('true');
});
it('should not attach itself if an aria-disabled attribute is already present', function() {
compileElement('<div ng-disabled="val" aria-disabled="userSetValue"></div>');
expect(element.attr('aria-disabled')).toBe('userSetValue');
});
it('should always set aria-disabled to a boolean value', function() {
compileElement('<div ng-disabled="val"></div>');
scope.$apply('val = "test angular"');
expect(element.attr('aria-disabled')).toBe('true');
scope.$apply('val = null');
expect(element.attr('aria-disabled')).toBe('false');
scope.$apply('val = {}');
expect(element.attr('aria-disabled')).toBe('true');
});
});
describe('aria-disabled when disabled', function() {
beforeEach(configAriaProvider({
ariaDisabled: false
}));
beforeEach(injectScopeAndCompiler);
it('should not attach aria-disabled', function() {
compileElement('<div ng-disabled="val"></div>');
scope.$apply('val = true');
expect(element.attr('aria-disabled')).toBeUndefined();
});
});
describe('aria-invalid', function() {
beforeEach(injectScopeAndCompiler);
it('should attach aria-invalid to input', function() {
compileElement('<input ng-model="txtInput" ng-minlength="10">');
scope.$apply('txtInput=\'LTten\'');
expect(element.attr('aria-invalid')).toBe('true');
scope.$apply('txtInput=\'morethantencharacters\'');
expect(element.attr('aria-invalid')).toBe('false');
});
it('should attach aria-invalid to custom controls', function() {
compileElement('<div ng-model="txtInput" role="textbox" ng-minlength="10"></div>');
scope.$apply('txtInput=\'LTten\'');
expect(element.attr('aria-invalid')).toBe('true');
scope.$apply('txtInput=\'morethantencharacters\'');
expect(element.attr('aria-invalid')).toBe('false');
});
it('should not attach itself if aria-invalid is already present', function() {
compileElement('<input ng-model="txtInput" ng-minlength="10" aria-invalid="userSetValue">');
scope.$apply('txtInput=\'LTten\'');
expect(element.attr('aria-invalid')).toBe('userSetValue');
});
it('should not attach if input is type="hidden"', function() {
compileElement('<input type="hidden" ng-model="txtInput">');
expect(element.attr('aria-invalid')).toBeUndefined();
});
it('should attach aria-invalid to custom control that is type="hidden"', function() {
compileElement('<div ng-model="txtInput" type="hidden" role="textbox" ng-minlength="10"></div>');
scope.$apply('txtInput=\'LTten\'');
expect(element.attr('aria-invalid')).toBe('true');
scope.$apply('txtInput=\'morethantencharacters\'');
expect(element.attr('aria-invalid')).toBe('false');
});
});
describe('aria-invalid when disabled', function() {
beforeEach(configAriaProvider({
ariaInvalid: false
}));
beforeEach(injectScopeAndCompiler);
it('should not attach aria-invalid if the option is disabled', function() {
scope.$apply('txtInput=\'LTten\'');
compileElement('<input ng-model="txtInput" ng-minlength="10">');
expect(element.attr('aria-invalid')).toBeUndefined();
});
});
describe('aria-readonly', function() {
beforeEach(injectScopeAndCompiler);
they('should not attach itself to native $prop controls', {
input: '<input ng-readonly="val">',
textarea: '<textarea ng-readonly="val"></textarea>',
select: '<select ng-readonly="val"></select>',
button: '<button ng-readonly="val"></button>'
}, function(tmpl) {
var element = $compile(tmpl)(scope);
scope.$apply('val = true');
expect(element.attr('readonly')).toBeDefined();
expect(element.attr('aria-readonly')).toBeUndefined();
});
it('should attach itself to custom controls', function() {
compileElement('<div ng-readonly="val"></div>');
expect(element.attr('aria-readonly')).toBe('false');
scope.$apply('val = true');
expect(element.attr('aria-readonly')).toBe('true');
});
it('should not attach itself if an aria-readonly attribute is already present', function() {
compileElement('<div ng-readonly="val" aria-readonly="userSetValue"></div>');
expect(element.attr('aria-readonly')).toBe('userSetValue');
});
it('should always set aria-readonly to a boolean value', function() {
compileElement('<div ng-readonly="val"></div>');
scope.$apply('val = "test angular"');
expect(element.attr('aria-readonly')).toBe('true');
scope.$apply('val = null');
expect(element.attr('aria-readonly')).toBe('false');
scope.$apply('val = {}');
expect(element.attr('aria-readonly')).toBe('true');
});
});
describe('aria-readonly when disabled', function() {
beforeEach(configAriaProvider({
ariaReadonly: false
}));
beforeEach(injectScopeAndCompiler);
it('should not add the aria-readonly attribute', function() {
compileElement('<input ng-model=\'val\' readonly>');
expect(element.attr('aria-readonly')).toBeUndefined();
compileElement('<div ng-model=\'val\' ng-readonly=\'true\'></div>');
expect(element.attr('aria-readonly')).toBeUndefined();
});
});
describe('aria-required', function() {
beforeEach(injectScopeAndCompiler);
it('should not attach to input', function() {
compileElement('<input ng-model="val" required>');
expect(element.attr('aria-required')).toBeUndefined();
});
it('should attach to custom controls with ngModel and required', function() {
compileElement('<div ng-model="val" role="checkbox" required></div>');
expect(element.attr('aria-required')).toBe('true');
});
it('should set aria-required to false when ng-required is false', function() {
compileElement('<div role=\'checkbox\' ng-required=\'false\' ng-model=\'val\'></div>');
expect(element.attr('aria-required')).toBe('false');
});
it('should attach to custom controls with ngRequired', function() {
compileElement('<div role="checkbox" ng-model="val" ng-required="true"></div>');
expect(element.attr('aria-required')).toBe('true');
});
it('should not attach itself if aria-required is already present', function() {
compileElement('<div role=\'checkbox\' ng-model=\'val\' ng-required=\'true\' aria-required=\'userSetValue\'></div>');
expect(element.attr('aria-required')).toBe('userSetValue');
});
});
describe('aria-required when disabled', function() {
beforeEach(configAriaProvider({
ariaRequired: false
}));
beforeEach(injectScopeAndCompiler);
it('should not add the aria-required attribute', function() {
compileElement('<input ng-model=\'val\' required>');
expect(element.attr('aria-required')).toBeUndefined();
compileElement('<div ng-model=\'val\' ng-required=\'true\'></div>');
expect(element.attr('aria-required')).toBeUndefined();
});
});
describe('aria-value', function() {
beforeEach(injectScopeAndCompiler);
it('should attach to input type="range"', function() {
var element = [
$compile('<input type="range" ng-model="val" min="0" max="100">')(scope),
$compile('<div role="progressbar" min="0" max="100" ng-model="val">')(scope),
$compile('<div role="slider" min="0" max="100" ng-model="val">')(scope)
];
scope.$apply('val = 50');
expectAriaAttrOnEachElement(element, 'aria-valuenow', '50');
expectAriaAttrOnEachElement(element, 'aria-valuemin', '0');
expectAriaAttrOnEachElement(element, 'aria-valuemax', '100');
scope.$apply('val = 90');
expectAriaAttrOnEachElement(element, 'aria-valuenow', '90');
});
it('should not attach if aria-value* is already present', function() {
var element = [
$compile('<input type="range" ng-model="val" min="0" max="100" aria-valuenow="userSetValue1" aria-valuemin="userSetValue2" aria-valuemax="userSetValue3">')(scope),
$compile('<div role="progressbar" min="0" max="100" ng-model="val" aria-valuenow="userSetValue1" aria-valuemin="userSetValue2" aria-valuemax="userSetValue3">')(scope),
$compile('<div role="slider" min="0" max="100" ng-model="val" aria-valuenow="userSetValue1" aria-valuemin="userSetValue2" aria-valuemax="userSetValue3">')(scope)
];
scope.$apply('val = 50');
expectAriaAttrOnEachElement(element, 'aria-valuenow', 'userSetValue1');
expectAriaAttrOnEachElement(element, 'aria-valuemin', 'userSetValue2');
expectAriaAttrOnEachElement(element, 'aria-valuemax', 'userSetValue3');
});
it('should update `aria-valuemin/max` when `min/max` changes dynamically', function() {
scope.$apply('min = 25; max = 75');
compileElement('<input type="range" ng-model="val" min="{{min}}" max="{{max}}" />');
expect(element.attr('aria-valuemin')).toBe('25');
expect(element.attr('aria-valuemax')).toBe('75');
scope.$apply('min = 0');
expect(element.attr('aria-valuemin')).toBe('0');
scope.$apply('max = 100');
expect(element.attr('aria-valuemax')).toBe('100');
});
it('should update `aria-valuemin/max` when `ng-min/ng-max` changes dynamically', function() {
scope.$apply('min = 25; max = 75');
compileElement('<input type="range" ng-model="val" ng-min="min" ng-max="max" />');
expect(element.attr('aria-valuemin')).toBe('25');
expect(element.attr('aria-valuemax')).toBe('75');
scope.$apply('min = 0');
expect(element.attr('aria-valuemin')).toBe('0');
scope.$apply('max = 100');
expect(element.attr('aria-valuemax')).toBe('100');
});
});
describe('announcing ngMessages', function() {
beforeEach(injectScopeAndCompiler);
it('should attach aria-live', function() {
var element = [
$compile('<div ng-messages="myForm.myName.$error">')(scope)
];
expectAriaAttrOnEachElement(element, 'aria-live', 'assertive');
});
});
describe('aria-value when disabled', function() {
beforeEach(configAriaProvider({
ariaValue: false
}));
beforeEach(injectScopeAndCompiler);
it('should not attach itself', function() {
scope.$apply('val = 50');
compileElement('<input type="range" ng-model="val" min="0" max="100">');
expect(element.attr('aria-valuenow')).toBeUndefined();
expect(element.attr('aria-valuemin')).toBeUndefined();
expect(element.attr('aria-valuemax')).toBeUndefined();
compileElement('<div role="progressbar" min="0" max="100" ng-model="val">');
expect(element.attr('aria-valuenow')).toBeUndefined();
expect(element.attr('aria-valuemin')).toBeUndefined();
expect(element.attr('aria-valuemax')).toBeUndefined();
});
});
describe('tabindex', function() {
beforeEach(injectScopeAndCompiler);
they('should not attach to native control $prop', {
'button': '<button ng-click=\'something\'></button>',
'a': '<a ng-href=\'#/something\'>',
'input[text]': '<input type=\'text\' ng-model=\'val\'>',
'input[radio]': '<input type=\'radio\' ng-model=\'val\'>',
'input[checkbox]': '<input type=\'checkbox\' ng-model=\'val\'>',
'textarea': '<textarea ng-model=\'val\'></textarea>',
'select': '<select ng-model=\'val\'></select>',
'details': '<details ng-model=\'val\'></details>'
}, function(html) {
compileElement(html);
expect(element.attr('tabindex')).toBeUndefined();
});
it('should not attach to random ng-model elements', function() {
compileElement('<div ng-model="val"></div>');
expect(element.attr('tabindex')).toBeUndefined();
});
it('should attach tabindex to custom inputs', function() {
compileElement('<div role="checkbox" ng-model="val"></div>');
expect(element.attr('tabindex')).toBe('0');
compileElement('<div role="slider" ng-model="val"></div>');
expect(element.attr('tabindex')).toBe('0');
});
it('should attach to ng-click and ng-dblclick', function() {
compileElement('<div ng-click="someAction()"></div>');
expect(element.attr('tabindex')).toBe('0');
compileElement('<div ng-dblclick="someAction()"></div>');
expect(element.attr('tabindex')).toBe('0');
});
it('should not attach tabindex if it is already on an element', function() {
compileElement('<div role="button" tabindex="userSetValue"></div>');
expect(element.attr('tabindex')).toBe('userSetValue');
compileElement('<div role="checkbox" tabindex="userSetValue"></div>');
expect(element.attr('tabindex')).toBe('userSetValue');
compileElement('<div ng-click="someAction()" tabindex="userSetValue"></div>');
expect(element.attr('tabindex')).toBe('userSetValue');
compileElement('<div ng-dblclick="someAction()" tabindex="userSetValue"></div>');
expect(element.attr('tabindex')).toBe('userSetValue');
});
});
describe('accessible actions', function() {
var clickEvents;
beforeEach(injectScopeAndCompiler);
beforeEach(function() {
clickEvents = [];
scope.onClick = jasmine.createSpy('onClick').and.callFake(function(evt) {
var nodeName = evt ? evt.target.nodeName.toLowerCase() : '';
var prevented = !!(evt && evt.isDefaultPrevented());
clickEvents.push(nodeName + '(' + prevented + ')');
});
});
it('should trigger a click from the keyboard (and prevent default action)', function() {
compileElement(
'<section>' +
'<div ng-click="onClick($event)"></div>' +
'<ul><li ng-click="onClick($event)"></li></ul>' +
'</section>');
var divElement = element.find('div');
var liElement = element.find('li');
divElement.triggerHandler({type: 'keydown', keyCode: 13});
liElement.triggerHandler({type: 'keydown', keyCode: 13});
divElement.triggerHandler({type: 'keydown', keyCode: 32});
liElement.triggerHandler({type: 'keydown', keyCode: 32});
expect(clickEvents).toEqual(['div(true)', 'li(true)', 'div(true)', 'li(true)']);
});
it('should trigger a click in browsers that provide `event.which` instead of `event.keyCode`',
function() {
compileElement(
'<section>' +
'<div ng-click="onClick($event)"></div>' +
'<ul><li ng-click="onClick($event)"></li></ul>' +
'</section>');
var divElement = element.find('div');
var liElement = element.find('li');
divElement.triggerHandler({type: 'keydown', which: 13});
liElement.triggerHandler({type: 'keydown', which: 13});
divElement.triggerHandler({type: 'keydown', which: 32});
liElement.triggerHandler({type: 'keydown', which: 32});
expect(clickEvents).toEqual(['div(true)', 'li(true)', 'div(true)', 'li(true)']);
}
);
it('should not prevent default keyboard action if the target element has editable content',
inject(function($document) {
// Note:
// `contenteditable` is an enumarated (not a boolean) attribute (see
// https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/contenteditable).
// We need to check the following conditions:
// - No attribute.
// - Value: ""
// - Value: "true"
// - Value: "false"
function eventFor(keyCode) {
return {bubbles: true, cancelable: true, keyCode: keyCode};
}
compileElement(
'<section>' +
// No attribute.
'<div id="no-attribute">' +
'<div ng-click="onClick($event)"></div>' +
'<ul ng-click="onClick($event)"><li></li></ul>' +
'</div>' +
// Value: ""