@@ -5,6 +5,7 @@ import { ElementAssertion } from "../../../src/lib/ElementAssertion";
55
66import { HaveClassTest } from "./fixtures/HaveClassTest" ;
77import { NestedElementsTest } from "./fixtures/NestedElementsTest" ;
8+ import { PressedTestComponent } from "./fixtures/PressedTestComponent" ;
89import { SimpleTest } from "./fixtures/SimpleTest" ;
910import { WithAttributesTest } from "./fixtures/WithAttributesTest" ;
1011import { DescriptionTestComponent } from "./fixtures/descriptionTestComponent" ;
@@ -586,4 +587,240 @@ describe("[Unit] ElementAssertion.test.ts", () => {
586587 } ) ;
587588 } ) ;
588589 } ) ;
590+
591+ describe ( ".toBePressed" , ( ) => {
592+ context ( "when the element is a valid button-like element" , ( ) => {
593+ context ( "when aria-pressed is \"true\"" , ( ) => {
594+ it ( "returns the assertion instance" , ( ) => {
595+ const { getByTestId } = render ( < PressedTestComponent /> ) ;
596+ const button = getByTestId ( "button-pressed" ) ;
597+ const test = new ElementAssertion ( button ) ;
598+
599+ expect ( test . toBePressed ( ) ) . toBeEqual ( test ) ;
600+
601+ expect ( ( ) => test . not . toBePressed ( ) )
602+ . toThrowError ( AssertionError )
603+ . toHaveMessage ( 'Expected the element to NOT be pressed, but received aria-pressed="true"' ) ;
604+ } ) ;
605+ } ) ;
606+
607+ context ( "when aria-pressed is \"false\"" , ( ) => {
608+ it ( "throws an assertion error" , ( ) => {
609+ const { getByTestId } = render ( < PressedTestComponent /> ) ;
610+ const button = getByTestId ( "button-not-pressed" ) ;
611+ const test = new ElementAssertion ( button ) ;
612+
613+ expect ( ( ) => test . toBePressed ( ) )
614+ . toThrowError ( AssertionError )
615+ . toHaveMessage ( 'Expected the element to be pressed, but received aria-pressed="false"' ) ;
616+
617+ expect ( test . not . toBePressed ( ) ) . toBeEqual ( test ) ;
618+ } ) ;
619+ } ) ;
620+
621+ context ( "when aria-pressed is \"mixed\"" , ( ) => {
622+ it ( "throws an assertion error" , ( ) => {
623+ const { getByTestId } = render ( < PressedTestComponent /> ) ;
624+ const button = getByTestId ( "button-mixed" ) ;
625+ const test = new ElementAssertion ( button ) ;
626+
627+ expect ( ( ) => test . toBePressed ( ) )
628+ . toThrowError ( AssertionError )
629+ . toHaveMessage ( 'Expected the element to be pressed, but received aria-pressed="mixed"' ) ;
630+
631+ expect ( test . not . toBePressed ( ) ) . toBeEqual ( test ) ;
632+ } ) ;
633+ } ) ;
634+
635+ context ( "when the element is an input with type=\"button\"" , ( ) => {
636+ it ( "returns the assertion instance when aria-pressed is \"true\"" , ( ) => {
637+ const { getByTestId } = render ( < PressedTestComponent /> ) ;
638+ const input = getByTestId ( "input-button-pressed" ) ;
639+ const test = new ElementAssertion ( input ) ;
640+
641+ expect ( test . toBePressed ( ) ) . toBeEqual ( test ) ;
642+
643+ expect ( ( ) => test . not . toBePressed ( ) )
644+ . toThrowError ( AssertionError )
645+ . toHaveMessage ( 'Expected the element to NOT be pressed, but received aria-pressed="true"' ) ;
646+ } ) ;
647+
648+ it ( "throws an assertion error when aria-pressed is \"false\"" , ( ) => {
649+ const { getByTestId } = render ( < PressedTestComponent /> ) ;
650+ const input = getByTestId ( "input-button-not-pressed" ) ;
651+ const test = new ElementAssertion ( input ) ;
652+
653+ expect ( ( ) => test . toBePressed ( ) )
654+ . toThrowError ( AssertionError )
655+ . toHaveMessage ( 'Expected the element to be pressed, but received aria-pressed="false"' ) ;
656+
657+ expect ( test . not . toBePressed ( ) ) . toBeEqual ( test ) ;
658+ } ) ;
659+ } ) ;
660+
661+ context ( "when the element has role=\"button\"" , ( ) => {
662+ it ( "returns the assertion instance when aria-pressed is \"true\"" , ( ) => {
663+ const { getByTestId } = render ( < PressedTestComponent /> ) ;
664+ const div = getByTestId ( "role-button-pressed" ) ;
665+ const test = new ElementAssertion ( div ) ;
666+
667+ expect ( test . toBePressed ( ) ) . toBeEqual ( test ) ;
668+
669+ expect ( ( ) => test . not . toBePressed ( ) )
670+ . toThrowError ( AssertionError )
671+ . toHaveMessage ( 'Expected the element to NOT be pressed, but received aria-pressed="true"' ) ;
672+ } ) ;
673+
674+ it ( "throws an assertion error when aria-pressed is \"false\"" , ( ) => {
675+ const { getByTestId } = render ( < PressedTestComponent /> ) ;
676+ const div = getByTestId ( "role-button-not-pressed" ) ;
677+ const test = new ElementAssertion ( div ) ;
678+
679+ expect ( ( ) => test . toBePressed ( ) )
680+ . toThrowError ( AssertionError )
681+ . toHaveMessage ( 'Expected the element to be pressed, but received aria-pressed="false"' ) ;
682+
683+ expect ( test . not . toBePressed ( ) ) . toBeEqual ( test ) ;
684+ } ) ;
685+ } ) ;
686+ } ) ;
687+
688+ context ( "when the element is not a valid button-like element" , ( ) => {
689+ it ( "throws a plain Error" , ( ) => {
690+ const { getByTestId } = render ( < PressedTestComponent /> ) ;
691+ const div = getByTestId ( "non-button-element" ) ;
692+ const test = new ElementAssertion ( div ) ;
693+
694+ expect ( ( ) => test . toBePressed ( ) ) . toThrowError ( Error ) ;
695+ } ) ;
696+ } ) ;
697+
698+ context ( "when aria-pressed is missing" , ( ) => {
699+ it ( "throws a plain Error" , ( ) => {
700+ const { getByTestId } = render ( < PressedTestComponent /> ) ;
701+ const button = getByTestId ( "button-no-aria-pressed" ) ;
702+ const test = new ElementAssertion ( button ) ;
703+
704+ expect ( ( ) => test . toBePressed ( ) ) . toThrowError ( Error ) ;
705+ } ) ;
706+ } ) ;
707+ } ) ;
708+
709+ describe ( ".toBePartiallyPressed" , ( ) => {
710+ context ( "when the element is a valid button-like element" , ( ) => {
711+ context ( "when aria-pressed is \"mixed\"" , ( ) => {
712+ it ( "returns the assertion instance" , ( ) => {
713+ const { getByTestId } = render ( < PressedTestComponent /> ) ;
714+ const button = getByTestId ( "button-mixed" ) ;
715+ const test = new ElementAssertion ( button ) ;
716+
717+ expect ( test . toBePartiallyPressed ( ) ) . toBeEqual ( test ) ;
718+
719+ expect ( ( ) => test . not . toBePartiallyPressed ( ) )
720+ . toThrowError ( AssertionError )
721+ . toHaveMessage ( 'Expected the element to NOT be partially pressed, but received aria-pressed="mixed"' ) ;
722+ } ) ;
723+ } ) ;
724+
725+ context ( "when aria-pressed is \"true\"" , ( ) => {
726+ it ( "throws an assertion error" , ( ) => {
727+ const { getByTestId } = render ( < PressedTestComponent /> ) ;
728+ const button = getByTestId ( "button-pressed" ) ;
729+ const test = new ElementAssertion ( button ) ;
730+
731+ expect ( ( ) => test . toBePartiallyPressed ( ) )
732+ . toThrowError ( AssertionError )
733+ . toHaveMessage ( 'Expected the element to be partially pressed, but received aria-pressed="true"' ) ;
734+
735+ expect ( test . not . toBePartiallyPressed ( ) ) . toBeEqual ( test ) ;
736+ } ) ;
737+ } ) ;
738+
739+ context ( "when aria-pressed is \"false\"" , ( ) => {
740+ it ( "throws an assertion error" , ( ) => {
741+ const { getByTestId } = render ( < PressedTestComponent /> ) ;
742+ const button = getByTestId ( "button-not-pressed" ) ;
743+ const test = new ElementAssertion ( button ) ;
744+
745+ expect ( ( ) => test . toBePartiallyPressed ( ) )
746+ . toThrowError ( AssertionError )
747+ . toHaveMessage ( 'Expected the element to be partially pressed, but received aria-pressed="false"' ) ;
748+
749+ expect ( test . not . toBePartiallyPressed ( ) ) . toBeEqual ( test ) ;
750+ } ) ;
751+ } ) ;
752+
753+ context ( "when the element is an input with type=\"button\"" , ( ) => {
754+ it ( "returns the assertion instance when aria-pressed is \"mixed\"" , ( ) => {
755+ const { getByTestId } = render ( < PressedTestComponent /> ) ;
756+ const input = getByTestId ( "input-button-mixed" ) ;
757+ const test = new ElementAssertion ( input ) ;
758+
759+ expect ( test . toBePartiallyPressed ( ) ) . toBeEqual ( test ) ;
760+
761+ expect ( ( ) => test . not . toBePartiallyPressed ( ) )
762+ . toThrowError ( AssertionError )
763+ . toHaveMessage ( 'Expected the element to NOT be partially pressed, but received aria-pressed="mixed"' ) ;
764+ } ) ;
765+
766+ it ( "throws an assertion error when aria-pressed is \"false\"" , ( ) => {
767+ const { getByTestId } = render ( < PressedTestComponent /> ) ;
768+ const input = getByTestId ( "input-button-not-pressed" ) ;
769+ const test = new ElementAssertion ( input ) ;
770+
771+ expect ( ( ) => test . toBePartiallyPressed ( ) )
772+ . toThrowError ( AssertionError )
773+ . toHaveMessage ( 'Expected the element to be partially pressed, but received aria-pressed="false"' ) ;
774+
775+ expect ( test . not . toBePartiallyPressed ( ) ) . toBeEqual ( test ) ;
776+ } ) ;
777+ } ) ;
778+
779+ context ( "when the element has role=\"button\"" , ( ) => {
780+ it ( "returns the assertion instance when aria-pressed is \"mixed\"" , ( ) => {
781+ const { getByTestId } = render ( < PressedTestComponent /> ) ;
782+ const div = getByTestId ( "role-button-mixed" ) ;
783+ const test = new ElementAssertion ( div ) ;
784+
785+ expect ( test . toBePartiallyPressed ( ) ) . toBeEqual ( test ) ;
786+
787+ expect ( ( ) => test . not . toBePartiallyPressed ( ) )
788+ . toThrowError ( AssertionError )
789+ . toHaveMessage ( 'Expected the element to NOT be partially pressed, but received aria-pressed="mixed"' ) ;
790+ } ) ;
791+
792+ it ( "throws an assertion error when aria-pressed is \"false\"" , ( ) => {
793+ const { getByTestId } = render ( < PressedTestComponent /> ) ;
794+ const div = getByTestId ( "role-button-not-pressed" ) ;
795+ const test = new ElementAssertion ( div ) ;
796+
797+ expect ( ( ) => test . toBePartiallyPressed ( ) )
798+ . toThrowError ( AssertionError )
799+ . toHaveMessage ( 'Expected the element to be partially pressed, but received aria-pressed="false"' ) ;
800+
801+ expect ( test . not . toBePartiallyPressed ( ) ) . toBeEqual ( test ) ;
802+ } ) ;
803+ } ) ;
804+ } ) ;
805+
806+ context ( "when the element is not a valid button-like element" , ( ) => {
807+ it ( "throws a plain Error" , ( ) => {
808+ const { getByTestId } = render ( < PressedTestComponent /> ) ;
809+ const div = getByTestId ( "non-button-element" ) ;
810+ const test = new ElementAssertion ( div ) ;
811+
812+ expect ( ( ) => test . toBePartiallyPressed ( ) ) . toThrowError ( Error ) ;
813+ } ) ;
814+ } ) ;
815+
816+ context ( "when aria-pressed is missing" , ( ) => {
817+ it ( "throws a plain Error" , ( ) => {
818+ const { getByTestId } = render ( < PressedTestComponent /> ) ;
819+ const button = getByTestId ( "button-no-aria-pressed" ) ;
820+ const test = new ElementAssertion ( button ) ;
821+
822+ expect ( ( ) => test . toBePartiallyPressed ( ) ) . toThrowError ( Error ) ;
823+ } ) ;
824+ } ) ;
825+ } ) ;
589826} ) ;
0 commit comments