@@ -175,63 +175,97 @@ describe("[Unit] ElementAssertion.test.ts", () => {
175
175
} ) ;
176
176
177
177
describe ( ".toHaveClass" , ( ) => {
178
- context ( "when the element has the the expected class" , ( ) => {
178
+ context ( "when the element has the expected class" , ( ) => {
179
179
it ( "returns the assertion instance" , async ( ) => {
180
180
const { findByTestId } = render ( < HaveClassTestComponent /> ) ;
181
181
const divTest = await findByTestId ( "classTest" ) ;
182
- divTest . className = "foo bar" ;
182
+ divTest . classList . add ( "foo" , " bar") ;
183
183
const test = new ElementAssertion ( divTest ) ;
184
184
185
185
expect ( test . toHaveClass ( "foo" ) ) . toBeEqual ( test ) ;
186
186
187
187
expect ( ( ) => test . not . toHaveClass ( "foo" ) )
188
188
. toThrowError ( AssertionError )
189
- . toHaveMessage ( " Expected the element to NOT have class(es): \ "foo\"" ) ;
189
+ . toHaveMessage ( ' Expected the element to NOT have class: "foo"' ) ;
190
190
} ) ;
191
191
} ) ;
192
192
193
- context ( "when the element does not have the expected class " , ( ) => {
193
+ context ( "when the element does not have the expected class" , ( ) => {
194
+ it ( "throws an assertion error" , async ( ) => {
195
+ const { findByTestId } = render ( < HaveClassTestComponent /> ) ;
196
+ const divTest = await findByTestId ( "classTest" ) ;
197
+ divTest . classList . add ( "foo" , "bar" ) ;
198
+ const test = new ElementAssertion ( divTest ) ;
199
+
200
+ expect ( ( ) => test . toHaveClass ( "baz" ) )
201
+ . toThrowError ( AssertionError )
202
+ . toHaveMessage ( 'Expected the element to have class: "baz"' ) ;
203
+
204
+ expect ( test . not . toHaveClass ( "baz" ) ) . toBeEqual ( test ) ;
205
+ } ) ;
206
+ } ) ;
207
+ } ) ;
208
+
209
+ describe ( ".toHaveAnyClass" , ( ) => {
210
+ context ( "when the element has at least one of the expected classes" , ( ) => {
211
+ it ( "returns the assertion instance" , async ( ) => {
212
+ const { findByTestId } = render ( < HaveClassTestComponent /> ) ;
213
+ const divTest = await findByTestId ( "classTest" ) ;
214
+ divTest . classList . add ( "foo" , "bar" ) ;
215
+ const test = new ElementAssertion ( divTest ) ;
216
+
217
+ expect ( test . toHaveAnyClass ( "bar" , "baz" ) ) . toBeEqual ( test ) ;
218
+
219
+ expect ( ( ) => test . not . toHaveAnyClass ( "bar" , "baz" ) )
220
+ . toThrowError ( AssertionError )
221
+ . toHaveMessage ( 'Expected the element to NOT have any of these classes: "bar baz"' ) ;
222
+ } ) ;
223
+ } ) ;
224
+
225
+ context ( "when the element does not have any of the expected classes" , ( ) => {
194
226
it ( "throws an assertion error" , async ( ) => {
195
227
const { findByTestId } = render ( < HaveClassTestComponent /> ) ;
196
228
const divTest = await findByTestId ( "classTest" ) ;
197
229
divTest . className = "foo" ;
198
230
const test = new ElementAssertion ( divTest ) ;
199
231
200
- expect ( ( ) => test . toHaveClass ( "bar" ) )
232
+ expect ( ( ) => test . toHaveAnyClass ( "bar" , "baz ") )
201
233
. toThrowError ( AssertionError )
202
- . toHaveMessage ( " Expected the element to have class(es): \ "bar\"" ) ;
234
+ . toHaveMessage ( ' Expected the element to have at least one of these classes: "bar baz"' ) ;
203
235
204
- expect ( test . not . toHaveClass ( "bar" ) ) . toBeEqual ( test ) ;
236
+ expect ( test . not . toHaveAnyClass ( "bar" , "baz ") ) . toBeEqual ( test ) ;
205
237
} ) ;
206
238
} ) ;
239
+ } ) ;
207
240
208
- context ( "when the element element has the the exact matching expected class" , ( ) => {
241
+ describe ( ".toHaveAllClasses" , ( ) => {
242
+ context ( "when the element has all the expected classes" , ( ) => {
209
243
it ( "returns the assertion instance" , async ( ) => {
210
244
const { findByTestId } = render ( < HaveClassTestComponent /> ) ;
211
245
const divTest = await findByTestId ( "classTest" ) ;
212
- divTest . className = "foo bar" ;
246
+ divTest . classList . add ( "foo" , " bar", "baz" ) ;
213
247
const test = new ElementAssertion ( divTest ) ;
214
248
215
- expect ( test . toHaveClass ( [ "foo" , "bar" ] , { exact : true } ) ) . toBeEqual ( test ) ;
249
+ expect ( test . toHaveAllClasses ( "foo" , "bar" ) ) . toBeEqual ( test ) ;
216
250
217
- expect ( ( ) => test . not . toHaveClass ( [ "foo" , "bar" ] , { exact : true } ) )
251
+ expect ( ( ) => test . not . toHaveAllClasses ( "foo" , "bar" ) )
218
252
. toThrowError ( AssertionError )
219
- . toHaveMessage ( " Expected the element to NOT have exactly these classes: \ "foo bar\"" ) ;
253
+ . toHaveMessage ( ' Expected the element to NOT have all of these classes: "foo bar"' ) ;
220
254
} ) ;
221
255
} ) ;
222
256
223
- context ( "when the element does not have the exact matching expected class " , ( ) => {
257
+ context ( "when the element does not have all the expected classes " , ( ) => {
224
258
it ( "throws an assertion error" , async ( ) => {
225
259
const { findByTestId } = render ( < HaveClassTestComponent /> ) ;
226
260
const divTest = await findByTestId ( "classTest" ) ;
227
- divTest . className = "foo bar extra" ;
261
+ divTest . classList . add ( "foo" , " bar" ) ;
228
262
const test = new ElementAssertion ( divTest ) ;
229
263
230
- expect ( ( ) => test . toHaveClass ( [ "foo" , "bar" ] , { exact : true } ) )
264
+ expect ( ( ) => test . toHaveAllClasses ( "foo" , "bar" , "baz" ) )
231
265
. toThrowError ( AssertionError )
232
- . toHaveMessage ( " Expected the element to have exactly these classes: \ "foo bar\"" ) ;
266
+ . toHaveMessage ( ' Expected the element to have all of these classes: "foo bar baz"' ) ;
233
267
234
- expect ( test . not . toHaveClass ( [ "foo" , "bar" ] , { exact : true } ) ) . toBeEqual ( test ) ;
268
+ expect ( test . not . toHaveAllClasses ( "foo" , "bar" , "baz" ) ) . toBeEqual ( test ) ;
235
269
} ) ;
236
270
} ) ;
237
271
} ) ;
0 commit comments