-
-
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
Add loose equals and loose contains functions on ShallowWrapper #362
Add loose equals and loose contains functions on ShallowWrapper #362
Conversation
I like this, with the exception of the API name 😄. Maybe something like |
|
||
#### Returns | ||
|
||
`Boolean`: whether or not the current wrapper has a node anywhere in it's render tree that looks |
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.
should be "its" not "it's"
I definitely am not a fan of the names. Does this comparison ignore all props, or just |
@ljharb I believe it ignores all props that aren't present on the expression passed into |
so something like |
# `.rightContains(nodeOrNodes) => Boolean` | ||
|
||
Returns whether or not the current wrapper has a node anywhere in it's render tree that looks like | ||
the one passed in. Equality is based on the expected element (`nodeOrNodes`) and not on the wrapper. It will determine if an element in the wrapper `looks like` the expected element by checking if all props of the expected element are present on the wrappers element and equals to each other. |
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.
"looks like" here should be in curly quotes, not backticks, since it's not code
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.
If you're going for emphasis, try italics 😄
|
||
#### Arguments | ||
|
||
1. `nodeOrNodes` (`ReactElement|Array<ReactElement>`): The node or array of nodes whose presence you are detecting in the current instance's |
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.
can we be very very explicit about whether this has some
or every
semantics?
I almost would prefer three separate methods: containsMatchingElement
, containsAllMatchingElements
, and containsAnyMatchingElements
- taking a node, an array, and an array, respectively - rather than risking any confusion about what conditions cause true
or false
to be returned. Thoughts?
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.
It actually just work like wrapper.contains(nodeOrNodes)
except the "source of truth" is inverted. It's not A.contains(B) anymore but B.contains(A) instead.
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.
I don't think that's accurate. shallow(<A a><B b /></A>).containsMatchingElement(<B />)
isn't the same thing as shallow(<B />).contains(<A a><B b /></A>)
.
I see this as simply "contains", but using the element-to-be-found's prop list rather than the element-being-searched.
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.
I see this as simply "contains", but using the element-to-be-found's prop list rather than the element-being-searched.
Yes, that's what I was trying to explain :-)
Nothing immediately jumps out at me. Is this perhaps something we also want exposed on |
Yeah, I thought exactly the same thing. I will take a look at |
@ljharb I just added the contains* function to the |
@ljharb it should be good now :-) |
@lelandrichardson @ljharb do you think we can get this merged soon? |
@aweary it needs to be rebased on latest master :-) |
Yeah, I meant more in terms of whether you guys will be accepting it. If that's the case then @mathieuancelin it'd be great if you could rebase 😄 |
@aweary it's rebased, just waiting on CI :-) |
Maybe it's just me, but it says it's still out-of-date with master 😬 |
@aweary it seems good now, but travis seems a bit overloaded at the moment |
3394025
to
dbe677b
Compare
@aweary It should be okay now. I don't even know where does this merge come from. Anyway, it seems to be fixed |
I had considered a similar API before, but never ended up writing it. I think this is quite useful in practice, though it always feels a little messy to me in my head. I think it's a reasonable addition though. What do we think about the names |
I like the methods but I don't like the name "fuzzy" - the current names were my suggestion and atm I still find them to be the clearest. |
The problem I have with the current names is that they don't seem to indicate to me that they are not "strict". using the "fuzzy" prefix makes it obvious that this is not strict equality. |
To me, "matching" is a non-strict thing - since in every test framework, "to match" generally means "is kind of the same as". Another common word to describe non-strict-equality is "loose equality". "Fuzzy" might imply that |
de36e4f
to
c250345
Compare
Considering test libraries use the OK. Let's do this! We've got some fresh conflicts. Mind rebasing and fixing? Thanks! |
c250345
to
102d631
Compare
@lelandrichardson it should be good now :-) |
Thanks @lelandrichardson :-) |
Updated to make the function more clear. I was looking for this method but had not realised it existed until I dug into the PRs (enzymejs#362)
Updated to make the function more clear. I was looking for this method but had not realised it existed until I dug into the PRs (enzymejs#362)
This PR add two functions on the
ShallowWrapper
to perform loose equality and loose contains check on a tree. The functions are named.rightContains()
and.rightEquals()
because the equality is based on the expected element (the right one) instead of the wrapped element (the left one). The function names are not very good and I'll be happy to change it for something more relevant and meaningful.I added these functions because it is really really handy to write clean tests that check just the necessary parts of the tree for the test, i.e. "I want to check if that particular node is present in my tree, but I don't care about its style or onClick handler".
It works by checking if at least, all the props (including children) of the expected element are present on the wrapper element and equal to each other. It is based on the
Utils.nodeEqual
function with a little tweak on the props length comparison.To use it, you just have to write something like :
As the contributors guide suggest, I added some documentation and some tests that should pass with all versions of react.
These kind of feature are suggested on some issues like #204 or #273