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

Add an isEmptyRender() method to both ShallowWrapper and ReactWrapper #339

Merged
merged 5 commits into from
May 25, 2016
Merged

Add an isEmptyRender() method to both ShallowWrapper and ReactWrapper #339

merged 5 commits into from
May 25, 2016

Conversation

milesj
Copy link
Contributor

@milesj milesj commented Apr 20, 2016

Why

Added an isNull() method to both ShallowWrapper and ReactWrapper,
making it easier in tests to detect when a component renders nothing;
either a null or undefined value.

I chose the name isNull() since isEmpty() was used for other situations.
It's not as descriptive as it could be, so open to suggestion.

Tested

Unit tests!

*
* @returns {boolean}
*/
isNull() {

Choose a reason for hiding this comment

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

I feel like this name too specifically implies that the component did return null in its render method, even though return undefined (either explicitly or implicitly) should also be fine.

@milesj
Copy link
Contributor Author

milesj commented Apr 20, 2016

Seems like a lot changed internally during the React 15 upgrade. I've not been able to find an implementation that works across all versions. Using this.type() doesn't work for all use cases, nor does findDOMNode(), so I had to use a conditional.

@lelandrichardson
Copy link
Collaborator

I think we need to duplicate all of these tests for undefined and false, which are also things that are "rendering nothing" that we would probably want this function to behave identically for. Looking at the code, I don't think this will pass right now.

The other question for me is the name. isNull() is pretty directly indicating that we are testing for null, which might not always be the case (other things can return ReactNodeTypes.EMPTY).

That said, I'm not really sure what the right name is. Maybe it should be isEmpty() or isNothing()?

@milesj
Copy link
Contributor Author

milesj commented Apr 21, 2016

Perhaps something like hasRenderedEmpty() or hasRenderedNothing()?

@milesj
Copy link
Contributor Author

milesj commented Apr 21, 2016

Ok, I changed it to isEmptyRender(), to be a bit more explicit. I also added false based tests and tried to add undefined, but supposedly you can't, as React errors with "Invariant Violation: Unexpected node: undefined" or "Error: Invariant Violation: Foo.render(): A valid ReactComponent must be returned. You may have returned undefined, an array or some other invalid object.".

@milesj milesj changed the title Add an isNull() method to both ShallowWrapper and ReactWrapper Add an isEmptyRender() method to both ShallowWrapper and ReactWrapper Apr 21, 2016
* 'master' of github.com:airbnb/enzyme:
  Update docs to externalize react/addons for newer versions of React (#338)
@lelandrichardson
Copy link
Collaborator

Hey @milesj , you're right. My bad - I guess undefined is not valid here.

We also want to ensure that this returns false in the proper cases though.

false => true
null => true
true => false
0 => false
<div /> => false
<Foo /> => false

At the moment, isEmptyRender() { return true; } would pass all of these tests :)

I think at that point this will be ready to merge. Not super psyched about the name, but can't think of anything better at the moment.... so at least it's explicit.

@milesj
Copy link
Contributor Author

milesj commented Apr 22, 2016

I was a bit surprised about the undefined thing as well. I could of sworn it allowed it at some point.

I'll look into adding some truthy tests.

@ljharb
Copy link
Member

ljharb commented Apr 22, 2016

Don't forget about NaN and the empty string, just to make sure the other falsy values are covered (both should return false i believe)

@aweary
Copy link
Collaborator

aweary commented May 16, 2016

@milesj any updates on this? 😄

@milesj
Copy link
Contributor Author

milesj commented May 16, 2016

Oops, thanks for reminding me. Been pretty swamped at work. Will try to get it this week.

@aweary
Copy link
Collaborator

aweary commented May 16, 2016

No worries, don't feel pressured/rushed! Thanks for the contribution, looking forward to seeing how this turns out 👍

@milesj
Copy link
Contributor Author

milesj commented May 16, 2016

Added a ton of return value checks.

Miles Johnson added 2 commits May 19, 2016 12:41
* 'master' of github.com:airbnb/enzyme: (30 commits)
  Update Jest docs. (#399)
  DOC: Clarify that .simulate() calls prop (#354)
  Beef up tests for SFCs and optimize npm test scripts
  Use public API for batchedUpdates
  ShallowRenderer supports batched updates
  grammar.. so trivial, sorry
  Id selector not working when combined with a tag selector (#314)
  Fix mount::simulate() signature (#381)
  Fix documentation code example mistake
  [lgtm] Fix capitalization of maintainer name, drop approvals to 1
  v2.3.0
  Add lgtm.co config and MAINTAINERS file.
  added error checking for state, setState, context, instance methods on ShallowWrapper (#373)
  equals should skip null and undefined nodes fix #151 (#192)
  Fix incorrect docs for type
  add docs related methods for .at() and .get() (#372)
  [Tests] remove io.js exclusions
  Add `name` method (#335)
  Test on node v6
  Add loose equals and loose contains functions on ShallowWrapper (#362)
  ...
making it easier in tests to detect when a component renders nothing;
either a null or undefined value.

Fixed inconsistencies between React 0.14 and 15.

Renamed isNull() to isEmptyRender(). Added tests for false checks.

Added itWithData() and a better way to test possible falsey values for React render().

Added tests for components and element return types.

Wrapped empty render data in a function.

Simplify the empty render data checks.
@milesj
Copy link
Contributor Author

milesj commented May 19, 2016

@lelandrichardson I added tests that check null/false, as well as React components/elements. I originally had it testing every possible falsey value, but then it just barfed all over the output, as seen by the screenshot. I don't really see the benefit of testing all the falsey values as React doesn't allow anything besides null/false.

screen shot 2016-05-19 at 12 37 07 pm

@aweary
Copy link
Collaborator

aweary commented May 19, 2016

@milesj I think we should only be testing valid "empty" return values 👍

@ljharb
Copy link
Member

ljharb commented May 19, 2016

What about NaN, and the empty string? Does React complain about those too?

@milesj
Copy link
Contributor Author

milesj commented May 19, 2016

I tested a ton of values, all of which are: null, false, undefined, NaN, array, empty array, object, empty object, zero, string, empty string, React element/component, DOM element, etc.

All of them threw those warnings excluding null/false/React.

@milesj
Copy link
Contributor Author

milesj commented May 19, 2016

Screenshot I took.
screen shot 2016-05-19 at 2 22 17 pm

@aweary
Copy link
Collaborator

aweary commented May 19, 2016

LGTM

@milesj
Copy link
Contributor Author

milesj commented May 24, 2016

@lelandrichardson Anything else you'd like done on this? Besides hitting the "Update Branch" button :P

@aweary
Copy link
Collaborator

aweary commented May 24, 2016

@milesj can you squash this to get read of all those merge commits? I think we can merge this after that 👍

@milesj
Copy link
Contributor Author

milesj commented May 25, 2016

Wouldn't Github's new merge squash feature suffice here?

I've tried rebasing the history a bit, but it's turned out troublesome since the commits happened at different points in history. I'll mess around some more.

@aweary
Copy link
Collaborator

aweary commented May 25, 2016

Does Github do that now? I didn't realize, thats neat. I'll merge. Thanks again @milesj!

@aweary aweary merged commit 6845529 into enzymejs:master May 25, 2016
@milesj milesj deleted the add-isnull-check branch May 25, 2016 03:04
@ljharb
Copy link
Member

ljharb commented May 25, 2016

(merge-squash is fine when the PR author has issues with rebasing, but in general we do strongly prefer rebased and non-squash-merged commits. github's squash-merge breaks the history between the PR and master)

@milesj
Copy link
Contributor Author

milesj commented May 25, 2016

Yeah, GitHub's automatic "Update Branch" is useful, but annoying history wise. Will probably do it manually going forward.

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.

5 participants