@@ -12,6 +12,14 @@ function TestComponent(): ReactElement {
12
12
) ;
13
13
}
14
14
15
+ function TestComponentElement ( ) : ReactElement {
16
+ return (
17
+ < button role = "button" type = "submit" className = "btn primary" disabled >
18
+ click me
19
+ </ button >
20
+ ) ;
21
+ }
22
+
15
23
describe ( "[Unit] ElementAssertion.test.ts" , ( ) => {
16
24
describe ( ".toBeInTheDocument" , ( ) => {
17
25
context ( "when the element is in the document" , ( ) => {
@@ -42,4 +50,63 @@ describe("[Unit] ElementAssertion.test.ts", () => {
42
50
} ) ;
43
51
} ) ;
44
52
} ) ;
53
+
54
+ describe ( ".toHaveAttribute" , ( ) => {
55
+ context ( "when the element has the attribute with the expected value" , ( ) => {
56
+ it ( "returns the assertion instance" , async ( ) => {
57
+ const { findByRole } = render ( < TestComponentElement /> ) ;
58
+ const button = await findByRole ( "button" , { name : "click me" } ) ;
59
+ const test = new ElementAssertion ( button ) ;
60
+
61
+ expect ( test . toHaveAttribute ( "type" , "submit" ) ) . toBeEqual ( test ) ;
62
+
63
+ expect ( ( ) => test . not . toHaveAttribute ( "type" , "submit" ) )
64
+ . toThrowError ( AssertionError )
65
+ . toHaveMessage ( "Expected to NOT have attribute \"type\" with value \"submit\", but received \"submit\"" ) ;
66
+ } ) ;
67
+ } ) ;
68
+
69
+ context ( "when the element has the attribute with a not expected value" , ( ) => {
70
+ it ( "throws an assertion error" , async ( ) => {
71
+ const { findByRole } = render ( < TestComponentElement /> ) ;
72
+ const button = await findByRole ( "button" , { name : "click me" } ) ;
73
+ const test = new ElementAssertion ( button ) ;
74
+
75
+ expect ( ( ) => test . toHaveAttribute ( "type" , "different value" ) )
76
+ . toThrowError ( AssertionError )
77
+ . toHaveMessage ( "Expected to have attribute \"type\" with value \"different value\", but received \"submit\"" ,
78
+ ) ;
79
+
80
+ expect ( test . not . toHaveAttribute ( "type" , "different value" ) ) . toBeEqual ( test ) ;
81
+ } ) ;
82
+ } ) ;
83
+
84
+ context ( "when the element has the attribute without checking value" , ( ) => {
85
+ it ( "returns the assertion instance" , async ( ) => {
86
+ const { findByRole } = render ( < TestComponentElement /> ) ;
87
+ const button = await findByRole ( "button" , { name : "click me" } ) ;
88
+ const test = new ElementAssertion ( button ) ;
89
+
90
+ expect ( test . toHaveAttribute ( "disabled" ) ) . toBeEqual ( test ) ;
91
+
92
+ expect ( ( ) => test . not . toHaveAttribute ( "disabled" ) )
93
+ . toThrowError ( AssertionError )
94
+ . toHaveMessage ( "Expected to NOT have attribute \"disabled\"" ) ;
95
+ } ) ;
96
+ } ) ;
97
+
98
+ context ( "when the element does not have the attribute" , ( ) => {
99
+ it ( "throws an assertion error" , async ( ) => {
100
+ const { findByRole } = render ( < TestComponentElement /> ) ;
101
+ const button = await findByRole ( "button" , { name : "click me" } ) ;
102
+ const test = new ElementAssertion ( button ) ;
103
+
104
+ expect ( ( ) => test . toHaveAttribute ( "non-existent" ) )
105
+ . toThrowError ( AssertionError )
106
+ . toHaveMessage ( "Expected to have attribute \"non-existent\"" ) ;
107
+
108
+ expect ( test . not . toHaveAttribute ( "non-existent" ) ) . toBeEqual ( test ) ;
109
+ } ) ;
110
+ } ) ;
111
+ } ) ;
45
112
} ) ;
0 commit comments