Skip to content

Commit 2481a74

Browse files
committed
Add: refactored test
1 parent b0573a5 commit 2481a74

File tree

1 file changed

+55
-35
lines changed

1 file changed

+55
-35
lines changed

packages/dom/test/unit/lib/ElementAssertion.test.tsx

Lines changed: 55 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AssertionError, expect } from "@assertive-ts/core";
2-
import { getByTestId, render } from "@testing-library/react";
2+
import { render } from "@testing-library/react";
33

44
import { ElementAssertion } from "../../../src/lib/ElementAssertion";
55

@@ -257,50 +257,70 @@ describe("[Unit] ElementAssertion.test.ts", () => {
257257
const test = new ElementAssertion(divTest);
258258

259259
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+
263263
expect(test.not.toHaveAllClasses("foo", "bar", "baz")).toBeEqual(test);
264264
});
265265
});
266266
});
267-
267+
268268
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+
});
282283

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);
284291

292+
});
293+
});
285294
});
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);
293295

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);
299304

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+
});
301311

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+
});
303325
});
304326
})
305-
})
306-
})

0 commit comments

Comments
 (0)