Skip to content
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

toHaveAttribute custom matcher #43

Closed
gnapse opened this issue Apr 4, 2018 · 2 comments
Closed

toHaveAttribute custom matcher #43

gnapse opened this issue Apr 4, 2018 · 2 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@gnapse
Copy link
Member

gnapse commented Apr 4, 2018

I'm finding my self commonly checking if an element has a given attribute or not, and possibly also expecting a certain value off some attribute.

expect(getByTestId('ok-button').hasAttribute('disabled')).toBeTruthy()
expect(getByTestId('dropdown-toggle').getAttribute('aria-expanded')).toEqual('false')

I wonder if it would be acceptable to the goals and intentions of this library to provide certain other custom matchers for common tasks. In this particular case I'd be proposing a .toHaveAttribute custom matcher that could work both to assert that an element has an attribute present or not, but also to assert that the attribute is present and has a certain value:

expect(getByTestId('ok-button')).toHaveAttribute('disabled')
expect(getByTestId('dropdown-toggle')).toHaveAttribute('aria-expanded', 'false')

It could be considered syntactic sugar, but I think it makes tests more readable. Although I'd also understand if these are considered out of the scope of this library.

@kentcdodds
Copy link
Member

Yes! This would be super 👍 Thanks for the idea! Would you like to contribute to make it?

@kentcdodds kentcdodds added enhancement New feature or request help wanted Extra attention is needed labels Apr 4, 2018
@gnapse
Copy link
Member Author

gnapse commented Apr 4, 2018

I'd love to. I'm on it!

kentcdodds pushed a commit that referenced this issue Apr 5, 2018
* Add toHaveAttribute custom matcher

* Add gnapse as contributor

* Generate messages more consistent with jest's other matchers

* Add comment to matcher hint

* Add tests to cover toHaveAttribute custom matcher
julienw pushed a commit to julienw/react-testing-library that referenced this issue Dec 20, 2018
lucbpz pushed a commit to lucbpz/react-testing-library that referenced this issue Jul 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants