Skip to content

Commit

Permalink
Add inverted errors and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fonsiher committed Oct 4, 2024
1 parent 03e9e4a commit 6827334
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
6 changes: 6 additions & 0 deletions packages/dom/src/lib/ElementAssertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ export class ElementAssertion<T extends Element> extends Assertion<T> {
}

public toHaveClass(classNames: string | string[], options: { exact?: boolean; } = {}): this {
if (!(this.actual instanceof HTMLElement)) {
throw new AssertionError({
actual: this.actual,
message: "Expected an HTML element with a 'className' property",
});
}
const actualClassList = this.actual.className.split(/\s+/).filter(Boolean);
const expectedClassList = Array.isArray(classNames) ? classNames : [classNames];
const { exact = false } = options;
Expand Down
16 changes: 16 additions & 0 deletions packages/dom/test/unit/lib/ElementAssertion.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,12 @@ describe("[Unit] ElementAssertion.test.ts", () => {
const divTest = await findByTestId("classTest");
divTest.className = "foo bar";
const test = new ElementAssertion(divTest);

expect(test.toHaveClass("foo")).toBeEqual(test);

expect(() => test.not.toHaveClass("foo"))
.toThrowError(AssertionError)
.toHaveMessage("Expected the element to NOT have class(es): \"foo\"");
});
});

Expand All @@ -191,9 +196,12 @@ describe("[Unit] ElementAssertion.test.ts", () => {
const divTest = await findByTestId("classTest");
divTest.className = "foo";
const test = new ElementAssertion(divTest);

expect(() => test.toHaveClass("bar"))
.toThrowError(AssertionError)
.toHaveMessage("Expected the element to have class(es): \"bar\"");

expect(test.not.toHaveClass("bar")).toBeEqual(test);
});
});

Expand All @@ -203,7 +211,12 @@ describe("[Unit] ElementAssertion.test.ts", () => {
const divTest = await findByTestId("classTest");
divTest.className = "foo bar";
const test = new ElementAssertion(divTest);

expect(test.toHaveClass(["foo", "bar"], { exact: true })).toBeEqual(test);

expect(() => test.not.toHaveClass(["foo", "bar"], { exact: true }))
.toThrowError(AssertionError)
.toHaveMessage("Expected the element to NOT have exactly these classes: \"foo bar\"");
});
});

Expand All @@ -213,9 +226,12 @@ describe("[Unit] ElementAssertion.test.ts", () => {
const divTest = await findByTestId("classTest");
divTest.className = "foo bar extra";
const test = new ElementAssertion(divTest);

expect(() => test.toHaveClass(["foo", "bar"], { exact: true }))
.toThrowError(AssertionError)
.toHaveMessage("Expected the element to have exactly these classes: \"foo bar\"");

expect(test.not.toHaveClass(["foo", "bar"], { exact: true })).toBeEqual(test);
});
});
});
Expand Down

0 comments on commit 6827334

Please sign in to comment.