JoeTidee
JoeTidee

Reputation: 26064

Enzyme - how to detect if a component is rendered using the shallow() method?

I have a component called Foo and the render function is such:

render() {
    if (!this.props.something) {
        return null;
    }

    return (
        <div>
            <Bar />
            <Baz />
        </div>
    );
}

I want to test the Foo component to see if the div is rendered:

test('is div rendered', () => {
    const component = shallow(
        <Foo something={false} />
    );

    expect(component.find('div').at(0).exists()).toBeFalsy();
});

but the test results in true. How can I test, using the shallow render method whether div exists or not?

Upvotes: 0

Views: 46

Answers (1)

Kenji Mukai
Kenji Mukai

Reputation: 599

When you use .find().at() it will return an object everytime. The expect assertion should be like this:

expect(component.find('div').node).toBe(undefined);

Upvotes: 1

Related Questions