-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharia.test.js
342 lines (242 loc) · 12.7 KB
/
aria.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
/* global QUnit, aria */
// http://api.qunitjs.com/category/assert/
QUnit.fixture = () => document.getElementById('qunit-fixture');
QUnit.testDone(() => QUnit.fixture().aria = null);
QUnit.test('aria() returns an aria instance when given an element or a valid element ID, otherwise null', assert => {
assert.ok(aria(QUnit.fixture()) instanceof aria, 'Returns an aria instance when given an element');
assert.ok(aria('qunit-fixture') instanceof aria, 'Returns an aria instance when given a valid element ID');
assert.strictEqual(aria(), null, 'Returns null when given no parameters');
assert.strictEqual(aria('invalid'), null, 'Returns null when given an invalid element ID');
});
QUnit.test('aria() caches the aria instance', assert => {
let element = QUnit.fixture();
assert.ok(aria(element) === aria(element), 'Returns the same aria instance when given the same element again');
});
QUnit.test('Only properties can be set whose corresponding attributes are defined in aria.attributes', assert => {
aria.attributes.defined = {};
let ariaInstance = aria(QUnit.fixture());
assert.ok(ariaInstance.hasOwnProperty('defined'), 'An aria instance has a property which is defined');
assert.ok(Object.getOwnPropertyDescriptor(ariaInstance, 'defined').set, 'That property can be set');
ariaInstance.undefined = 'value';
assert.notOk(ariaInstance.hasOwnProperty('undefined'), 'An undefined property cannot be added');
});
QUnit.test('Properties are proxies to the corresponding aria-prefixed element attributes', assert => {
aria.attributes.label = {};
let element = QUnit.fixture();
let ariaInstance = aria(element);
element.setAttribute('aria-label', 'value');
assert.equal(ariaInstance.label, 'value', 'The value of the attribute is also the property value');
ariaInstance.label = 'another value';
assert.equal(element.getAttribute('aria-label'), 'another value', 'When assigning the property, the attribute value is also set');
});
QUnit.test('Values are converted using the get() and set() functions defined the attribute definition', assert => {
aria.attributes.label = {
get: attributeValue => attributeValue.slice(1, -1),
set: value => `(${value})`
};
let element = QUnit.fixture();
let ariaInstance = aria(element);
element.setAttribute('aria-label', '(value)');
assert.equal(ariaInstance.label, 'value', 'The attribute value is converted using get() before being returned as property value');
ariaInstance.label = 'another value';
assert.equal(element.getAttribute('aria-label'), '(another value)', 'A value assigned to the property is converted using set() before being set as attribute value');
});
QUnit.test('When assigning null (or undefined) to the property, the element attribute is removed', assert => {
aria.attributes.label = {};
let element = QUnit.fixture();
element.setAttribute('aria-label', 'value');
aria(element).label = null;
assert.notOk(element.hasAttribute('aria-label'), 'The attribute was removed');
});
QUnit.test('Attributes are mapped to their type', (assert) => {
let ariaInstance = aria(QUnit.fixture());
// control samples
assert.strictEqual(ariaInstance.hidden, false, 'Sample 1');
assert.deepEqual(ariaInstance.dropeffect, [ 'none' ], 'Sample 2');
});
QUnit.module('Error handling', () => {
aria.attributes.errorTest = {
get: (attributeValue) => {
if (attributeValue == null) {
return 'default';
}
if (attributeValue == 'error') {
throw new Error();
}
throw new TypeError();
},
set: function (value) {
if (value == 'error') {
throw new Error();
}
throw new TypeError();
}
};
var element = QUnit.fixture();
var ariaInstance = aria(element);
QUnit.test('get()', (assert) => {
element.setAttribute('aria-errorTest', 'invalid');
assert.strictEqual(ariaInstance.errorTest, 'default', 'Returns the default value for an invalid attribute value');
element.setAttribute('aria-errorTest', 'error');
assert.throws(() => ariaInstance.errorTest, Error, 'Throws errors which are not TypeErrors');
});
QUnit.test('set()', (assert) => {
element.setAttribute('aria-errortest', 'default');
ariaInstance.errorTest = 'invalid';
assert.strictEqual(element.getAttribute('aria-errortest'), 'default', 'Does not apply an invalid value');
assert.throws(() => ariaInstance.errorTest = 'error', Error, 'Throws errors which are not TypeErrors');
});
});
QUnit.module('aria.types.trueFalse', () => {
let type = aria.types.trueFalse();
QUnit.test('get()', assert => {
assert.strictEqual(type.get('true'), true, 'Returns true for "true"');
assert.strictEqual(type.get('false'), false, 'Returns false for "false"');
assert.strictEqual(type.get(null), false, 'Returns false for null');
assert.throws(() => type.get('string'), TypeError, 'Throws TypeError for any other string');
});
QUnit.test('set()', assert => {
assert.strictEqual(type.set(true), 'true', 'Returns "true" for true');
assert.strictEqual(type.set(false), 'false', 'Returns "false" for false');
assert.strictEqual(type.set(1), 'true', 'Returns "true" for truthy');
assert.strictEqual(type.set(0), 'false', 'Returns "false" for falsy');
});
});
QUnit.module('aria.types.tristate', () => {
let type = aria.types.tristate();
QUnit.test('get()', assert => {
assert.strictEqual(type.get('true'), true, 'Returns true for "true"');
assert.strictEqual(type.get('false'), false, 'Returns false for "false"');
assert.strictEqual(type.get('mixed'), 'mixed', 'Returns "mixed" for "mixed"');
assert.strictEqual(type.get(null), undefined, 'Returns undefined for null');
assert.throws(() => type.get('string'), TypeError, 'Throws TypeError for any other string');
});
QUnit.test('set()', assert => {
assert.strictEqual(type.set(true), 'true', 'Returns "true" for true');
assert.strictEqual(type.set(false), 'false', 'Returns "false" for false');
assert.strictEqual(type.set(1), 'true', 'Returns "true" for truthy');
assert.strictEqual(type.set(0), 'false', 'Returns "false" for falsy');
assert.strictEqual(type.set('mixed'), 'mixed', 'Returns "mixed" for "mixed"');
});
});
QUnit.module('aria.types.trueFalseUndefined', () => {
let type = aria.types.trueFalseUndefined();
QUnit.test('get()', assert => {
assert.strictEqual(type.get('true'), true, 'Returns true for "true"');
assert.strictEqual(type.get('false'), false, 'Returns false for "false"');
assert.strictEqual(type.get(null), undefined, 'Returns undefined for null');
assert.throws(() => type.get('string'), TypeError, 'Throws TypeError for any other string');
});
QUnit.test('set()', assert => {
assert.strictEqual(type.set(true), 'true', 'Returns "true" for true');
assert.strictEqual(type.set(false), 'false', 'Returns "false" for false');
assert.strictEqual(type.set(1), 'true', 'Returns "true" for truthy');
assert.strictEqual(type.set(0), 'false', 'Returns "false" for falsy');
});
});
QUnit.module('aria.types.idReference', () => {
let type = aria.types.idReference();
QUnit.test('get()', assert => {
assert.strictEqual(type.get('qunit-fixture'), QUnit.fixture(), 'Returns the referenced element');
assert.strictEqual(type.get('invalid'), null, 'Returns null for an invalid reference');
assert.strictEqual(type.get('41'), null, 'Returns null for an invalid ID');
assert.strictEqual(type.get(null), null, 'Returns null for null');
});
QUnit.test('set()', assert => {
assert.strictEqual(type.set('ID'), 'ID', 'Returns a given string');
assert.strictEqual(type.set(QUnit.fixture()), 'qunit-fixture', 'Returns the ID of a given element');
assert.strictEqual(type.set(document.body), '', 'Returns an empty string for an element without an ID');
assert.throws(() => type.set(true), TypeError, 'Throws TypeError for any other value');
});
});
QUnit.module('aria.types.integer', () => {
let type = aria.types.integer();
QUnit.test('get()', assert => {
assert.strictEqual(type.get('42'), 42, 'Returns the number parsed from a given string');
assert.strictEqual(type.get('3.6'), 3, 'Removes decimal places from that number');
assert.strictEqual(type.get('-3.6'), -3, 'Does this correctly also for negative numbers');
assert.ok(Number.isNaN(type.get(null)), 'Returns NaN for null');
});
QUnit.test('set()', assert => {
assert.strictEqual(type.set(42), '42', 'Returns a string representation of a given number');
assert.strictEqual(type.set( 3.6), '3', 'Removes decimal places from that number');
assert.strictEqual(type.set(-3.6), '-3', 'Does this correctly also for negative numbers');
assert.strictEqual(type.set('string'), 'NaN', 'Returns "NaN" for any other value');
});
});
QUnit.module('aria.types.number', () => {
let type = aria.types.number();
QUnit.test('get()', assert => {
assert.strictEqual(type.get('42'), 42, 'Returns the number parsed from a given string');
assert.ok(Number.isNaN(type.get(null)), 'Returns NaN for null');
});
QUnit.test('set()', assert => {
assert.strictEqual(type.set(42), '42', 'Returns a string representation of a given number');
assert.strictEqual(type.set('string'), 'NaN', 'Returns "NaN" for any other value');
});
});
QUnit.module('aria.types.string', () => {
let type = aria.types.string();
QUnit.test('get()', assert => {
assert.strictEqual(type.get('string'), 'string', 'Returns a given string');
assert.strictEqual(type.get(null), null, 'Returns null for null');
});
QUnit.test('set()', assert => {
assert.strictEqual(type.set('string'), 'string', 'Returns a given string');
assert.ok(typeof type.set(true) == 'string', 'Returns a string representation of any other value');
});
});
QUnit.module('aria.types.token', () => {
let type = aria.types.token([ 'foo', 'bar' ]);
let trueFalse = aria.types.token([ 'true', 'false' ]);
QUnit.test('get()', assert => {
assert.strictEqual(type.get('bar'), 'bar', 'Returns a given valid token');
assert.strictEqual(type.get(null), 'foo', 'Returns the default token for null');
assert.throws(() => type.get('true'), TypeError, 'Throws TypeError for any other string');
assert.strictEqual(trueFalse.get('true'), true, 'Returns true for "true", if latter is a valid token');
assert.strictEqual(trueFalse.get('false'), false, 'Returns false for "false", if latter is a valid token');
});
QUnit.test('set()', assert => {
assert.strictEqual(type.set('bar'), 'bar', 'Returns a given valid token');
assert.throws(() => type.get('true'), TypeError, 'Throws TypeError for any other string');
assert.strictEqual(trueFalse.set(true), 'true', 'Returns "true" for true, if first is a valid token');
assert.strictEqual(trueFalse.set(false), 'false', 'Returns "false" for false, if first is a valid token');
});
});
QUnit.module('aria.types.list', () => {
let type = aria.types.list(aria.types.idReference());
QUnit.test('get()', assert => {
assert.deepEqual(type.get('qunit-fixture invalid'), [ QUnit.fixture(), null ], 'Returns an array of values for space separated list');
assert.deepEqual(type.get(null), [], 'Returns an empty array for null');
assert.deepEqual(aria.types.list(aria.types.idReference(), [ document.body ]).get(null), [ document.body ],
'Returns a default value for null if one was given');
});
QUnit.test('set()', assert => {
assert.deepEqual(type.set([ QUnit.fixture(), document.body ]), 'qunit-fixture', 'Returns space separated list for an array of values');
assert.deepEqual(type.set(QUnit.fixture()), 'qunit-fixture', 'Works also for single values');
assert.deepEqual(type.set($(QUnit.fixture())), 'qunit-fixture', 'Works also for array-like values');
});
});
QUnit.module('jquery-aria', () => {
/* global $ */
QUnit.test('get', assert => {
assert.strictEqual($('<div aria-hidden="true">').aria('hidden'), true, 'Returns the value of the first element');
assert.strictEqual($().aria('hidden'), undefined, 'Returns undefined if the collection is empty');
assert.ok($('<div aria-activedescendant="qunit-fixture">').aria('activedescendant') instanceof $, 'Returns a jQuery collection when getting an ID reference');
assert.ok($('<div aria-controls="qunit-fixture">').aria('controls') instanceof $, 'Returns a jquery collection when getting an ID reference list');
});
QUnit.test('set', assert => {
assert.ok(
$('<div><div>')
.aria('hidden', true)
.toArray()
.every(element => aria(element).hidden),
'Sets the value on all elements in the collection');
assert.ok($('<div>').aria('hidden', value => ! value).aria('hidden'), 'Accepts a value function');
var fixture;
fixture = QUnit.fixture();
assert.ok($('<div>').aria('activedescendant', $(fixture)).aria('activedescendant').is(fixture), 'Accepts a jQuery collection when setting an ID reference');
fixture = [ QUnit.fixture(), QUnit.fixture() ];
assert.deepEqual($('<div>').aria('controls', $(fixture)).aria('controls').toArray(), fixture, 'Accepts a jQuery collection when setting an ID reference list');
});
});