Skip to content

feat(dom): DOM - ttoHaveClass , toHaveAllClasses and toHaveAnyClass #134

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

fonsiher
Copy link
Contributor

@fonsiher fonsiher commented Oct 3, 2024

Added the toHaveClass matcher.

@fonsiher fonsiher marked this pull request as ready for review October 4, 2024 20:17
@fonsiher fonsiher force-pushed the feat/dom-have-class branch from 6827334 to 18f9d98 Compare October 4, 2024 20:18
Copy link
Contributor

@ChristianSama ChristianSama left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great @fonsiher. Thank you!

Copy link
Member

@JoseLion JoseLion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good so far, but I think we can improve things if we create 3 assertions instead of one 🙂

* - `exact` (boolean): When true, checks for an exact match of all classes.
* @returns the assertion instance.
*/
public toHaveClass(classNames: string | string[], options: { exact?: boolean; } = {}): this {
Copy link
Member

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:

expect(elem).toHaveClass("w-full"); // single arg

expect(elem).toHaveAnyClass("w-full", "flex"); // variadic args

expect(elem).toHaveAllClasses("w-full", "flex", "gap-8"); // variadic args

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @JoseLion this is actually a great idea to improve readability. I just added these new matches.

@fonsiher fonsiher force-pushed the feat/dom-have-class branch from 2f3d0b5 to 6e6062d Compare April 3, 2025 17:30
@fonsiher fonsiher force-pushed the feat/dom-have-class branch from 6e6062d to a831c90 Compare April 3, 2025 17:34
@fonsiher fonsiher requested review from kdquistanchala, KevinJ1998 and JoseLion and removed request for matycarolina April 3, 2025 17:35
Copy link
Contributor

@suany0805 suany0805 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rename the PR to contain all the toHaveClass , toHaveAllClasses, toHaveAnyClass 👀 please?

Copy link
Member

@JoseLion JoseLion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! I left some comments and suggestions. Let me know if you have any questions 🙂

/**
* Asserts that the element has the specified class.
*
* @param className - The class name to check.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to add the hyphen. IDEs add it for us:

Suggested change
* @param className - The class name to check.
* @param className The class name to check.

it("returns the assertion instance", async () => {
const { findByTestId } = render(<HaveClassTestComponent />);
const divTest = await findByTestId("classTest");
divTest.classList.add("foo", "bar");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can pass a className prop to the <HaveClassTestComponent /> component to avoid using classList.add(..), wdyt?

Comment on lines +3 to +9
export function HaveClassTestComponent(): ReactElement {
return (
<div data-testid="classTest">
{"Test text inside a div"}
</div>
);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export function HaveClassTestComponent(): ReactElement {
return (
<div data-testid="classTest">
{"Test text inside a div"}
</div>
);
}
export function HaveClassTestComponent(): ReactElement {
return (
<div data-testid="classTest">
{"Test text inside a div"}
</div>
);
}


export function HaveClassTestComponent(): ReactElement {
return (
<div data-testid="classTest">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need the data-testid? The getByTestID(..) query should be our last resort when finding elements. For instance, this element can be found with the query below instead:

getByText("Test text inside a div")

@fonsiher fonsiher changed the title feat(dom): DOM - toHaveClass feat(dom): DOM - ttoHaveClass , toHaveAllClasses and toHaveAnyClass Apr 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants