@@ -175,63 +175,97 @@ describe("[Unit] ElementAssertion.test.ts", () => {
175175 } ) ;
176176
177177 describe ( ".toHaveClass" , ( ) => {
178- context ( "when the element has the the expected class" , ( ) => {
178+ context ( "when the element has the expected class" , ( ) => {
179179 it ( "returns the assertion instance" , async ( ) => {
180180 const { findByTestId } = render ( < HaveClassTestComponent /> ) ;
181181 const divTest = await findByTestId ( "classTest" ) ;
182- divTest . className = "foo bar" ;
182+ divTest . classList . add ( "foo" , " bar") ;
183183 const test = new ElementAssertion ( divTest ) ;
184184
185185 expect ( test . toHaveClass ( "foo" ) ) . toBeEqual ( test ) ;
186186
187187 expect ( ( ) => test . not . toHaveClass ( "foo" ) )
188188 . toThrowError ( AssertionError )
189- . toHaveMessage ( " Expected the element to NOT have class(es): \ "foo\"" ) ;
189+ . toHaveMessage ( ' Expected the element to NOT have class: "foo"' ) ;
190190 } ) ;
191191 } ) ;
192192
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" , ( ) => {
194226 it ( "throws an assertion error" , async ( ) => {
195227 const { findByTestId } = render ( < HaveClassTestComponent /> ) ;
196228 const divTest = await findByTestId ( "classTest" ) ;
197229 divTest . className = "foo" ;
198230 const test = new ElementAssertion ( divTest ) ;
199231
200- expect ( ( ) => test . toHaveClass ( "bar" ) )
232+ expect ( ( ) => test . toHaveAnyClass ( "bar" , "baz ") )
201233 . 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"' ) ;
203235
204- expect ( test . not . toHaveClass ( "bar" ) ) . toBeEqual ( test ) ;
236+ expect ( test . not . toHaveAnyClass ( "bar" , "baz ") ) . toBeEqual ( test ) ;
205237 } ) ;
206238 } ) ;
239+ } ) ;
207240
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" , ( ) => {
209243 it ( "returns the assertion instance" , async ( ) => {
210244 const { findByTestId } = render ( < HaveClassTestComponent /> ) ;
211245 const divTest = await findByTestId ( "classTest" ) ;
212- divTest . className = "foo bar" ;
246+ divTest . classList . add ( "foo" , " bar", "baz" ) ;
213247 const test = new ElementAssertion ( divTest ) ;
214248
215- expect ( test . toHaveClass ( [ "foo" , "bar" ] , { exact : true } ) ) . toBeEqual ( test ) ;
249+ expect ( test . toHaveAllClasses ( "foo" , "bar" ) ) . toBeEqual ( test ) ;
216250
217- expect ( ( ) => test . not . toHaveClass ( [ "foo" , "bar" ] , { exact : true } ) )
251+ expect ( ( ) => test . not . toHaveAllClasses ( "foo" , "bar" ) )
218252 . 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"' ) ;
220254 } ) ;
221255 } ) ;
222256
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 " , ( ) => {
224258 it ( "throws an assertion error" , async ( ) => {
225259 const { findByTestId } = render ( < HaveClassTestComponent /> ) ;
226260 const divTest = await findByTestId ( "classTest" ) ;
227- divTest . className = "foo bar extra" ;
261+ divTest . classList . add ( "foo" , " bar" ) ;
228262 const test = new ElementAssertion ( divTest ) ;
229263
230- expect ( ( ) => test . toHaveClass ( [ "foo" , "bar" ] , { exact : true } ) )
264+ expect ( ( ) => test . toHaveAllClasses ( "foo" , "bar" , "baz" ) )
231265 . 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"' ) ;
233267
234- expect ( test . not . toHaveClass ( [ "foo" , "bar" ] , { exact : true } ) ) . toBeEqual ( test ) ;
268+ expect ( test . not . toHaveAllClasses ( "foo" , "bar" , "baz" ) ) . toBeEqual ( test ) ;
235269 } ) ;
236270 } ) ;
237271 } ) ;
0 commit comments