This repository has been archived by the owner on Sep 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
aria.spec.js
185 lines (132 loc) · 6.84 KB
/
aria.spec.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
describe('$mdAria service', function() {
beforeEach(module('material.core'));
describe('expecting attributes', function() {
it('should warn if an invalid element is specified', inject(function($compile, $rootScope, $log, $mdAria) {
spyOn($log, 'warn');
var target = $compile('<div></div>')($rootScope);
$mdAria.expect(null,'aria-label');
expect($log.warn).not.toHaveBeenCalled();
}));
it('should warn if element is missing attribute', inject(function($compile, $rootScope, $log, $mdAria) {
spyOn($log, 'warn');
var button = $compile('<button><md-icon></md-icon></button>')($rootScope);
$mdAria.expect(button, 'aria-label');
expect($log.warn).toHaveBeenCalled();
}));
it('should warn if element is missing attribute value', inject(function($compile, $rootScope, $log, $mdAria) {
spyOn($log, 'warn');
var button = $compile('<button aria-label><md-icon></md-icon></button>')($rootScope);
$mdAria.expect(button, 'aria-label');
expect($log.warn).toHaveBeenCalled();
}));
it('should warn if element is empty attribute', inject(function($compile, $rootScope, $log, $mdAria) {
spyOn($log, 'warn');
var button = $compile('<button aria-label=""><md-icon></md-icon></button>')($rootScope);
$mdAria.expect(button, 'aria-label');
expect($log.warn).toHaveBeenCalled();
}));
it('should not warn if element has text', inject(function($compile, $rootScope, $log, $mdAria) {
spyOn($log, 'warn');
var button = $compile('<button>Text</button>')($rootScope);
$mdAria.expectWithoutText(button, 'aria-label');
expect($log.warn).not.toHaveBeenCalled();
}));
it('should warn if control is missing text', inject(function($compile, $rootScope, $log, $mdAria) {
spyOn($log, 'warn');
var radioButton = $compile('<md-radio-button>Text</md-radio-button>')($rootScope);
$mdAria.expectWithText(radioButton, 'aria-label');
expect($log.warn).not.toHaveBeenCalled();
}));
it('should not warn if child element has attribute', inject(function($compile, $rootScope, $log, $mdAria) {
spyOn($log, 'warn');
var button = $compile('<button><md-icon aria-label="text"></md-icon></button>')($rootScope);
$mdAria.expect(button, 'aria-label');
expect($log.warn).not.toHaveBeenCalled();
}));
it('should warn if child with attribute is hidden', inject(function($compile, $rootScope, $log, $mdAria) {
spyOn($log, 'warn');
var container = angular.element(document.body);
var button = $compile('<button><md-icon aria-label="text" style="display:none;"></md-icon></button>')($rootScope);
container.append(button);
$mdAria.expect(button, 'aria-label');
expect($log.warn).toHaveBeenCalled();
button.remove();
}));
it('should correctly retrieve the aria-label text', inject(function($compile, $rootScope, $mdAria) {
var container = $compile(
'<div>' +
'PLAIN' +
'<span>SPAN</span>' +
'<div>DIV</div>' +
'</div>'
)($rootScope);
$mdAria.expectWithText(container, 'aria-label');
expect(container[0].textContent).toBe('PLAINSPANDIV');
expect(container.attr('aria-label')).toBe('PLAINSPANDIV');
}));
it('should ignore aria-hidden texts when retrieving aria-label', inject(function($compile, $rootScope, $mdAria) {
var container = $compile(
'<div>' +
'PLAIN' +
'<span aria-hidden="true">SPAN</span>' +
'<div aria-hidden="true">DIV</div>' +
'</div>'
)($rootScope);
$mdAria.expectWithText(container, 'aria-label');
expect(container[0].textContent).toBe('PLAINSPANDIV');
expect(container.attr('aria-label')).toBe('PLAIN');
}));
});
describe('aria label checks', function() {
it('should detect if element has valid aria-label string', inject(function($compile, $rootScope, $log, $mdAria) {
var container = $compile('<div aria-label="hello"></div>')($rootScope);
expect($mdAria.hasAriaLabel(container)).toBe(true);
}));
it('should detect if element has valid aria-labelledby string', inject(function($compile, $rootScope, $log, $mdAria) {
var container = $compile('<div aria-labelledby="myOtherElementId"></div>')($rootScope);
expect($mdAria.hasAriaLabel(container)).toBe(true);
}));
it('should detect if element has valid aria-describedby string', inject(function($compile, $rootScope, $log, $mdAria) {
var container = $compile('<div aria-describedby="myOtherElementId"></div>')($rootScope);
expect($mdAria.hasAriaLabel(container)).toBe(true);
}));
});
describe('aria label parent checks', function() {
it('should detect if parent of element has valid aria label', inject(function($compile, $rootScope, $log, $mdAria) {
var container = $compile('<button aria-label="hello"><img></img></button>')($rootScope);
var imgElement = container.find('img');
expect($mdAria.hasAriaLabel(imgElement)).toBe(false);
expect($mdAria.parentHasAriaLabel(imgElement)).toBe(true);
}));
it('should prevent aria-label on a parent of element based on role', inject(function($compile, $rootScope, $log, $mdAria) {
var container = $compile('<button role="log" aria-label="hello"><img></img></button>')($rootScope);
var imgElement = container.find('img');
expect($mdAria.hasAriaLabel(imgElement)).toBe(false);
expect($mdAria.parentHasAriaLabel(imgElement)).toBe(false);
}));
it('should prevent aria-label on a parent of element based on tagName', inject(function($compile, $rootScope, $log, $mdAria) {
var container = $compile('<span aria-label="hello"><img></img></span>')($rootScope);
var imgElement = container.find('img');
expect($mdAria.hasAriaLabel(imgElement)).toBe(false);
expect($mdAria.parentHasAriaLabel(imgElement)).toBe(false);
}));
it('should detect if second parent of element has valid aria label', inject(function($compile, $rootScope, $log, $mdAria) {
var container = $compile('<button aria-label="hello"><div><img></img></div></button>')($rootScope);
var imgElement = container.find('img');
expect($mdAria.hasAriaLabel(imgElement)).toBe(false);
expect($mdAria.parentHasAriaLabel(imgElement)).toBe(false);
expect($mdAria.parentHasAriaLabel(imgElement, 2)).toBe(true);
}));
});
describe('with disabled warnings', function() {
beforeEach(module('material.core', function($mdAriaProvider) {
$mdAriaProvider.disableWarnings();
}));
it('should not warn if warnings are disabled', inject(function($compile, $rootScope, $log, $mdAria) {
spyOn($log, 'warn');
var button = $compile('<button aria-label><md-icon></md-icon></button>')($rootScope);
$mdAria.expect(button, 'aria-label');
expect($log.warn).not.toHaveBeenCalled();
}));
})
});