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

Document how to test for null/false #52

Closed
silvenon opened this issue Dec 5, 2015 · 20 comments
Closed

Document how to test for null/false #52

silvenon opened this issue Dec 5, 2015 · 20 comments
Assignees
Labels

Comments

@silvenon
Copy link
Contributor

silvenon commented Dec 5, 2015

I would like to add to the docs a good way to test for null, but I'm not sure which one is it.

This is the component:

import React, { PropTypes } from 'react';

const Component = (props) => {
  return props.returnNull ? null : <div />;
};

Component.propTypes = {
  returnNull: PropTypes.bool
};

export default Component;

I want to test if setting the flag to true will return null:

import React from 'react';
import { shallow } from 'reagent';
import { expect } from 'chai';
import Component from './Component';

describe('<Component />', () => {
  it('returns null if the flag is on', () => {
    const wrapper = shallow(
      <Component returnNull={true} />
    );
    expect(wrapper.text()).to.equal('');
  });
});

What is the correct way to do it? Would it perhaps be better if wrapper.type() returned null instead of throwing?

@lelandrichardson
Copy link
Collaborator

@silvenon good question.

I think I like changing wrapper.type() to return null.

At the moment, you could just check whether or not wrapper.get(0) is falsy or not

@silvenon
Copy link
Contributor Author

silvenon commented Dec 5, 2015

Thanks, I'll use that workaround in the meantime. I can send a PR if you guys decide you want that approach.

@ewendel
Copy link

ewendel commented Dec 18, 2015

I would love this as well!
Can it be that enzyme doesn't like components returning arrays of elements? I am getting the same error.

@ljharb
Copy link
Member

ljharb commented Dec 19, 2015

@ewendel doesn't a react component typically always return a single root element? i feel like React throws an invariant violation if not.

@ewendel
Copy link

ewendel commented Dec 19, 2015

No. You can return null or an empty string (perhaps also an empty array), and something in React will replace this with a <noscript>-tag before it hits the real DOM.

This is essential in components that handle conditional rendering - its a pity that enzyme apparently doesn't support this.

@ljharb
Copy link
Member

ljharb commented Dec 19, 2015

@ewendel right, i mean that either it returns "nothing" - which could be null, or an empty string, or undefined, possibly a few others - or a single component. React.render([], document.body) in 0.13 throws an invariant violation for me, as does React.render([React.createElement('div')], document.body), whereas React.render(React.createElement('div'), document.body) works. Am I missing something about "components returning arrays of elements"?

@ewendel
Copy link

ewendel commented Dec 21, 2015

You are absolutely correct, I don't know what I was talking about. I have an <If>-component that will return null if a certain prop is falsy, which gives the error.

@Villanuevand
Copy link

Thanks, @lelandrichardson your answers works for me.

@fauna5
Copy link

fauna5 commented Jul 21, 2017

rendered.getNode() seems syntactically nicer and returns null

@dupski
Copy link

dupski commented Sep 27, 2017

Also wrapper.html() comes out as null too

@trevordmiller
Copy link

trevordmiller commented Oct 18, 2017

For me, wrapper.html() returns "<div><!-- react-empty: 2 --></div>" - depends on which version of React you are using. I think the initial suggested expect(wrapper.text()).toBe('') is the best way to do this without tying yourself to a React implementation.

@dignite
Copy link

dignite commented Jan 24, 2018

When using getNode(), a warning says to use getElement() instead.

@camjc
Copy link

camjc commented Jan 25, 2018

Can you not also do
expect(wrapper.equals(null)).toBe(true);
?
Whats the cost/benefit of this over getNode?

@doque
Copy link

doque commented Feb 8, 2018

wrapper.get(0).toBeNull() works for us.

@briandipalma
Copy link

When using getNode(), a warning says to use getElement() instead.

Yep, Rich @fauna5 was using the old API, it's now

wrapper.getElement()

// In Jest
expect(wrapper.getElement()).toBe(null);

Which as mentioned is semantically clear.

@jeserkin

This comment has been minimized.

@ljharb

This comment has been minimized.

@jeserkin

This comment has been minimized.

@ahuggins
Copy link

In case this helps anyone else. I ended up doing this:

const wrap = mount(
    <SomeComponent
       someProp={whateverNeedsToBePassedThatWillCauseNullReturn} />
);

expect(wrap.html() === null).to.be.true;

Or if this is a bad idea, then let me know.

@ljharb
Copy link
Member

ljharb commented Jan 18, 2019

@ahuggins I'd suggest not using .html(), if possible - enzyme now has an isEmptyRender method that would be better.

htbkoo added a commit to htbkoo/ts-react-codepen-embed that referenced this issue Apr 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests