@@ -64,6 +64,8 @@ clear to read and to maintain.
64
64
- [ ` toBeVisible ` ] ( #tobevisible )
65
65
- [ ` toContainElement ` ] ( #tocontainelement )
66
66
- [ ` toContainHTML ` ] ( #tocontainhtml )
67
+ - [ ` toHaveAccessibleDescription ` ] ( #tohaveaccessibledescription )
68
+ - [ ` toHaveAccessibleName ` ] ( #tohaveaccessiblename )
67
69
- [ ` toHaveAttribute ` ] ( #tohaveattribute )
68
70
- [ ` toHaveClass ` ] ( #tohaveclass )
69
71
- [ ` toHaveFocus ` ] ( #tohavefocus )
@@ -155,8 +157,7 @@ toBeDisabled()
155
157
```
156
158
157
159
This allows you to check whether an element is disabled from the user's
158
- perspective.
159
- According to the specification, the following elements can be
160
+ perspective. According to the specification, the following elements can be
160
161
[ disabled] ( https://html.spec.whatwg.org/multipage/semantics-other.html#disabled-elements ) :
161
162
` button ` , ` input ` , ` select ` , ` textarea ` , ` optgroup ` , ` option ` , ` fieldset ` .
162
163
@@ -526,6 +527,94 @@ expect(getByTestId('parent')).toContainHTML('</span>')
526
527
527
528
<hr />
528
529
530
+ ### ` toHaveAccessibleDescription `
531
+
532
+ ``` typescript
533
+ toHaveAccessibleDescription (expectedAccessibleDescription ?: string | RegExp )
534
+ ```
535
+
536
+ This allows to assert that an element has the expected
537
+ [ accessible description] ( https://w3c.github.io/accname/ ) .
538
+
539
+ You can pass the exact string of the expected accessible description, or you can
540
+ make a partial match passing a regular expression, or by using
541
+ [ expect.stringContaining] ( https://jestjs.io/docs/en/expect.html#expectnotstringcontainingstring ) /[ expect.stringMatching] ( https://jestjs.io/docs/en/expect.html#expectstringmatchingstring-regexp ) .
542
+
543
+ #### Examples
544
+
545
+ ``` html
546
+ <a
547
+ data-testid =" link"
548
+ href =" /"
549
+ aria-label =" Home page"
550
+ title =" A link to start over"
551
+ >Start</a
552
+ >
553
+ <a data-testid =" extra-link" href =" /about" aria-label =" About page" >About</a >
554
+ <img src =" avatar.jpg" data-testid =" avatar" alt =" User profile pic" />
555
+ <img
556
+ src =" logo.jpg"
557
+ data-testid =" logo"
558
+ alt =" Company logo"
559
+ aria-describedby =" t1"
560
+ />
561
+ <span id =" t1" role =" presentation" >The logo of Our Company</span >
562
+ ```
563
+
564
+ ``` js
565
+ expect (getByTestId (' link' )).toHaveAccessibleDescription ()
566
+ expect (getByTestId (' link' )).toHaveAccessibleDescription (' A link to start over' )
567
+ expect (getByTestId (' link' )).not .toHaveAccessibleDescription (' Home page' )
568
+ expect (getByTestId (' extra-link' )).not .toHaveAccessibleDescription ()
569
+ expect (getByTestId (' avatar' )).not .toHaveAccessibleDescription ()
570
+ expect (getByTestId (' logo' )).not .toHaveAccessibleDescription (' Company logo' )
571
+ expect (getByTestId (' logo' )).toHaveAccessibleDescription (
572
+ ' The logo of Our Company' ,
573
+ )
574
+ ```
575
+
576
+ <hr />
577
+
578
+ ### ` toHaveAccessibleName `
579
+
580
+ ``` typescript
581
+ toHaveAccessibleName (expectedAccessibleName ?: string | RegExp )
582
+ ```
583
+
584
+ This allows to assert that an element is has the expected
585
+ [ accessible name] ( https://w3c.github.io/accname/ ) . It is useful, for instance,
586
+ to assert that form elements and buttons are properly labelled.
587
+
588
+ You can pass the exact string of the expected accessible name, or you can make a
589
+ partial match passing a regular expression, or by using
590
+ [ expect.stringContaining] ( https://jestjs.io/docs/en/expect.html#expectnotstringcontainingstring ) /[ expect.stringMatching] ( https://jestjs.io/docs/en/expect.html#expectstringmatchingstring-regexp ) .
591
+
592
+ #### Examples
593
+
594
+ ``` html
595
+ <img data-testid =" img-alt" src =" " alt =" Test alt" />
596
+ <img data-testid =" img-empty-alt" src =" " alt =" " />
597
+ <svg data-testid =" svg-title" ><title >Test title</title ></svg >
598
+ <button data-testid =" button-img-alt" ><img src =" " alt =" Test" /></button >
599
+ <p ><img data-testid =" img-paragraph" src =" " alt =" " /> Test content</p >
600
+ <button data-testid =" svg-button" ><svg ><title >Test</title ></svg ></p >
601
+ <div ><svg data-testid =" svg-without-title" ></svg ></div >
602
+ <input data-testid =" input-title" title =" test" />
603
+ ```
604
+
605
+ ``` javascript
606
+ expect (getByTestId (' img-alt' )).toHaveAccessibleName (' Test alt' )
607
+ expect (getByTestId (' img-empty-alt' )).not .toHaveAccessibleName ()
608
+ expect (getByTestId (' svg-title' )).toHaveAccessibleName (' Test title' )
609
+ expect (getByTestId (' button-img-alt' )).toHaveAccessibleName ()
610
+ expect (getByTestId (' img-paragraph' )).not .toHaveAccessibleName ()
611
+ expect (getByTestId (' svg-button' )).toHaveAccessibleName ()
612
+ expect (getByTestId (' svg-without-title' )).not .toHaveAccessibleName ()
613
+ expect (getByTestId (' input-title' )).toHaveAccessibleName ()
614
+ ```
615
+
616
+ <hr />
617
+
529
618
### ` toHaveAttribute `
530
619
531
620
``` typescript
0 commit comments