React-testing-library: toHaveAttribute custom matcher

Created on 4 Apr 2018  路  2Comments  路  Source: testing-library/react-testing-library

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.

enhancement help wanted

Most helpful comment

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

All 2 comments

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

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

Was this page helpful?
0 / 5 - 0 ratings