如何测试带有 <Router> 标签的组件?

How to test a component with the <Router> tag inside of it?

关于:

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

问题:

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

因此,例如,如果当前路径名为“/impressum”,我想从 Impressum 页面获取快照。

我不知道如何将路径传递给 <App> 以便只显示一条路线。

我要测试的组件:

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. 我还尝试用 <MemoryRouter> 包装 <App> 组件 from -> import { MemoryRouter } from 'react-router';并推送路线:https://reacttraining.com/react-router/web/guides/testing

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

我的基本测试代码

下面是我测试用的代码,我在测试的时候做了一些修改,但是这个基本部分一直保留着。

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

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

我认为你需要像这样在 '/' 的路径前加上 exact

<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 show 我添加到它就像 path="/impressum" 你只会看到 ImpressumPage。 希望对你有帮助:)

我刚刚找到了解决方案。

我必须将 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()
    })
})

也访问下面的link,我从那里得到了解决方案。 https://medium.com/@antonybudianto/react-router-testing-with-jest-and-enzyme-17294fefd303