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

hasClassName throws Error if component has property className which is not a string #453

Closed
wants to merge 2 commits into from

Conversation

trmjoa
Copy link

@trmjoa trmjoa commented Jun 13, 2016

A proposal to fix #452

I now see that there are PR's pending for related issues (#448). I'm not sure if the proposed fixes/code changes are overlapping.

If you feel its a viable approach, but want changes, please advise me on how to proceed and I will give it a try.

@trmjoa trmjoa force-pushed the bug/452-hasClassName_throws branch from fdcd2d5 to f52830a Compare June 13, 2016 10:47
@aweary
Copy link
Collaborator

aweary commented Jun 13, 2016

Thanks for the PR @Bannan! I think we will be going with #448 since it was opened first, but we appreciate the effort. We also don't want to simply return false when the className is an object, as that's essentially a silent failure, which is what we had before 2.3.0.

@aweary aweary closed this Jun 13, 2016
@aweary aweary reopened this Jun 14, 2016
@@ -27,6 +27,9 @@ export function childrenOfNode(node) {

export function hasClassName(node, className) {
let classes = propsOfNode(node).className || '';
if (node && typeof node.type === 'function' && typeof classes !== 'string') {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just do the typeof check instead of all these checks on node? No matter what, if classes is not a string we should exit early since the next operation is a string operation.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have the overview of the whole codebase, but I would consider it good if the method throws if its a native component and className is not a string. That should not be a silent error?
So;
node && typeof node.type === 'function' if that is true the node currently being processed is a custom component. If its not a custom component e.g a regular div, span etc, it will continue and have the same behavior as today, that is throw a error if you have passed something else then a string to className.

That's my take on it. What do you think? Your decision, of course :)

@trmjoa
Copy link
Author

trmjoa commented Jun 15, 2016

Updated the PR according to your comments. Didn't update the code inside hasClassName itself as I just wanted to clarify the behavior first. Whats your policy on patches. Do you want me to rebase and squash the commits into one?

@aweary
Copy link
Collaborator

aweary commented Jul 5, 2016

@Bannan do you see this conflicting with #448 in any way?

@trmjoa
Copy link
Author

trmjoa commented Jul 8, 2016

Well... We're not using the mounted version of enzyme that much yet. But from looking at the code in MountedTraversal.js and the patch in #448 they both address issues around the class checking. The #448 patch just adds support for checking the classList property first if it exists, and if it doesn't and className is a object it uses the baseVal property if it exists.

The problem described in this issue (#453) doesn't exist in the mounted part of the code because in MountedTraversal.js a check for !isDOMComponent(inst) is executed. It will return false for custom components, I also do this in my patch so the behavior will be consistent with the version used in the mounted part.

The only "conflict" I see is that the patch in #448 should maybe be applied to the ShallowTraversal.js also, or at least ported. So that the behavior is consistent. Then both methods that check for classnames has the following behavior:

  1. Check that this is a node representing a native html element(div,span, p etc); Otherwise return false
  2. Check the classList property, if present check if the classname is in it and return the result.
  3. If the instance doesn't have a classList property, check the className property. If it exists and is a object assign the objects baseVal property to the classes variable(this is to suppoert SVG elements).
  4. Do the check as it is done today and return the result

@trmjoa
Copy link
Author

trmjoa commented Sep 29, 2016

@aweary Are you interested in getting this in, or should I close the issue?

@lencioni
Copy link
Contributor

@aweary what do you think we should do with this PR?

@aweary
Copy link
Collaborator

aweary commented Oct 11, 2016

I don't think we should throw if it's not a string, using an object with an overloaded toString method is perfectly valid. https://jsfiddle.net/tLs53LnL/

We should probably just coerce className to a string before calling replace.

@ljharb
Copy link
Member

ljharb commented Oct 11, 2016

This looks worthwhile to get in in some form - either way it needs to be rebased, now that #448 is already merged.

@aweary
Copy link
Collaborator

aweary commented Oct 11, 2016

@ljharb looks like we posted at the same time, ping just in case you missed my comment 🙂

@ljharb
Copy link
Member

ljharb commented Oct 11, 2016

Coercing to string (with String() only) seems reasonable also.

@aweary
Copy link
Collaborator

aweary commented Oct 11, 2016

@trmjoa do you want to update your PR to just coerce className to a string using String()?

@trmjoa
Copy link
Author

trmjoa commented Oct 12, 2016

I will rebase and update the PR over the weekend.

@ljharb
Copy link
Member

ljharb commented Aug 7, 2018

The linked issue was closed with 3070850

@ljharb ljharb closed this Aug 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

hasClassName throws Error if component has property className which is not a string
4 participants