简体   繁体   English

如何使用玩笑来测试React组件

[英]How to test React component using jest

I have such react stateless component. 我有这样的反应无状态组件。 I need to check if lowbandwidth icon exist when isSmallVideoView is true 当isSmallVideoView为true时,我需要检查是否存在低带宽图标

export const UnmountableVideoLayout: React.StatelessComponent<VideoLayoutProps> = ({
    isSmallVideoView,
    videoComponent,
    videoOverlayComponent,
}: VideoLayoutProps) => (
    <div className={vStyles.topCenterSeparate}>
        <div className={svStyles.box}>
            {isSmallVideoView && (
                <div className={svStyles.lowBandwidthIcon}>
                    <LowBandwidthConnected />
                </div>
            )}
        </div>
    </div>
);

How I can test LowBandwidthConnected? 如何测试LowBandwidthConnected? My test doesn't work 我的测试无效

it("should render lowBandwidthIcon if isSmallVideoView is true", () => {
    const props = {
        isSmallVideoView: true,
        videoComponent: dummyVideo,
    };
    const wrapper = shallow(<UnmountableVideoLayout {...props} />);
    expect(wrapper.find(svStyles.lowBandwidthIcon).length).toBe(1);
});

According to Enzyme documentation on selectors if you want to search by CSS class, you need to concat "." 根据选择器上的酶文档,如果要按CSS类进行搜索,则需要连接“”。 with the class name. 类名。

So your find method call should look like this: 因此,您的find方法调用应如下所示:

wrapper.find('.' + svStyles.lowBandwidthIcon)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM