【问题标题】:How to test a component with the <Router> tag inside of it?如何测试带有 <Router> 标签的组件?
【发布时间】:2020-05-20 13:10:40
【问题描述】:

关于:

嘿,目前我正在尝试使用 jest 和我的 react 组件的 react 测试库编写测试。我也使用反应路由器。

问题:

我想检查应用程序在路径更改时是否路由到正确的组件而不与单个组件交互。

例如,如果当前路径名是“/impressum”,我希望仅从 Impressum 页面获取快照。

我不知道如何将路径传递给&lt;App&gt;,以便只显示一条路线。

我要测试的组件:

import React from 'react'
import { BrowserRouter as Router, Switch, Route } from "react-router-dom"

import WeatherPage from "../WeatherPage/WeatherPage.js"
import AddWeatherPage from "../AddWeatherPage/AddWeatherPage.js"
import WeatherDetailsPage from "../WeatherDetailsPage/WeatherDetailsPage.js"
import DeleteWeatherPage from '../DeleteWeatherPage/DeleteWeatherPage.js'
import ImpressumPage from '../ImpressumPage/ImpressumPage.js'

class App extends React.Component {
  render() {
    return (
      <Router>
        <Switch>
          <Route path="/weatherDetails" component={WeatherDetailsPage} />
          <Route path="/addWeather" component={AddWeatherPage} />
          <Route path="/deleteWeather" component={DeleteWeatherPage} />
          <Route path="/impressum" component={ImpressumPage} />
          <Route path="/" component={WeatherPage} />
        </Switch>
      </Router>
    );
  }
}

export default App

我尝试了什么:

  1. 所以我已经尝试实现以下示例:https://testing-library.com/docs/example-react-router

  2. 我还尝试使用 &lt;MemoryRouter&gt; from -> import { MemoryRouter } from 'react-router' 包装 &lt;App&gt; 组件;并推送路由:https://reacttraining.com/react-router/web/guides/testing

  3. 我还尝试使用 &lt;Router&gt; from -> import { Router } from "react-router-dom" 包装 &lt;App&gt; 组件;并推送历史对象。

我的基本测试代码

下面你可以看到我用于测试的代码,我在测试时做了一些更改,但这个基本部分一直保留。

describe("snapshot-test", () => {
    it("renders component", () => {
        const { asFragment } = render(
            <App></App>
        )

        expect(asFragment()).toMatchSnapshot()
    })
})

【问题讨论】:

    标签: javascript reactjs react-router react-testing-library


    【解决方案1】:

    我刚刚找到了解决方案。

    我不得不将 Router 模拟为一个 div 并包含它的子代码。 这通过以下方式工作:

    您需要文件夹__mocks__/react-router-dom.js,其中包含以下代码:

    import React from 'react';
    
    const reactRouterDom = require("react-router-dom")
    reactRouterDom.BrowserRouter = ({children}) => <div>{children}</div>
    
    module.exports = reactRouterDom
    

    现在您可以使用MemoryRouter 来定义Route 应该指向的路径。

    App.test.js:

    import React from "react";
    import { render } from "@testing-library/react";
    import { MemoryRouter } from 'react-router-dom';
    import App from './App';
    
    
    describe("unit-test", () => {
        it("renders the right component with following path '/impressum'", () => {
            const { getByTestId } = render(
                <MemoryRouter initialEntries={['/impressum']}>
                    <App></App>
                </MemoryRouter>
            )
    
            let impressumPage = getByTestId("impressum-page")
    
            expect(impressumPage).toBeInTheDocument()
        })
    })
    

    还可以访问以下链接,我从那里得到了解决方案。 https://medium.com/@antonybudianto/react-router-testing-with-jest-and-enzyme-17294fefd303

    【讨论】:

    • RTL 中无论如何都可以禁用 react component 中的生命周期方法。因为当我执行 时,它会执行那些方法。!我需要不必要地嘲笑所有人。 @本杰明
    • @SakthiSureshAnand 不幸的是,我不知道。 - 对不起:/
    【解决方案2】:

    我认为您需要像这样在“/”的路径之前放置确切的内容

    <Switch>
          <Route path="/weatherDetails" component={WeatherDetailsPage} />
          <Route path="/addWeather" component={AddWeatherPage} />
          <Route path="/deleteWeather" component={DeleteWeatherPage} />
          <Route path="/impressum" component={ImpressumPage} />
          <Route exact path="/" component={WeatherPage} />
    </Switch>
    

    这样的意思是只有当你有这个 path="/" 的 WeatherPage 显示时,你才能像 path="/impressum" 这样添加到它,你只会看到 ImpressumPage。 希望对你有帮助:)

    【讨论】:

    • 感谢您的快速答复。不幸的是,它没有用。此外,当您不使用exact 并且最后列出了路线时(此处的顺序很重要)。然后“/”路径将捕获不包括其他路径之一的所有内容。所以当&lt;Route path="/" component={WeatherPage} /&gt;首先被写入时,每条Route都会显示到WeatherPage。但是最后写的时候,所有不是确切路径的“/weatherDetails”、“/addWeather”等都会显示WeatherPage组件。我认为我的代码由于 标记而无法工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-19
    相关资源
    最近更新 更多