简体   繁体   English

使用svg渲染图像标签时,用酶反应组件单元测试失败

[英]React component unit test with enzyme fails when rendering Image tag with svg

I am using React with Typescript and using Jest and Enzyme for unit testing. 我正在使用带有Typescript的React,并使用Jest和Enzyme进行单元测试。 I am trying to unit test the following component (simplified, of course): 我正在尝试对以下组件进行单元测试(当然是简化的):

import image1 from 'path/to/image1.svg';
import image2 from 'path/to/image2.svg';

export const ShowImage: React.StatelessComponent<{isTab1: boolean, someId: number, isTab2: boolean }> = ({ isTab1, someId, isTab2}) => (
    <td>
        {isTab1 !== true && someId === 1 && !isTab2 ?
            <Image1 />
            :
            null
        }
        {isTab1 !== true && someId === 2 && !isTab2 ?
            <Image2 />
            :
            null
        }
    </td>
);

export const Image1 = () => (
    <Image src={image1} />
);

export const Image2 = () => (
    <Image src={image2} />
);

The Image tag is a React Bootstrap tag. Image标签是一个React Bootstrap标签。 I am testing the ShowImage component in a test file like so: 我正在像这样的测试文件中测试ShowImage组件:

import * as React from 'react';
import * as Enzyme from 'enzyme';
import { mount, shallow } from 'enzyme';
import * as Adapter from 'enzyme-adapter-react-15';

Enzyme.configure({ adapter: new Adapter() });

function setupShowImageComponent(isTab1: boolean, someId: number, isTab2: boolean): any {
    const props = {
        isTab1: isTab1,
        someId: someId,
        isTab2: isTab2
    };

    const enzymeWrapper = mount(<ShowImage {...props} />);

    return {
        props,
        enzymeWrapper
    }
}

describe('Test ShowImage Component', () => {
    it('false, 1, false', () => {
        const { enzymeWrapper } = setupShowImageComponent(false, 1, false);

        expect(enzymeWrapper.find('td')).toHaveLength(1);    
        expect(enzymeWrapper.find('Image1')).toHaveLength(1); // fails: receives length 0
        expect(enzymeWrapper.find('Image2')).toHaveLength(0);
    });

    it('false, 2, false', () => {
        const { enzymeWrapper } = setupShowImageComponent(false, 2, false);

        expect(enzymeWrapper.find('td')).toHaveLength(1);    
        expect(enzymeWrapper.find('Image1')).toHaveLength(0);
        expect(enzymeWrapper.find('Image2')).toHaveLength(1); // fails: receives length 0
    });
});

Both of these tests fail. 这两项测试均失败。 Other React components that I am testing pass fine but this one eludes me. 我正在测试的其他React组件都可以通过,但这使我难以理解。 Any help would be greatly appreciated. 任何帮助将不胜感激。 It is important to note that this used to work but when I migrated my code over to use TypeScript, these two tests failed. 重要的是要注意这曾经起作用,但是当我将代码迁移到使用TypeScript时,这两项测试均失败了。

UPDATE UPDATE

After some experimentation, if I test for the existence of the Image tag rather than Image1 or Image2 it works. 经过一些试验后,如果我测试Image标签而不是Image1Image2的存在,那么它将起作用。 It seems that it does not recognize the functional component but rather its children instead? 似乎它不识别功能组件,而是识别其子组件? I'm confused here. 我在这里很困惑。 How can I test if it's specifically Image1 or Image2 instead of just testing Image ? 如何测试它是专门用于Image1还是Image2而不是仅仅测试Image Again, this worked in with Javascript but is not working with TypeScript. 同样,这适用于Javascript,但不适用于TypeScript。

I found a solution that works. 我找到了可行的解决方案。 Instead of doing: 而不是做:

expect(enzymeWrapper.find('Image1')).toHaveLength(1); // fails

do: 做:

expect(enzymeWrapper.find(Image1)).toHaveLength(1); // passes

I'm not sure if this is the best solution but it's something that gets me past my issues. 我不确定这是否是最佳解决方案,但这使我摆脱了困境。

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

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