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

component.state is null #1395

Closed
Jauny opened this issue Nov 28, 2017 · 8 comments
Closed

component.state is null #1395

Jauny opened this issue Nov 28, 2017 · 8 comments

Comments

@Jauny
Copy link

Jauny commented Nov 28, 2017

Hi, I have state initialized in my component's ES6 class constructor, but during test state is null;

class Card extends PureComponent {
  constructor(props) {
    super(props);
    
    this.styles = props.styles;
    this.state = {
      isCollapsed: this.props.isCollapsed || false  
    };

    this.collapse = this.collapse.bind(this);
  }

  [...]
test('<Card/> state', t => {
  const collapsedCard = mount(<Card isCollapsed={true}/>);
  t.equal(collapsedCard.state('isCollapsed'), true, 'collapsed when props.isCollapsed');

  t.end();
});

and I get this error

TypeError: Cannot read property 'isCollapsed' of null

I use

  • "enzyme": "^3.2.0"
  • "enzyme-adapter-react-15": "^1.0.5"
  • "tape": "^4.8.0"
@ugogo
Copy link

ugogo commented Nov 28, 2017

Use props instead of this.props in the constructor

this.state = {
  isCollapsed: this.props.isCollapsed || false
};

->

this.state = {
  isCollapsed: props.isCollapsed || false
};

this.props does not exist yet in the constructor

@Jauny
Copy link
Author

Jauny commented Nov 28, 2017

oh good point I updated. Although I still have the same issue.

even just making the state static won't work

this.state = {
  isCollapsed: true
};

@Jauny
Copy link
Author

Jauny commented Nov 28, 2017

Ah nevermind, I was using a HOC - found discussion in #431 and will export my inner component separately!

@Jauny Jauny closed this as completed Nov 28, 2017
@ljharb
Copy link
Member

ljharb commented Nov 29, 2017

@Jauny you shouldn't need to do that; use .dive() instead with shallow.

@Jauny
Copy link
Author

Jauny commented Nov 29, 2017

@ljharb wow awesome thanks a lot!

@dabit1
Copy link

dabit1 commented Feb 1, 2018

I finally found a very good solution to get a wrapper from a decorated component. For shallowed components you can use dive() but there is no similar method for mounted components. This is the solution I did:

const wrapper = mount(shallow(<MyComponent />).get(0))

Works like a charm :)

@shroukkhan
Copy link

@dabit1 const wrapper = mount(shallow(<MyComponent />).get(0)) fails with Warning: Failed prop type: Invalid prop props of type function supplied to <<anonymous>>, expected object.
in Unknown

@tommyz0123
Copy link

tommyz0123 commented Nov 8, 2019

const wrapper = mount(shallow(<MyComponent store={store}/>).get(0), { context: { store }, childContextTypes: { store: PropTypes.objectOf(PropTypes.any), }, });

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

No branches or pull requests

6 participants