@@ -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" , ( ) => {
@@ -69,7 +70,7 @@ describe("[Unit] ElementAssertion.test.ts", () => {
69
70
70
71
context ( "and it is an indirect child" , ( ) => {
71
72
it ( "returns the assertion instance" , async ( ) => {
72
- const { findByTestId } = render ( < NestedElementsTestComponent /> ) ;
73
+ const { findByTestId } = render ( < NestedElementsTestComponent /> ) ;
73
74
const grandparent = await findByTestId ( "grandparent" ) ;
74
75
const child = await findByTestId ( "child" ) ;
75
76
const grandparentTest = new ElementAssertion ( grandparent ) ;
@@ -84,7 +85,7 @@ describe("[Unit] ElementAssertion.test.ts", () => {
84
85
85
86
context ( "and it is a deeply nested child" , ( ) => {
86
87
it ( "returns the assertion instance" , async ( ) => {
87
- const { findByTestId } = render ( < NestedElementsTestComponent /> ) ;
88
+ const { findByTestId } = render ( < NestedElementsTestComponent /> ) ;
88
89
const grandparent = await findByTestId ( "grandparent" ) ;
89
90
const deepChild = await findByTestId ( "deep-child" ) ;
90
91
const grandparentTest = new ElementAssertion ( grandparent ) ;
@@ -101,7 +102,7 @@ describe("[Unit] ElementAssertion.test.ts", () => {
101
102
context ( "when element is NOT contained in ancestor element" , ( ) => {
102
103
it ( "throws an assertion error" , async ( ) => {
103
104
const notChildElement = document . createElement ( "span" ) ;
104
- const { findByTestId } = render ( < NestedElementsTestComponent /> ) ;
105
+ const { findByTestId } = render ( < NestedElementsTestComponent /> ) ;
105
106
const grandparent = await findByTestId ( "grandparent" ) ;
106
107
const grandparentTest = new ElementAssertion ( grandparent ) ;
107
108
@@ -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