javascript - enzyme 安装 API 问题 : TypeError: Cannot read property 'find' of undefined

标签 javascript reactjs typescript enzyme

我正在尝试测试我的组件,但我不断收到此错误: 类型错误:无法读取未定义的属性“查找”

在将我的组件变成无状态组件之前,我的测试工作正常。据我了解,您也可以测试功能性无状态组件。我看不出我在这里做错了什么。

我的测试文件

import * as React from 'react';
import { LeftNavigation } from "../src/components/LeftNavigation";
import { mount } from 'enzyme';

describe("<LeftNavigation />", () => {
    const wrapper;

    beforeEach(() => {
        wrapper = mount(
            <LeftNavigation title="Protocol Vantage Points" path="/hello"/>
        );
    });

    it("renders the title", () => {
        expect(wrapper.find(".uk-h3 span").text()).toBe("Protocol Vantage Points");
    });

    it("renders first menu item 'On demand tests'", () => {
        expect(wrapper.find("#synth-left-nav .uk-nav li a").at(0).text()).toBe("On demand tests");
    });

    it("renders second menu item 'Feel status'", () => {
        expect(wrapper.find("#synth-left-nav .uk-nav li a").at(1).text()).toBe("Fleet status");
    });
});

我的组件

import * as React from "react";
import { NavLink, Link } from 'react-router-dom';

export interface LeftNavigationProps {
    title: string;
    path: string;
}

export class LeftNavigationItem {
    title: string;
    path: string;

    constructor(title: string, path: string) {
        this.title = title;
        this.path = path;
    }
}

export const LeftNavigation: React.StatelessComponent<LeftNavigationProps> = (props: LeftNavigationProps) => {

    const items: Array<LeftNavigationItem> = [
        new LeftNavigationItem('On demand tests', '/ondemandtests'),
        new LeftNavigationItem('Fleet status', '/fleetstatus')
    ];

    return (
        <div id="synth-left-nav" className="uk-margin-bottom">
            <h1 className="uk-h3">
                <span><Link id="synth-left-nav-title" to={props.path}>{props.title}</Link></span>
            </h1>
            <hr className="uk-width-1-1" />
            <ul className="uk-nav uk-margin-remove"> {
                items.map(function (m, index) {
                    return (
                        <li key={m.path}>
                            <NavLink activeClassName="uk-text-bold uk-active"
                                        to={props.path + m.path} replace>{m.title}</NavLink>
                        </li>
                    );
                })
            }
            </ul>
        </div>
    );
};

最佳答案

我发现了问题。测试呈现 <Link> 的组件或 <Route> ,可能会导致有关上下文的错误和警告。建议将测试包装在 <StaticRouter> 中或 <MemoryRouter> .查看此以供引用:https://github.com/ReactTraining/react-router/blob/master/packages/react-router/docs/guides/testing.md

这是我的解决方案:

import * as React from 'react';
import { LeftNavigation } from "../src/components/LeftNavigation";
import { mount } from 'enzyme';
import { MemoryRouter } from 'react-router-dom'

describe("<LeftNavigation />", () => {
    const wrapper;
    // Testing components that renders a <Link> or a <Route>, may cause 
    // errors and warnings about context. It is recommended to wrap your
    // tests in a <StaticRouter> or <MemoryRouter>

    beforeEach(() => {
        wrapper = mount(
            <MemoryRouter><LeftNavigation title="Protocol Vantage Points" path="/hello"/></MemoryRouter>
        );
    });

    it("renders the title", () => {
        expect(wrapper.find(".uk-h3 span").text()).toBe("Protocol Vantage Points");
    });

    it("renders first menu item 'On demand tests'", () => {
        expect(wrapper.find("#synth-left-nav .uk-nav li a").at(0).text()).toEqual("On demand tests");
    });

    it("renders second menu item 'Feel status'", () => {
        expect(wrapper.find("#synth-left-nav .uk-nav li a").at(1).text()).toBe("Fleet status");
    });
});

关于javascript - enzyme 安装 API 问题 : TypeError: Cannot read property 'find' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46611392/

相关文章:

javascript - 为什么在操作中使用符号是不好的做法?

typescript 通用 promise 返回类型

javascript - 使用动态模式从 JSON 加载

javascript - 为什么在执行 ajax 请求时我的页面会锁定?

javascript - 让用户在 jquery 中复制文本并重写粘贴

node.js - TypeScript: Node 11 的 "right" `target` 是什么?

javascript - 更改 Azure 上 NodeJS 应用程序的初始文件位置

javascript - 将元素存储在内存中以防止过于频繁地更新 DOM?

reactjs - 在React应用程序中将index.js重命名为index.jsx

javascript - React JS 中在特定 Prop 更改时调用组件方法的正确模式是什么?