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

API Addition: findByClass #1629

Closed
danny-andrews-snap opened this issue Apr 24, 2018 · 11 comments
Closed

API Addition: findByClass #1629

danny-andrews-snap opened this issue Apr 24, 2018 · 11 comments
Labels
feature request Issues asking for stuff that would be semver-minor wontfix

Comments

@danny-andrews-snap
Copy link

danny-andrews-snap commented Apr 24, 2018

I think it would be really nice to have a findByClass (or equivalent) method. 99% of the time, I select portions of the DOM by class, and when using css-modules, you end up having to do awkward things
like

const subject = wrapper.find(`.${styles.slotNumberCol}`);

to select them.

const subject = wrapper.findByClass(styles.slotNumberCol);

would be much nicer.

Even in the normal case, it would nicer (albeit slightly) to do

const subject = wrapper.findByClass('slot-number-col');

rather than

const subject = wrapper.find('.slot-number-col');
@danny-andrews-snap
Copy link
Author

danny-andrews-snap commented Apr 24, 2018

Similar to #349 but different in that it matches class names exactly, not by RegExp. I would argue this is a much more common scenario and so warrants a special method in the API. So common, in fact that there exists a special native method to do just that.

@ljharb
Copy link
Member

ljharb commented Apr 24, 2018

The "special native method" you mention is a legacy one; the modern API is to only use querySelector/querySelectorAll.

I think that "find by class" is a pattern that shouldn't be common; either way, I think adding a specialized convenience method to avoid prepending a . is complexity we probably don't want.

@ljharb ljharb added the feature request Issues asking for stuff that would be semver-minor label Apr 24, 2018
@danny-andrews-snap
Copy link
Author

danny-andrews-snap commented Apr 24, 2018

I think that "find by class" is a pattern that shouldn't be common;

What? Why? How else should you find elements?

I think adding a specialized convenience method to avoid prepending a . is complexity we probably don't want.

You're ignoring the first scenario, which is actually really common. wrapper.find(`.${styles.slotNumberCol}`); is pretty ugly to have to do every time you want to find an element by class name.

@serhii-yakymuk
Copy link

@danny-andrews-snap
Just use this, if you're using jest
https://facebook.github.io/jest/docs/en/webpack.html#mocking-css-modules

@craigkovatch
Copy link

@ljharb

The "special native method" you mention is a legacy one; the modern API is to only use querySelector/querySelectorAll.

Not sure that's the whole story. On my computer (macOS 10.12 Chrome 66) getElementsByTagName is literally 32x more performant than querySelectorAll. There's useful reasons to have both.

https://jsperf.com/getelementbyid-vs-queryselector/25

@Everettss

This comment has been minimized.

@ljharb

This comment has been minimized.

@ljharb
Copy link
Member

ljharb commented Jun 28, 2018

@craigkovatch if you're selecting enough DOM elements that 3200x even matters, you've got something subpar with your architecture :-)

@ljharb
Copy link
Member

ljharb commented Jun 28, 2018

I'm going to close this; the added complexity in enzyme isn't worth it for the simple sugar of avoiding a template literal with a leading dot.

@ljharb ljharb closed this as completed Jun 28, 2018
@ljharb ljharb added the wontfix label Jun 28, 2018
@danny-andrews-snap

This comment has been minimized.

@ljharb

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues asking for stuff that would be semver-minor wontfix
Projects
None yet
Development

No branches or pull requests

5 participants