@@ -3,6 +3,7 @@ import { render } from "@testing-library/react";
33
44import { ElementAssertion } from "../../../src/lib/ElementAssertion" ;
55
6+ import { DescriptionTestComponent } from "./fixtures/descriptionTestComponent" ;
67import { FocusTestComponent } from "./fixtures/focusTestComponent" ;
78import { HaveClassTestComponent } from "./fixtures/haveClassTestComponent" ;
89import { NestedElementsTestComponent } from "./fixtures/nestedElementsTestComponent" ;
@@ -465,4 +466,123 @@ describe("[Unit] ElementAssertion.test.ts", () => {
465466 } ) ;
466467 } ) ;
467468
469+ describe ( ".toHaveDescription" , ( ) => {
470+ context ( "when checking for any description" , ( ) => {
471+ context ( "when the element has a description" , ( ) => {
472+ it ( "returns the assertion instance" , ( ) => {
473+ const { getByTestId } = render ( < DescriptionTestComponent /> ) ;
474+ const button = getByTestId ( "button-single" ) ;
475+ const test = new ElementAssertion ( button ) ;
476+
477+ expect ( test . toHaveDescription ( ) ) . toBeEqual ( test ) ;
478+
479+ expect ( ( ) => test . not . toHaveDescription ( ) )
480+ . toThrowError ( AssertionError )
481+ . toHaveMessage ( 'Expected the element NOT to have a description, but received "This is a description"' ) ;
482+ } ) ;
483+ } ) ;
484+
485+ context ( "when the element does not have a description" , ( ) => {
486+ it ( "throws an assertion error" , ( ) => {
487+ const { getByTestId } = render ( < DescriptionTestComponent /> ) ;
488+ const button = getByTestId ( "button-no-description" ) ;
489+ const test = new ElementAssertion ( button ) ;
490+
491+ expect ( ( ) => test . toHaveDescription ( ) )
492+ . toThrowError ( AssertionError )
493+ . toHaveMessage ( "Expected the element to have a description" ) ;
494+
495+ expect ( test . not . toHaveDescription ( ) ) . toBeEqual ( test ) ;
496+ } ) ;
497+ } ) ;
498+ } ) ;
499+
500+ context ( "when checking for specific description text" , ( ) => {
501+ context ( "when the element has the expected description" , ( ) => {
502+ it ( "returns the assertion instance" , ( ) => {
503+ const { getByTestId } = render ( < DescriptionTestComponent /> ) ;
504+ const button = getByTestId ( "button-single" ) ;
505+ const test = new ElementAssertion ( button ) ;
506+
507+ expect ( test . toHaveDescription ( "This is a description" ) ) . toBeEqual ( test ) ;
508+
509+ expect ( ( ) => test . not . toHaveDescription ( "This is a description" ) )
510+ . toThrowError ( AssertionError )
511+ . toHaveMessage (
512+ 'Expected the element NOT to have description "This is a description", ' +
513+ 'but received "This is a description"' ,
514+ ) ;
515+ } ) ;
516+ } ) ;
517+
518+ context ( "when the element has multiple descriptions combined" , ( ) => {
519+ it ( "returns the assertion instance" , ( ) => {
520+ const { getByTestId } = render ( < DescriptionTestComponent /> ) ;
521+ const button = getByTestId ( "button-multiple" ) ;
522+ const test = new ElementAssertion ( button ) ;
523+
524+ expect ( test . toHaveDescription ( "This is a description Additional info" ) ) . toBeEqual ( test ) ;
525+
526+ expect ( ( ) => test . not . toHaveDescription ( "This is a description Additional info" ) )
527+ . toThrowError ( AssertionError )
528+ . toHaveMessage (
529+ 'Expected the element NOT to have description "This is a description Additional info", ' +
530+ 'but received "This is a description Additional info"' ,
531+ ) ;
532+ } ) ;
533+ } ) ;
534+
535+ context ( "when the element does not have the expected description" , ( ) => {
536+ it ( "throws an assertion error" , ( ) => {
537+ const { getByTestId } = render ( < DescriptionTestComponent /> ) ;
538+ const button = getByTestId ( "button-single" ) ;
539+ const test = new ElementAssertion ( button ) ;
540+
541+ expect ( ( ) => test . toHaveDescription ( "Wrong description" ) )
542+ . toThrowError ( AssertionError )
543+ . toHaveMessage (
544+ 'Expected the element to have description "Wrong description", but received "This is a description"' ,
545+ ) ;
546+
547+ expect ( test . not . toHaveDescription ( "Wrong description" ) ) . toBeEqual ( test ) ;
548+ } ) ;
549+ } ) ;
550+ } ) ;
551+
552+ context ( "when checking with a RegExp pattern" , ( ) => {
553+ context ( "when the description matches the pattern" , ( ) => {
554+ it ( "returns the assertion instance" , ( ) => {
555+ const { getByTestId } = render ( < DescriptionTestComponent /> ) ;
556+ const button = getByTestId ( "button-single" ) ;
557+ const test = new ElementAssertion ( button ) ;
558+
559+ expect ( test . toHaveDescription ( / d e s c r i p t i o n / i) ) . toBeEqual ( test ) ;
560+
561+ expect ( ( ) => test . not . toHaveDescription ( / d e s c r i p t i o n / i) )
562+ . toThrowError ( AssertionError )
563+ . toHaveMessage (
564+ "Expected the element NOT to have description matching /description/i, " +
565+ 'but received "This is a description"' ,
566+ ) ;
567+ } ) ;
568+ } ) ;
569+
570+ context ( "when the description does not match the pattern" , ( ) => {
571+ it ( "throws an assertion error" , ( ) => {
572+ const { getByTestId } = render ( < DescriptionTestComponent /> ) ;
573+ const button = getByTestId ( "button-single" ) ;
574+ const test = new ElementAssertion ( button ) ;
575+
576+ expect ( ( ) => test . toHaveDescription ( / w r o n g p a t t e r n / ) )
577+ . toThrowError ( AssertionError )
578+ . toHaveMessage (
579+ "Expected the element to have description matching /wrong pattern/, " +
580+ 'but received "This is a description"' ,
581+ ) ;
582+
583+ expect ( test . not . toHaveDescription ( / w r o n g p a t t e r n / ) ) . toBeEqual ( test ) ;
584+ } ) ;
585+ } ) ;
586+ } ) ;
587+ } ) ;
468588} ) ;
0 commit comments