酶 - 如何使用shallow()方法检测组件是否呈现?

时间:2017-10-05 16:55:40

标签: reactjs enzyme

我有一个名为Foo的组件,render函数是这样的:

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

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

我想测试Foo组件以查看是否呈现了div

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

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

但测试结果为true。如何使用shallow渲染方法测试div是否存在?

1 个答案:

答案 0 :(得分:1)

当你使用.find()。at()时,它会每次都返回一个对象。期望断言应该是这样的:

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