|
1 | 1 | import { AssertionError, expect } from "@assertive-ts/core"; |
2 | | -import { getByTestId, render } from "@testing-library/react"; |
| 2 | +import { render } from "@testing-library/react"; |
3 | 3 |
|
4 | 4 | import { ElementAssertion } from "../../../src/lib/ElementAssertion"; |
5 | 5 |
|
@@ -257,50 +257,70 @@ describe("[Unit] ElementAssertion.test.ts", () => { |
257 | 257 | const test = new ElementAssertion(divTest); |
258 | 258 |
|
259 | 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 | | - |
| 260 | + .toThrowError(AssertionError) |
| 261 | + .toHaveMessage('Expected the element to have all of these classes: "foo bar baz"'); |
| 262 | + |
263 | 263 | expect(test.not.toHaveAllClasses("foo", "bar", "baz")).toBeEqual(test); |
264 | 264 | }); |
265 | 265 | }); |
266 | 266 | }); |
267 | | - |
| 267 | + |
268 | 268 | describe(".toHaveStyle", () => { |
269 | | - context("when the element has the expected style when passed as string", () => { |
270 | | - it("returns the assertion instance when the styles are the same", () => { |
271 | | - const { getByTestId } = render(<div className="foo bar test" style={{ display: "flex", color: "red", border: "1px solid black" }} data-testid="test-div" />); |
272 | | - const divTest = getByTestId("test-div"); |
273 | | - const test = new ElementAssertion(divTest); |
274 | | - |
275 | | - expect(test.toHaveStyle("display: flex; color: red; border: 1px solid black")).toBeEqual(test); |
276 | | - |
277 | | - }); |
278 | | - it("fails the assertion when the styles are not the same", () => { |
279 | | - const { getByTestId } = render(<div className="foo bar test" style={{ display: "flex", color: "red" }} data-testid="test-div" />); |
280 | | - const divTest = getByTestId("test-div"); |
281 | | - const test = new ElementAssertion(divTest); |
| 269 | + context("when the style is passed as a string", () => { |
| 270 | + context("and the element has the expected style", () => { |
| 271 | + it("returns the assertion instance", () => { |
| 272 | + const { getByTestId } = render(<div className="foo bar test" style={{ display: "flex", color: "red", border: "1px solid black" }} data-testid="test-div" />); |
| 273 | + const divTest = getByTestId("test-div"); |
| 274 | + const test = new ElementAssertion(divTest); |
| 275 | + |
| 276 | + expect(test.toHaveStyle("display: flex; color: red; border: 1px solid black")).toBeEqual(test); |
| 277 | + |
| 278 | + expect(() => test.not.toHaveStyle("display: flex; color: red; border: 1px solid black")) |
| 279 | + .toThrowError(AssertionError) |
| 280 | + .toHaveMessage('Expected the element to NOT have {"display":"flex","color":"rgb(255, 0, 0)","border":"1px solid black"} style'); |
| 281 | + }); |
| 282 | + }); |
282 | 283 |
|
283 | | - expect(test.toHaveStyle("color: red; display: flex; border: 1px solid black;")).toBeEqual(test); |
| 284 | + context("and the element does not have the expected style", () => { |
| 285 | + it("throws an assertion error", () => { |
| 286 | + const { getByTestId } = render(<div className="foo bar test" style={{ display: "flex", color: "red" }} data-testid="test-div" />); |
| 287 | + const divTest = getByTestId("test-div"); |
| 288 | + const test = new ElementAssertion(divTest); |
| 289 | + |
| 290 | + expect(test.not.toHaveStyle("color: red; display: flex; border: 1px solid black;")).toBeEqual(test); |
284 | 291 |
|
| 292 | + }); |
| 293 | + }); |
285 | 294 | }); |
286 | | - context("when the element has the expected style when passed as object", () => { |
287 | | - it("returns the assertion instance when the styles are the same", () => { |
288 | | - const { getByTestId } = render(<div className="foo bar test" style={{ display: "flex", color: "red", border: "1px solid black" }} data-testid="test-div" />); |
289 | | - const divTest = getByTestId("test-div"); |
290 | | - const test = new ElementAssertion(divTest); |
291 | | - |
292 | | - expect(test.toHaveStyle({color: "red", display: "flex", border: "1px solid black"})).toBeEqual(test); |
293 | 295 |
|
294 | | - }); |
295 | | - it("fails the assertion when the styles are not the same", () => { |
296 | | - const { getByTestId } = render(<div className="foo bar test" style={{ display: "flex", color: "red" }} data-testid="test-div" />); |
297 | | - const divTest = getByTestId("test-div"); |
298 | | - const test = new ElementAssertion(divTest); |
| 296 | + context("when the style is passed as an object", () => { |
| 297 | + context("and the element has the expected style", () => { |
| 298 | + it("returns the assertion instance", () => { |
| 299 | + const { getByTestId } = render(<div className="foo bar test" style={{ display: "flex", color: "red", border: "1px solid black" }} data-testid="test-div" />); |
| 300 | + const divTest = getByTestId("test-div"); |
| 301 | + const test = new ElementAssertion(divTest); |
| 302 | + |
| 303 | + expect(test.toHaveStyle({color: "red", display: "flex", border: "1px solid black"})).toBeEqual(test); |
299 | 304 |
|
300 | | - expect(test.toHaveStyle({color: "red", display: "flex", border: "1px solid black"})).toBeEqual(test); |
| 305 | + expect(() => test.not.toHaveStyle({color: "red", display: "flex", border: "1px solid black"})) |
| 306 | + .toThrowError(AssertionError) |
| 307 | + .toHaveMessage('Expected the element to NOT have {"color":"rgb(255, 0, 0)","display":"flex","border":"1px solid black"} style'); |
| 308 | + |
| 309 | + }); |
| 310 | + }); |
301 | 311 |
|
302 | | - }); |
| 312 | + context("and the element does not have the expected style", () => { |
| 313 | + it("throws an assertion error", () => { |
| 314 | + const { getByTestId } = render(<div className="foo bar test" style={{ display: "block", color: "blue" }} data-testid="test-div" />); |
| 315 | + const divTest = getByTestId("test-div"); |
| 316 | + const test = new ElementAssertion(divTest); |
| 317 | + |
| 318 | + expect(() => test.toHaveStyle(({ color: "red", display: "flex", border: "1px solid black" }))) |
| 319 | + .toThrowError(AssertionError) |
| 320 | + .toHaveMessage("Expected the element to have {\"color\":\"rgb(255, 0, 0)\",\"display\":\"flex\",\"border\":\"1px solid black\"} style"); |
| 321 | + |
| 322 | + }); |
| 323 | + }); |
| 324 | + }); |
303 | 325 | }); |
304 | 326 | }) |
305 | | -}) |
306 | | -}) |
|
0 commit comments