@@ -572,3 +572,177 @@ test('should find the input using type property instead of attribute', () => {
572572 const { getByRole} = render ( '<input type="124">' )
573573 expect ( getByRole ( 'textbox' ) ) . not . toBeNull ( )
574574} )
575+
576+ test ( 'can be filtered by accessible description' , ( ) => {
577+ const targetedNotificationMessage = 'Your session is about to expire!'
578+ const { getByRole} = renderIntoDocument (
579+ `
580+ <ul>
581+ <li role="alertdialog" aria-describedby="notification-id-1">
582+ <div><button>Close</button></div>
583+ <div id="notification-id-1">You have unread emails</div>
584+ </li>
585+ <li role="alertdialog" aria-describedby="notification-id-2">
586+ <div><button>Close</button></div>
587+ <div id="notification-id-2">${ targetedNotificationMessage } </div>
588+ </li>
589+ </ul>` ,
590+ )
591+
592+ const notification = getByRole ( 'alertdialog' , {
593+ description : targetedNotificationMessage ,
594+ } )
595+
596+ expect ( notification ) . not . toBeNull ( )
597+ expect ( notification ) . toHaveTextContent ( targetedNotificationMessage )
598+
599+ expect (
600+ getQueriesForElement ( notification ) . getByRole ( 'button' , { name : 'Close' } ) ,
601+ ) . not . toBeNull ( )
602+ } )
603+
604+ test ( 'error should include description when filtering and no results are found' , ( ) => {
605+ const targetedNotificationMessage = 'Your session is about to expire!'
606+ const { getByRole} = renderIntoDocument (
607+ `<div role="dialog" aria-describedby="some-id"><div><button>Close</button></div><div id="some-id">${ targetedNotificationMessage } </div></div>` ,
608+ )
609+
610+ expect ( ( ) =>
611+ getByRole ( 'alertdialog' , { description : targetedNotificationMessage } ) ,
612+ ) . toThrowErrorMatchingInlineSnapshot ( `
613+ Unable to find an accessible element with the role "alertdialog" and description "Your session is about to expire!"
614+
615+ Here are the accessible roles:
616+
617+ dialog:
618+
619+ Name "":
620+ Description "Your session is about to expire!":
621+ <div
622+ aria-describedby="some-id"
623+ role="dialog"
624+ />
625+
626+ --------------------------------------------------
627+ button:
628+
629+ Name "Close":
630+ Description "":
631+ <button />
632+
633+ --------------------------------------------------
634+
635+ Ignored nodes: comments, <script />, <style />
636+ <body>
637+ <div
638+ aria-describedby="some-id"
639+ role="dialog"
640+ >
641+ <div>
642+ <button>
643+ Close
644+ </button>
645+ </div>
646+ <div
647+ id="some-id"
648+ >
649+ Your session is about to expire!
650+ </div>
651+ </div>
652+ </body>
653+ ` )
654+ } )
655+
656+ test ( 'TextMatch serialization for description filter in error message' , ( ) => {
657+ const { getByRole} = renderIntoDocument (
658+ `<div role="alertdialog" aria-describedby="some-id"><div><button>Close</button></div><div id="some-id">Your session is about to expire!</div></div>` ,
659+ )
660+
661+ expect ( ( ) => getByRole ( 'alertdialog' , { description : / u n k n o w n d e s c r i p t i o n / } ) )
662+ . toThrowErrorMatchingInlineSnapshot ( `
663+ Unable to find an accessible element with the role "alertdialog" and description \`/unknown description/\`
664+
665+ Here are the accessible roles:
666+
667+ alertdialog:
668+
669+ Name "":
670+ Description "Your session is about to expire!":
671+ <div
672+ aria-describedby="some-id"
673+ role="alertdialog"
674+ />
675+
676+ --------------------------------------------------
677+ button:
678+
679+ Name "Close":
680+ Description "":
681+ <button />
682+
683+ --------------------------------------------------
684+
685+ Ignored nodes: comments, <script />, <style />
686+ <body>
687+ <div
688+ aria-describedby="some-id"
689+ role="alertdialog"
690+ >
691+ <div>
692+ <button>
693+ Close
694+ </button>
695+ </div>
696+ <div
697+ id="some-id"
698+ >
699+ Your session is about to expire!
700+ </div>
701+ </div>
702+ </body>
703+ ` )
704+
705+ expect ( ( ) => getByRole ( 'alertdialog' , { description : ( ) => false } ) )
706+ . toThrowErrorMatchingInlineSnapshot ( `
707+ Unable to find an accessible element with the role "alertdialog" and description \`() => false\`
708+
709+ Here are the accessible roles:
710+
711+ alertdialog:
712+
713+ Name "":
714+ Description "Your session is about to expire!":
715+ <div
716+ aria-describedby="some-id"
717+ role="alertdialog"
718+ />
719+
720+ --------------------------------------------------
721+ button:
722+
723+ Name "Close":
724+ Description "":
725+ <button />
726+
727+ --------------------------------------------------
728+
729+ Ignored nodes: comments, <script />, <style />
730+ <body>
731+ <div
732+ aria-describedby="some-id"
733+ role="alertdialog"
734+ >
735+ <div>
736+ <button>
737+ Close
738+ </button>
739+ </div>
740+ <div
741+ id="some-id"
742+ >
743+ Your session is about to expire!
744+ </div>
745+ </div>
746+ </body>
747+ ` )
748+ } )
0 commit comments