-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Implement matchesClass method for class name matching with RegExp #349
Conversation
@@ -525,6 +526,9 @@ export default class ReactWrapper { | |||
* @returns {Boolean} | |||
*/ | |||
hasClass(className) { | |||
if (className instanceof RegExp) { |
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.
instanceof
isn't reliable across realms, and isn't actually a spec-compliant "is regex" test. https://www.npmjs.com/package/is-regex would fix the former.
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.
Didn't know that, thanks!
I'd prefer a new API, like |
Sounds good to me, I'll make those changes ASAP. |
@ljharb implemented |
@aweary which library are you using that returns hashed css classnames? That answers the question as to why this method would be needed, but I'm wondering if there is another way to solve this. It seems like the css module library could be used in the test and you could assert on the presence of that class instead of using a I am imagining something like:
or something like that. Does that seem possible? |
That would be preferred but the issue we have relates to css-modules/webpack-demo#15. We're using Jest as our test runner and the best option we've found out there is to just ignore these imported CSS files so it doesn't throw. |
ping @lelandrichardson any updates on this? 😃 |
@aweary I'm still pretty wary on making this a core API. I understand your use case but regex matching on classnames feels like the wrong approach in general to me. In the mean time, do you think you would be able to get by with just adding your own prototype methods to enzyme for your project?
|
I think you're right, after thinking about it more I don't think it's a correct approach in the general case. Im going to think about this more and see if there's some other approach that would cover my use case and also be useful as a core API. |
Related to #1629. |
This allows you do so stuff like:
This is especially useful in instances where folks may be using CSS modules that are being scoped with appended hash values.