@@ -6,6 +6,7 @@ import { ElementAssertion } from "../../../src/lib/ElementAssertion";
6
6
import { NestedElementsTestComponent } from "./fixtures/nestedElementsTestComponent" ;
7
7
import { SimpleTestComponent } from "./fixtures/simpleTestComponent" ;
8
8
import { WithAttributesTestComponent } from "./fixtures/withAttributesTestComponent" ;
9
+ import { HaveClassTestComponent } from "./fixtures/haveClassTestComponent" ;
9
10
10
11
describe ( "[Unit] ElementAssertion.test.ts" , ( ) => {
11
12
describe ( ".toBeInTheDocument" , ( ) => {
@@ -172,4 +173,51 @@ describe("[Unit] ElementAssertion.test.ts", () => {
172
173
} ) ;
173
174
} ) ;
174
175
} ) ;
176
+
177
+ describe ( ".toHaveClass" , ( ) => {
178
+ context ( "when the element has the the expected class" , ( ) => {
179
+ it ( "returns the assertion instance" , async ( ) => {
180
+ const { findByTestId } = render ( < HaveClassTestComponent /> ) ;
181
+ const divTest = await findByTestId ( "classTest" ) ;
182
+ divTest . className = "foo bar" ;
183
+ const test = new ElementAssertion ( divTest ) ;
184
+ expect ( test . toHaveClass ( "foo" ) ) . toBeEqual ( test ) ;
185
+ } ) ;
186
+ } ) ;
187
+
188
+ context ( "when the element does not have the expected class " , ( ) => {
189
+ it ( "throws an assertion error" , async ( ) => {
190
+ const { findByTestId } = render ( < HaveClassTestComponent /> ) ;
191
+ const divTest = await findByTestId ( "classTest" ) ;
192
+ divTest . className = "foo" ;
193
+ const test = new ElementAssertion ( divTest ) ;
194
+ expect ( ( ) => test . toHaveClass ( "bar" ) )
195
+ . toThrowError ( AssertionError )
196
+ . toHaveMessage ( `Expected the element to have class(es): "bar"` ) ;
197
+ } ) ;
198
+ } ) ;
199
+
200
+ context ( "when the element element has the the exact matching expected class" , ( ) => {
201
+ it ( "returns the assertion instance" , async ( ) => {
202
+ const { findByTestId } = render ( < HaveClassTestComponent /> ) ;
203
+ const divTest = await findByTestId ( "classTest" ) ;
204
+ divTest . className = "foo bar" ;
205
+ const test = new ElementAssertion ( divTest ) ;
206
+ expect ( test . toHaveClass ( [ "foo" , "bar" ] , { exact : true } ) ) . toBeEqual ( test ) ;
207
+ } ) ;
208
+ } ) ;
209
+
210
+ context ( "when the element does not have the exact matching expected class " , async ( ) => {
211
+ it ( "throws an assertion error" , async ( ) => {
212
+ const { findByTestId } = render ( < HaveClassTestComponent /> ) ;
213
+ const divTest = await findByTestId ( "classTest" ) ;
214
+ divTest . className = "foo bar extra" ;
215
+ const test = new ElementAssertion ( divTest ) ;
216
+ expect ( ( ) => test . toHaveClass ( [ "foo" , "bar" ] , { exact : true } ) )
217
+ . toThrowError ( AssertionError )
218
+ . toHaveMessage ( `Expected the element to have exactly these classes: "foo bar"` ) ;
219
+ } ) ;
220
+ } ) ;
221
+ } ) ;
222
+
175
223
} ) ;
0 commit comments