-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(dom): DOM - toHaveClass #134
Open
fonsiher
wants to merge
4
commits into
main
Choose a base branch
from
feat/dom-have-class
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ import { render } from "@testing-library/react"; | |
|
||
import { ElementAssertion } from "../../../src/lib/ElementAssertion"; | ||
|
||
import { HaveClassTestComponent } from "./fixtures/haveClassTestComponent"; | ||
import { NestedElementsTestComponent } from "./fixtures/nestedElementsTestComponent"; | ||
import { SimpleTestComponent } from "./fixtures/simpleTestComponent"; | ||
import { WithAttributesTestComponent } from "./fixtures/withAttributesTestComponent"; | ||
|
@@ -69,7 +70,7 @@ describe("[Unit] ElementAssertion.test.ts", () => { | |
|
||
context("and it is an indirect child", () => { | ||
it("returns the assertion instance", async () => { | ||
const { findByTestId } = render(<NestedElementsTestComponent/>); | ||
const { findByTestId } = render(<NestedElementsTestComponent />); | ||
const grandparent = await findByTestId("grandparent"); | ||
const child = await findByTestId("child"); | ||
const grandparentTest = new ElementAssertion(grandparent); | ||
|
@@ -84,7 +85,7 @@ describe("[Unit] ElementAssertion.test.ts", () => { | |
|
||
context("and it is a deeply nested child", () => { | ||
it("returns the assertion instance", async () => { | ||
const { findByTestId } = render(<NestedElementsTestComponent/>); | ||
const { findByTestId } = render(<NestedElementsTestComponent />); | ||
const grandparent = await findByTestId("grandparent"); | ||
const deepChild = await findByTestId("deep-child"); | ||
const grandparentTest = new ElementAssertion(grandparent); | ||
|
@@ -101,7 +102,7 @@ describe("[Unit] ElementAssertion.test.ts", () => { | |
context("when element is NOT contained in ancestor element", () => { | ||
it("throws an assertion error", async () => { | ||
const notChildElement = document.createElement("span"); | ||
const { findByTestId } = render(<NestedElementsTestComponent/>); | ||
const { findByTestId } = render(<NestedElementsTestComponent />); | ||
const grandparent = await findByTestId("grandparent"); | ||
const grandparentTest = new ElementAssertion(grandparent); | ||
|
||
|
@@ -172,4 +173,67 @@ describe("[Unit] ElementAssertion.test.ts", () => { | |
}); | ||
}); | ||
}); | ||
|
||
describe(".toHaveClass", () => { | ||
context("when the element has the the expected class", () => { | ||
it("returns the assertion instance", async () => { | ||
const { findByTestId } = render(<HaveClassTestComponent />); | ||
const divTest = await findByTestId("classTest"); | ||
divTest.className = "foo bar"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to use |
||
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\""); | ||
}); | ||
}); | ||
|
||
context("when the element does not have the expected class ", () => { | ||
it("throws an assertion error", async () => { | ||
const { findByTestId } = render(<HaveClassTestComponent />); | ||
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); | ||
}); | ||
}); | ||
|
||
context("when the element element has the the exact matching expected class", () => { | ||
it("returns the assertion instance", async () => { | ||
const { findByTestId } = render(<HaveClassTestComponent />); | ||
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\""); | ||
}); | ||
}); | ||
|
||
context("when the element does not have the exact matching expected class ", () => { | ||
it("throws an assertion error", async () => { | ||
const { findByTestId } = render(<HaveClassTestComponent />); | ||
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); | ||
}); | ||
}); | ||
}); | ||
|
||
}); |
9 changes: 9 additions & 0 deletions
9
packages/dom/test/unit/lib/fixtures/haveClassTestComponent.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { ReactElement } from "react"; | ||
|
||
export function HaveClassTestComponent(): ReactElement { | ||
return ( | ||
<div data-testid="classTest"> | ||
Test text inside a div | ||
</div> | ||
); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about making more than one assertion instead of adding the options param? To me, it'll provide more readability if we have the following: