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

Implement matchesClass method for class name matching with RegExp #349

Closed
wants to merge 5 commits into from

Conversation

aweary
Copy link
Collaborator

@aweary aweary commented Apr 26, 2016

This allows you do so stuff like:

expect(wrapper.hasClass(/foo/)).to.be(true)

This is especially useful in instances where folks may be using CSS modules that are being scoped with appended hash values.

@@ -525,6 +526,9 @@ export default class ReactWrapper {
* @returns {Boolean}
*/
hasClass(className) {
if (className instanceof RegExp) {
Copy link
Member

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.

Copy link
Collaborator Author

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!

@ljharb
Copy link
Member

ljharb commented Apr 26, 2016

I'd prefer a new API, like matchesClass, to overloading an existing one. Polymorphic APIs are confusing and harder to maintain.

@aweary
Copy link
Collaborator Author

aweary commented Apr 26, 2016

Sounds good to me, I'll make those changes ASAP.

@aweary
Copy link
Collaborator Author

aweary commented Apr 26, 2016

@ljharb implemented matchesClass and update the test suites 👍

@aweary aweary changed the title Add RexExp support to hasClass Implement matchesClass method for class name matching with RegExp Apr 26, 2016
@lelandrichardson
Copy link
Collaborator

@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 matchesClassName function, which seems like a highly specialized method.

I am imagining something like:

import Component from './components/Component';
import styles from './styles/Component';

// ...

const wrapper = shallow(<Component />);
expect(wrapper.find(`.${styles.someClassName}`)).to.have.length(4);

or something like that. Does that seem possible?

@aweary
Copy link
Collaborator Author

aweary commented Apr 27, 2016

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.

@aweary
Copy link
Collaborator Author

aweary commented May 2, 2016

ping @lelandrichardson any updates on this? 😃

@lelandrichardson
Copy link
Collaborator

@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?

import { ShallowWrapper } from 'enzyme';
ShallowWrapper.prototype.matchesClass = function (regex) { ... };

@aweary
Copy link
Collaborator Author

aweary commented May 8, 2016

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.

@aweary aweary closed this May 8, 2016
@danny-andrews-snap
Copy link

Related to #1629.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants