Skip to content

Cannot read property 'type' of null on ''.type() #113

@teod

Description

@teod

If returning an empty string/null inside a react component and trying to call the type() function on that node will result in the following error: Cannot read property 'type' of null. This can be reproduced when trying to the use the shallow rendering filterWhere() API method.
Please see the below code snippet:

const MyComp = ({foo}) => {
    return (
          <div>
                 <MyOtherComp />
                 {foo !== 0 && (
                      <MyAnotherComp />
                  )}
          </div>
    );
});

it('should rock the world', () => {
    const wrapper = shallow(<MyComp foo={0});
    const result = wrapper.findWhere(n => (
          n.type() ===  MyAnotherComp
    );
   expect('nothing because "Cannot read property \'type\' of null" will occur');
});

In my test I want to check if the value of a specific prop is for e.g. {bar} then component should not be rendered.
Are there any workarounds for this issue ?
Thanks !

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions