@@ -3,6 +3,7 @@ import { render } from "@testing-library/react";
33
44import { ElementAssertion } from "../../../src/lib/ElementAssertion" ;
55
6+ import { HaveClassTestComponent } from "./fixtures/haveClassTestComponent" ;
67import { NestedElementsTestComponent } from "./fixtures/nestedElementsTestComponent" ;
78import { SimpleTestComponent } from "./fixtures/simpleTestComponent" ;
89import { WithAttributesTestComponent } from "./fixtures/withAttributesTestComponent" ;
@@ -172,4 +173,96 @@ describe("[Unit] ElementAssertion.test.ts", () => {
172173 } ) ;
173174 } ) ;
174175 } ) ;
176+
177+ describe ( ".toHaveClass" , ( ) => {
178+ context ( "when the element has the expected class" , ( ) => {
179+ it ( "returns the assertion instance" , ( ) => {
180+ const { getByText } = render ( < HaveClassTestComponent className = "foo bar" /> ) ;
181+ const divTest = getByText ( "Test text inside a div" ) ;
182+ const test = new ElementAssertion ( divTest ) ;
183+
184+ expect ( test . toHaveClass ( "foo" ) ) . toBeEqual ( test ) ;
185+
186+ expect ( ( ) => test . not . toHaveClass ( "foo" ) )
187+ . toThrowError ( AssertionError )
188+ . toHaveMessage ( 'Expected the element to NOT have class: "foo"' ) ;
189+ } ) ;
190+ } ) ;
191+
192+ context ( "when the element does not have the expected class" , ( ) => {
193+ it ( "throws an assertion error" , ( ) => {
194+ const { getByText } = render ( < HaveClassTestComponent className = "foo bar" /> ) ;
195+ const divTest = getByText ( "Test text inside a div" ) ;
196+ const test = new ElementAssertion ( divTest ) ;
197+
198+ expect ( ( ) => test . toHaveClass ( "baz" ) )
199+ . toThrowError ( AssertionError )
200+ . toHaveMessage ( 'Expected the element to have class: "baz"' ) ;
201+
202+ expect ( test . not . toHaveClass ( "baz" ) ) . toBeEqual ( test ) ;
203+ } ) ;
204+ } ) ;
205+ } ) ;
206+
207+ describe ( ".toHaveAnyClass" , ( ) => {
208+ context ( "when the element has at least one of the expected classes" , ( ) => {
209+ it ( "returns the assertion instance" , ( ) => {
210+ const { getByText } = render ( < HaveClassTestComponent className = "foo bar" /> ) ;
211+ const divTest = getByText ( "Test text inside a div" ) ;
212+ const test = new ElementAssertion ( divTest ) ;
213+
214+ expect ( test . toHaveAnyClass ( "bar" , "baz" ) ) . toBeEqual ( test ) ;
215+
216+ expect ( ( ) => test . not . toHaveAnyClass ( "bar" , "baz" ) )
217+ . toThrowError ( AssertionError )
218+ . toHaveMessage ( 'Expected the element to NOT have any of these classes: "bar baz"' ) ;
219+ } ) ;
220+ } ) ;
221+
222+ context ( "when the element does not have any of the expected classes" , ( ) => {
223+ it ( "throws an assertion error" , ( ) => {
224+ const { getByText } = render ( < HaveClassTestComponent className = "foo" /> ) ;
225+ const divTest = getByText ( "Test text inside a div" ) ;
226+ const test = new ElementAssertion ( divTest ) ;
227+
228+ expect ( ( ) => test . toHaveAnyClass ( "bar" , "baz" ) )
229+ . toThrowError ( AssertionError )
230+ . toHaveMessage ( 'Expected the element to have at least one of these classes: "bar baz"' ) ;
231+
232+ expect ( test . not . toHaveAnyClass ( "bar" , "baz" ) ) . toBeEqual ( test ) ;
233+ } ) ;
234+ } ) ;
235+ } ) ;
236+
237+ describe ( ".toHaveAllClasses" , ( ) => {
238+ context ( "when the element has all the expected classes" , ( ) => {
239+ it ( "returns the assertion instance" , ( ) => {
240+ const { getByText } = render ( < HaveClassTestComponent className = "foo bar baz" /> ) ;
241+ const divTest = getByText ( "Test text inside a div" ) ;
242+ const test = new ElementAssertion ( divTest ) ;
243+
244+ expect ( test . toHaveAllClasses ( "foo" , "bar" ) ) . toBeEqual ( test ) ;
245+
246+ expect ( ( ) => test . not . toHaveAllClasses ( "foo" , "bar" ) )
247+ . toThrowError ( AssertionError )
248+ . toHaveMessage ( 'Expected the element to NOT have all of these classes: "foo bar"' ) ;
249+ } ) ;
250+ } ) ;
251+
252+ context ( "when the element does not have all the expected classes" , ( ) => {
253+ it ( "throws an assertion error" , ( ) => {
254+ const { getByText } = render ( < HaveClassTestComponent className = "foo bar" /> ) ;
255+ const divTest = getByText ( "Test text inside a div" ) ;
256+ divTest . classList . add ( "foo" , "bar" ) ;
257+ const test = new ElementAssertion ( divTest ) ;
258+
259+ expect ( ( ) => test . toHaveAllClasses ( "foo" , "bar" , "baz" ) )
260+ . toThrowError ( AssertionError )
261+ . toHaveMessage ( 'Expected the element to have all of these classes: "foo bar baz"' ) ;
262+
263+ expect ( test . not . toHaveAllClasses ( "foo" , "bar" , "baz" ) ) . toBeEqual ( test ) ;
264+ } ) ;
265+ } ) ;
266+ } ) ;
267+
175268} ) ;
0 commit comments