@@ -522,4 +522,183 @@ describe("[Unit] ElementAssertion.test.ts", () => {
522522 } ) ;
523523 } ) ;
524524 } ) ;
525+
526+ describe ( ".toHaveTextContent" , ( ) => {
527+ context ( "when the element contains the target text" , ( ) => {
528+ it ( "returns the assertion instance" , ( ) => {
529+ const element = render (
530+ < Text testID = "id" > { "Hello World" } </ Text > ,
531+ ) ;
532+ const test = new ElementAssertion ( element . getByTestId ( "id" ) ) ;
533+
534+ expect ( test . toHaveTextContent ( "Hello World" ) ) . toBe ( test ) ;
535+ expect ( ( ) => test . not . toHaveTextContent ( "Hello World" ) )
536+ . toThrowError ( AssertionError )
537+ . toHaveMessage ( "Expected element <Text ... /> NOT to have text content matching 'Hello World'." ) ;
538+ } ) ;
539+ } ) ;
540+
541+ context ( "when the element does NOT contain the target text" , ( ) => {
542+ it ( "throws an error" , ( ) => {
543+ const element = render (
544+ < Text testID = "id" > { "Hello World" } </ Text > ,
545+ ) ;
546+ const test = new ElementAssertion ( element . getByTestId ( "id" ) ) ;
547+
548+ expect ( test . not . toHaveTextContent ( "Goodbye World" ) ) . toBeEqual ( test ) ;
549+ expect ( ( ) => test . toHaveTextContent ( "Goodbye World" ) )
550+ . toThrowError ( AssertionError )
551+ . toHaveMessage ( "Expected element <Text ... /> to have text content matching 'Goodbye World'." ) ;
552+ } ) ;
553+ } ) ;
554+
555+ context ( "when the element contains the target text with a RegExp" , ( ) => {
556+ it ( "returns the assertion instance" , ( ) => {
557+ const element = render (
558+ < Text testID = "id" > { "Hello World" } </ Text > ,
559+ ) ;
560+ const test = new ElementAssertion ( element . getByTestId ( "id" ) ) ;
561+
562+ expect ( test . toHaveTextContent ( / H e l l o / ) ) . toBe ( test ) ;
563+ expect ( ( ) => test . not . toHaveTextContent ( / H e l l o / ) )
564+ . toThrowError ( AssertionError )
565+ . toHaveMessage ( "Expected element <Text ... /> NOT to have text content matching '/Hello/'." ) ;
566+ } ) ;
567+ } ) ;
568+
569+ context ( "when the element does NOT contain the target text with a RegExp" , ( ) => {
570+ it ( "throws an error" , ( ) => {
571+ const element = render (
572+ < Text testID = "id" > { "Hello World" } </ Text > ,
573+ ) ;
574+ const test = new ElementAssertion ( element . getByTestId ( "id" ) ) ;
575+
576+ expect ( test . not . toHaveTextContent ( / G o o d b y e / ) ) . toBeEqual ( test ) ;
577+ expect ( ( ) => test . toHaveTextContent ( / G o o d b y e / ) )
578+ . toThrowError ( AssertionError )
579+ . toHaveMessage ( "Expected element <Text ... /> to have text content matching '/Goodbye/'." ) ;
580+ } ) ;
581+ } ) ;
582+
583+ context ( "when the eleme contains the target text within a child element" , ( ) => {
584+ it ( "returns the assertion instance" , ( ) => {
585+ const element = render (
586+ < View testID = "id" >
587+ < View >
588+ < Text > { "Test 1" } </ Text >
589+ < View >
590+ < Text > { "Test 2" } </ Text >
591+ < Text > { "Hello World" } </ Text >
592+ </ View >
593+ </ View >
594+ </ View > ,
595+ ) ;
596+ const test = new ElementAssertion ( element . getByTestId ( "id" ) ) ;
597+ expect ( test . toHaveTextContent ( "Hello World" ) ) . toBe ( test ) ;
598+ expect ( ( ) => test . not . toHaveTextContent ( "Hello World" ) )
599+ . toThrowError ( AssertionError )
600+ . toHaveMessage ( "Expected element <View ... /> NOT to have text content matching 'Hello World'." ) ;
601+ } ) ;
602+ } ) ;
603+
604+ context ( "when the element does NOT contain the target text within a child element" , ( ) => {
605+ it ( "throws an error" , ( ) => {
606+ const element = render (
607+ < View testID = "id" >
608+ < View >
609+ < Text > { "Test 1" } </ Text >
610+ < View >
611+ < Text > { "Test 2" } </ Text >
612+ < Text > { "Hello World" } </ Text >
613+ </ View >
614+ </ View >
615+ </ View > ,
616+ ) ;
617+ const test = new ElementAssertion ( element . getByTestId ( "id" ) ) ;
618+ expect ( test . not . toHaveTextContent ( "Goodbye World" ) ) . toBeEqual ( test ) ;
619+ expect ( ( ) => test . toHaveTextContent ( "Goodbye World" ) )
620+ . toThrowError ( AssertionError )
621+ . toHaveMessage ( "Expected element <View ... /> to have text content matching 'Goodbye World'." ) ;
622+ } ) ;
623+ } ) ;
624+
625+ context ( "when the element contains the target text with a function matcher" , ( ) => {
626+ it ( "returns the assertion instance" , ( ) => {
627+ const element = render (
628+ < Text testID = "id" > { "Hello World" } </ Text > ,
629+ ) ;
630+ const test = new ElementAssertion ( element . getByTestId ( "id" ) ) ;
631+
632+ expect ( test . toHaveTextContent ( text => text . startsWith ( "Hello" ) ) ) . toBe ( test ) ;
633+ expect ( ( ) => test . not . toHaveTextContent ( text => text . startsWith ( "Hello" ) ) )
634+ . toThrowError ( AssertionError )
635+ . toHaveMessage (
636+ "Expected element <Text ... /> NOT to have text content matching " +
637+ "'text => text.startsWith(\"Hello\")'." ,
638+ ) ;
639+ } ) ;
640+ } ) ;
641+
642+ context ( "when the element does NOT contain the target text with a function matcher" , ( ) => {
643+ it ( "throws an error" , ( ) => {
644+ const element = render (
645+ < Text testID = "id" > { "Hello World" } </ Text > ,
646+ ) ;
647+ const test = new ElementAssertion ( element . getByTestId ( "id" ) ) ;
648+
649+ expect ( test . not . toHaveTextContent ( text => text . startsWith ( "Goodbye" ) ) ) . toBeEqual ( test ) ;
650+ expect ( ( ) => test . toHaveTextContent ( text => text . startsWith ( "Goodbye" ) ) )
651+ . toThrowError ( AssertionError )
652+ . toHaveMessage (
653+ "Expected element <Text ... /> to have text content matching " +
654+ "'text => text.startsWith(\"Goodbye\")'." ,
655+ ) ;
656+ } ) ;
657+ } ) ;
658+
659+ context ( "when the element has no text content" , ( ) => {
660+ it ( "throws an error" , ( ) => {
661+ const element = render (
662+ < View testID = "id" /> ,
663+ ) ;
664+ const test = new ElementAssertion ( element . getByTestId ( "id" ) ) ;
665+
666+ expect ( test . not . toHaveTextContent ( "Hello World" ) ) . toBeEqual ( test ) ;
667+ expect ( ( ) => test . toHaveTextContent ( "Hello World" ) )
668+ . toThrowError ( AssertionError )
669+ . toHaveMessage ( "Expected element <View ... /> to have text content matching 'Hello World'." ) ;
670+ } ) ;
671+ } ) ;
672+
673+ context ( "when the element has no text content with a RegExp" , ( ) => {
674+ it ( "throws an error" , ( ) => {
675+ const element = render (
676+ < View testID = "id" /> ,
677+ ) ;
678+ const test = new ElementAssertion ( element . getByTestId ( "id" ) ) ;
679+
680+ expect ( test . not . toHaveTextContent ( / H e l l o / ) ) . toBeEqual ( test ) ;
681+ expect ( ( ) => test . toHaveTextContent ( / H e l l o / ) )
682+ . toThrowError ( AssertionError )
683+ . toHaveMessage ( "Expected element <View ... /> to have text content matching '/Hello/'." ) ;
684+ } ) ;
685+ } ) ;
686+
687+ context ( "when the element has no text content with a function matcher" , ( ) => {
688+ it ( "throws an error" , ( ) => {
689+ const element = render (
690+ < View testID = "id" /> ,
691+ ) ;
692+ const test = new ElementAssertion ( element . getByTestId ( "id" ) ) ;
693+
694+ expect ( test . not . toHaveTextContent ( text => text . startsWith ( "Hello" ) ) ) . toBeEqual ( test ) ;
695+ expect ( ( ) => test . toHaveTextContent ( text => text . startsWith ( "Hello" ) ) )
696+ . toThrowError ( AssertionError )
697+ . toHaveMessage (
698+ "Expected element <View ... /> to have text content matching " +
699+ "'text => text.startsWith(\"Hello\")'." ,
700+ ) ;
701+ } ) ;
702+ } ) ;
703+ } ) ;
525704} ) ;
0 commit comments