-
Notifications
You must be signed in to change notification settings - Fork 123
/
assertions.ts
776 lines (677 loc) · 21.4 KB
/
assertions.ts
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
import exists from './assertions/exists';
import focused from './assertions/focused';
import notFocused from './assertions/not-focused';
import isChecked from './assertions/is-checked';
import isNotChecked from './assertions/is-not-checked';
import isRequired from './assertions/is-required';
import isNotRequired from './assertions/is-not-required';
import isVisible from './assertions/is-visible';
import isNotVisible from './assertions/is-not-visible';
import isDisabled from './assertions/is-disabled';
import elementToString from './helpers/element-to-string';
import collapseWhitespace from './helpers/collapse-whitespace';
export default class DOMAssertions {
constructor(private target: string | Element | null, private rootElement: Element, private testContext: Assert) {}
/**
* Assert an [HTMLElement][] (or multiple) matching the `selector` exists.
*
* @param {string?} message
*
* @example
* assert.dom('#title').exists();
*
* @see {@link #doesNotExist}
*/
exists(message?: string): void;
/**
* Assert an [HTMLElement][] (or multiple) matching the `selector` exists.
*
* @param {object?} options
* @param {string?} message
*
* @example
* assert.dom('.choice').exists({ count: 4 });
*
* @see {@link #doesNotExist}
*/
exists(options: { count: number }, message?: string): void;
/**
* Assert an [HTMLElement][] (or multiple) matching the `selector` exists.
*
* @param {object?} options
* @param {string?} message
*
* @example
* assert.dom('#title').exists();
* assert.dom('.choice').exists({ count: 4 });
*
* @see {@link #doesNotExist}
*/
exists(options: { count: number } | string, message?: string): void {
exists.call(this, options, message);
}
/**
* Assert an [HTMLElement][] matching the `selector` does not exists.
*
* @param {string?} message
*
* @example
* assert.dom('.should-not-exist').doesNotExist();
*
* @see {@link #exists}
*/
doesNotExist(message?: string): void {
exists.call(this, { count: 0 }, message);
}
/**
* Assert that the [HTMLElement][] or an [HTMLElement][] matching the
* `selector` is currently checked.
*
* @param {string?} message
*
* @example
* assert.dom('input.active').isChecked();
*
* @see {@link #isNotChecked}
*/
isChecked(message?: string): void {
isChecked.call(this, message);
}
/**
* Assert that the [HTMLElement][] or an [HTMLElement][] matching the
* `selector` is currently unchecked.
*
* @param {string?} message
*
* @example
* assert.dom('input.active').isNotChecked();
*
* @see {@link #isChecked}
*/
isNotChecked(message?: string): void {
isNotChecked.call(this, message);
}
/**
* Assert that the [HTMLElement][] or an [HTMLElement][] matching the
* `selector` is currently focused.
*
* @param {string?} message
*
* @example
* assert.dom('input.email').isFocused();
*
* @see {@link #isNotFocused}
*/
isFocused(message?: string): void {
focused.call(this, message);
}
/**
* Assert that the [HTMLElement][] or an [HTMLElement][] matching the
* `selector` is not currently focused.
*
* @param {string?} message
*
* @example
* assert.dom('input[type="password"]').isNotFocused();
*
* @see {@link #isFocused}
*/
isNotFocused(message?: string): void {
notFocused.call(this, message);
}
/**
* Assert that the [HTMLElement][] or an [HTMLElement][] matching the
* `selector` is currently required.
*
* @param {string?} message
*
* @example
* assert.dom('input[type="text"]').isRequired();
*
* @see {@link #isNotRequired}
*/
isRequired(message?: string): void {
isRequired.call(this, message);
}
/**
* Assert that the [HTMLElement][] or an [HTMLElement][] matching the
* `selector` is currently not required.
*
* @param {string?} message
*
* @example
* assert.dom('input[type="text"]').isNotRequired();
*
* @see {@link #isRequired}
*/
isNotRequired(message?: string): void {
isNotRequired.call(this, message);
}
/**
* Assert that the [HTMLElement][] or an [HTMLElement][] matching the
* `selector` exists and is visible.
*
* Visibility is determined by asserting that:
*
* - the element's offsetWidth and offsetHeight are non-zero
* - any of the element's DOMRect objects have a non-zero size
*
* Additionally, visibility in this case means that the element is visible on the page,
* but not necessarily in the viewport.
*
* @param {string?} message
*
* @example
* assert.dom('.foo').isVisible();
*
* @see {@link #isNotVisible}
*/
isVisible(message?: string): void {
isVisible.call(this, message);
}
/**
* Assert that the [HTMLElement][] or an [HTMLElement][] matching the
* `selector` does not exist or is not visible on the page.
*
* Visibility is determined by asserting that:
*
* - the element's offsetWidth or offsetHeight are zero
* - all of the element's DOMRect objects have a size of zero
*
* Additionally, visibility in this case means that the element is visible on the page,
* but not necessarily in the viewport.
*
* @param {string?} message
*
* @example
* assert.dom('.foo').isNotVisible();
*
* @see {@link #isVisible}
*/
isNotVisible(message?: string): void {
isNotVisible.call(this, message);
}
/**
* Assert that the [HTMLElement][] has an attribute with the provided `name`.
*
* @param {string} name
*
* @example
* assert.dom('input.password-input').hasAttribute('disabled');
*
* @see {@link #doesNotHaveAttribute}
*/
hasAttribute(name: string): void;
/**
* Assert that the [HTMLElement][] has an attribute with the provided `name`
* and checks if the attribute `value` matches the provided text or regular
* expression.
*
* @param {string} name
* @param {string|RegExp|object} value
* @param {string?} message
*
* @example
* assert.dom('input.password-input').hasAttribute('type', 'password');
*
* @see {@link #doesNotHaveAttribute}
*/
hasAttribute(name: string, value: string | RegExp | { any: true }, message?: string): void;
/**
* Assert that the [HTMLElement][] has an attribute with the provided `name`
* and optionally checks if the attribute `value` matches the provided text
* or regular expression.
*
* @param {string} name
* @param {string|RegExp|object?} value
* @param {string?} message
*
* @example
* assert.dom('input.password-input').hasAttribute('type', 'password');
*
* @see {@link #doesNotHaveAttribute}
*/
hasAttribute(name: string, value?: string | RegExp | { any: true }, message?: string): void {
let element = this.findTargetElement();
if (!element) return;
if (arguments.length === 1) {
value = { any: true };
}
let actualValue = element.getAttribute(name);
if (value instanceof RegExp) {
let result = value.test(actualValue);
let expected = `Element ${this.targetDescription} has attribute "${name}" with value matching ${value}`;
let actual = actualValue === null
? `Element ${this.targetDescription} does not have attribute "${name}"`
: `Element ${this.targetDescription} has attribute "${name}" with value ${JSON.stringify(actualValue)}`;
if (!message) {
message = expected;
}
this.pushResult({ result, actual, expected, message });
} else if ((value as { any: true }).any === true) {
let result = actualValue !== null;
let expected = `Element ${this.targetDescription} has attribute "${name}"`;
let actual = result ? expected : `Element ${this.targetDescription} does not have attribute "${name}"`;
if (!message) {
message = expected;
}
this.pushResult({ result, actual, expected, message });
} else {
let result = value === actualValue;
let expected = `Element ${this.targetDescription} has attribute "${name}" with value ${JSON.stringify(value)}`;
let actual = actualValue === null
? `Element ${this.targetDescription} does not have attribute "${name}"`
: `Element ${this.targetDescription} has attribute "${name}" with value ${JSON.stringify(actualValue)}`;
if (!message) {
message = expected;
}
this.pushResult({ result, actual, expected, message });
}
}
/**
* Assert that the [HTMLElement][] has no attribute with the provided `name`.
*
* **Aliases:** `hasNoAttribute`, `lacksAttribute`
*
* @param {string} name
* @param {string?} message
*
* @example
* assert.dom('input.username').hasNoAttribute('disabled');
*
* @see {@link #hasAttribute}
*/
doesNotHaveAttribute(name: string, message?: string): void {
let element = this.findTargetElement();
if (!element) return;
let result = !element.hasAttribute(name);
let expected = `Element ${this.targetDescription} does not have attribute "${name}"`;
let actual = expected;
if (!result) {
let value = element.getAttribute(name);
actual = `Element ${this.targetDescription} has attribute "${name}" with value ${JSON.stringify(value)}`;
}
if (!message) {
message = expected;
}
this.pushResult({ result, actual, expected, message });
}
hasNoAttribute(name: string, message?: string): void {
this.doesNotHaveAttribute(name, message);
}
lacksAttribute(name: string, message?: string): void {
this.doesNotHaveAttribute(name, message);
}
/**
* Assert that the [HTMLElement][] or an [HTMLElement][] matching the
* `selector` is disabled.
*
* @param {string?} message
*
* @example
* assert.dom('.foo').isDisabled();
*
* @see {@link #isNotDisabled}
*/
isDisabled(message?: string): void {
isDisabled.call(this, message);
}
/**
* Assert that the [HTMLElement][] or an [HTMLElement][] matching the
* `selector` is not disabled.
*
* @param {string?} message
*
* @example
* assert.dom('.foo').isNotDisabled();
*
* @see {@link #isDisabled}
*/
isNotDisabled(message?: string): void {
isDisabled.call(this, message, { inverted: true });
}
/**
* Assert that the [HTMLElement][] has the `expected` CSS class using
* [`classList`](https://developer.mozilla.org/en-US/docs/Web/API/Element/classList).
*
* @param {string} expected
* @param {string?} message
*
* @example
* assert.dom('input[type="password"]').hasClass('secret-password-input');
*
* @see {@link #doesNotHaveClass}
*/
hasClass(expected: string, message?: string): void {
let element = this.findTargetElement();
if (!element) return;
let actual = element.classList.toString();
let result = element.classList.contains(expected);
if (!message) {
message = `Element ${this.targetDescription} has CSS class "${expected}"`;
}
this.pushResult({ result, actual, expected, message });
}
/**
* Assert that the [HTMLElement][] does not have the `expected` CSS class using
* [`classList`](https://developer.mozilla.org/en-US/docs/Web/API/Element/classList).
*
* **Aliases:** `hasNoClass`, `lacksClass`
*
* @param {string} expected
* @param {string?} message
*
* @example
* assert.dom('input[type="password"]').doesNotHaveClass('username-input');
*
* @see {@link #hasClass}
*/
doesNotHaveClass(expected: string, message?: string): void {
let element = this.findTargetElement();
if (!element) return;
let result = !element.classList.contains(expected);
let actual = element.classList.toString();
if (!message) {
message = `Element ${this.targetDescription} does not have CSS class "${expected}"`;
}
this.pushResult({ result, actual, expected: `not: ${expected}`, message });
}
hasNoClass(expected: string, message?: string): void {
this.doesNotHaveClass(expected, message);
}
lacksClass(expected: string, message?: string): void {
this.doesNotHaveClass(expected, message);
}
/**
* Assert that the [HTMLElement][] has the `expected` style declarations using
* [`window.getComputedStyle`](https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle).
*
* @name hasStyle
* @param {object} expected
* @param {string?} message
*
* @example
* assert.dom('.progress-bar').hasStyle({
* opacity: 1,
* display: 'block'
* });
*
* @see {@link #hasClass}
*/
hasStyle(expected: object, message?: string): void {
let element = this.findTargetElement();
if (!element) return;
let computedStyle = window.getComputedStyle(element);
let expectedProperties = Object.keys(expected);
let result = expectedProperties.every(property => computedStyle[property] === expected[property]);
let actual = {};
expectedProperties.forEach(property => actual[property] = computedStyle[property]);
if (!message) {
message = `Element ${this.targetDescription} has style "${JSON.stringify(expected)}"`;
}
this.pushResult({ result, actual, expected, message });
}
/**
* Assert that the text of the [HTMLElement][] or an [HTMLElement][]
* matching the `selector` matches the `expected` text, using the
* [`textContent`](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent)
* attribute and stripping/collapsing whitespace.
*
* `expected` can also be a regular expression.
*
* **Aliases:** `matchesText`
*
* @param {string|RegExp} expected
* @param {string?} message
*
* @example
* // <h2 id="title">
* // Welcome to <b>QUnit</b>
* // </h2>
*
* assert.dom('#title').hasText('Welcome to QUnit');
*
* @example
* assert.dom('.foo').hasText(/[12]\d{3}/);
*
* @see {@link #includesText}
*/
hasText(expected: string | RegExp | { any: true }, message?: string): void {
let element = this.findTargetElement();
if (!element) return;
if (expected instanceof RegExp) {
let result = expected.test(element.textContent);
let actual = element.textContent;
if (!message) {
message = `Element ${this.targetDescription} has text matching ${expected}`;
}
this.pushResult({ result, actual, expected, message });
} else if ((expected as { any: true }).any === true) {
let result = Boolean(element.textContent);
let expected = `Element ${this.targetDescription} has a text`;
let actual = result ? expected : `Element ${this.targetDescription} has no text`;
if (!message) {
message = expected;
}
this.pushResult({ result, actual, expected, message });
} else if (typeof expected === 'string') {
expected = collapseWhitespace(expected);
let actual = collapseWhitespace(element.textContent);
let result = actual === expected;
if (!message) {
message = `Element ${this.targetDescription} has text "${expected}"`;
}
this.pushResult({ result, actual, expected, message });
} else {
throw new TypeError(`You must pass a string or Regular Expression to "hasText". You passed ${expected}.`);
}
}
matchesText(expected: string | RegExp | { any: true }, message?: string): void {
this.hasText(expected, message);
}
/**
* Assert that the `textContent` property of an [HTMLElement][] is not empty.
*
* @param {string?} message
*
* @example
* assert.dom('button.share').hasAnyText();
*
* @see {@link #hasText}
*/
hasAnyText(message?: string): void {
this.hasText({ any: true }, message);
}
/**
* Assert that the text of the [HTMLElement][] or an [HTMLElement][]
* matching the `selector` contains the given `text`, using the
* [`textContent`](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent)
* attribute.
*
* **Aliases:** `containsText`, `hasTextContaining`
*
* @param {string} text
* @param {string?} message
*
* @example
* assert.dom('#title').includesText('Welcome');
*
* @see {@link #hasText}
*/
includesText(text: string, message?: string): void {
let element = this.findTargetElement();
if (!element) return;
let collapsedText = collapseWhitespace(element.textContent);
let result = collapsedText.indexOf(text) !== -1;
let actual = collapsedText;
let expected = text;
if (!message) {
message = `Element ${this.targetDescription} has text containing "${text}"`;
}
this.pushResult({ result, actual, expected, message });
}
containsText(expected: string, message?: string): void {
this.includesText(expected, message);
}
hasTextContaining(expected: string, message?: string): void {
this.includesText(expected, message);
}
/**
* Assert that the text of the [HTMLElement][] or an [HTMLElement][]
* matching the `selector` does not include the given `text`, using the
* [`textContent`](https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent)
* attribute.
*
* **Aliases:** `doesNotContainText`, `doesNotHaveTextContaining`
*
* @param {string} text
* @param {string?} message
*
* @example
* assert.dom('#title').doesNotIncludeText('Welcome');
*/
doesNotIncludeText(text: string, message?: string): void {
let element = this.findTargetElement();
if (!element) return;
let collapsedText = collapseWhitespace(element.textContent);
let result = collapsedText.indexOf(text) === -1;
let expected = `Element ${this.targetDescription} does not include text "${text}"`;
let actual = expected;
if (!result) {
actual = `Element ${this.targetDescription} includes text "${text}"`;
}
if (!message) {
message = expected;
}
this.pushResult({ result, actual, expected, message });
}
doesNotContainText(unexpected: string, message?: string): void {
this.doesNotIncludeText(unexpected, message);
}
doesNotHaveTextContaining(unexpected: string, message?: string): void {
this.doesNotIncludeText(unexpected, message);
}
/**
* Assert that the `value` property of an [HTMLInputElement][] matches
* the `expected` text or regular expression.
*
* If no `expected` value is provided, the assertion will fail if the
* `value` is an empty string.
*
* @param {string|RegExp|object?} expected
* @param {string?} message
*
* @example
* assert.dom('input.username').hasValue('HSimpson');
* @see {@link #hasAnyValue}
* @see {@link #hasNoValue}
*/
hasValue(expected: string | RegExp | { any: true }, message?: string) {
let element = this.findTargetElement();
if (!element) return;
if (arguments.length === 0) {
expected = { any: true };
}
let value = (element as HTMLInputElement).value;
if (expected instanceof RegExp) {
let result = expected.test(value);
let actual = value;
if (!message) {
message = `Element ${this.targetDescription} has value matching ${expected}`;
}
this.pushResult({ result, actual, expected, message });
} else if ((expected as { any: true }).any === true) {
let result = Boolean(value);
let expected = `Element ${this.targetDescription} has a value`;
let actual = result ? expected : `Element ${this.targetDescription} has no value`;
if (!message) {
message = expected;
}
this.pushResult({ result, actual, expected, message });
} else {
let actual = value;
let result = actual === expected;
if (!message) {
message = `Element ${this.targetDescription} has value "${expected}"`;
}
this.pushResult({ result, actual, expected, message });
}
}
/**
* Assert that the `value` property of an [HTMLInputElement][] is not empty.
*
* @param {string?} message
*
* @example
* assert.dom('input.username').hasAnyValue();
*
* @see {@link #hasValue}
* @see {@link #hasNoValue}
*/
hasAnyValue(message?: string): void {
this.hasValue({ any: true }, message);
}
/**
* Assert that the `value` property of an [HTMLInputElement][] is empty.
*
* **Aliases:** `lacksValue`
*
* @param {string?} message
*
* @example
* assert.dom('input.username').hasNoValue();
*
* @see {@link #hasValue}
* @see {@link #hasAnyValue}
*/
hasNoValue(message?: string): void {
this.hasValue('', message);
}
lacksValue(message?: string): void {
this.hasNoValue(message);
}
/**
* @private
*/
private pushResult(result) {
this.testContext.pushResult(result);
}
/**
* Finds a valid HTMLElement from target, or pushes a failing assertion if a valid
* element is not found.
* @private
* @returns (HTMLElement|null) a valid HTMLElement, or null
*/
private findTargetElement(): Element | null {
let el = this.findElement();
if (el === null) {
let message = `Element ${this.target || '<unknown>'} should exist`;
this.pushResult({ message, result: false });
return null;
}
return el;
}
/**
* Finds a valid HTMLElement from target
* @private
* @returns (HTMLElement|null) a valid HTMLElement, or null
* @throws TypeError will be thrown if target is an unrecognized type
*/
private findElement(): Element | null {
if (this.target === null) {
return null;
} else if (typeof this.target === 'string') {
return this.rootElement.querySelector(this.target);
} else if (this.target instanceof Element) {
return this.target;
} else {
throw new TypeError(`Unexpected Parameter: ${this.target}`)
}
}
/**
* @private
*/
private get targetDescription() {
return elementToString(this.target);
}
}