【问题标题】:I want to test if the routes exist我想测试路线是否存在
【发布时间】:2019-11-11 09:30:07
【问题描述】:

我想测试组件中是否存在路由。应用应在特定路径上渲染特定组件。

App.js

    return (
      <Router>
        <Header jsTrendingTopics={this.props.data} />
        <Switch>
          <Route exact path="/" render={() => <Dashboard jsTrendingTopics={this.props.data} />} />
          <Route exact
            path="/language/:languagename"
            render={(props) => this.handleTopic(props)
            }
          />
        </Switch>
      </Router>
    )
  }

我希望测试返回应该在该特定路由上呈现的组件。

【问题讨论】:

标签: javascript reactjs jestjs


【解决方案1】:

我认为 MemoryRouterinitialEntries={['/]} 是您正在寻找的。​​p>


import Dashboard from ...
jest.mock('path to Dashboard component');


Dashboard.mockImplementationOnce(() => <div>Dashboard</div>);

    render(
      <MemoryRouter initialEntries={['/']}>
        <App/>
      </MemoryRouter>
    );

expect(screen.getByText("Dashboard").toBeInTheDocument()

您可以阅读此文档以获取更多示例 https://javascript.plainenglish.io/testing-react-router-with-jest-bc13d367bad

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-09
    • 2012-03-05
    • 2019-11-27
    • 1970-01-01
    • 2011-08-05
    • 2011-02-16
    • 2013-11-11
    • 1970-01-01
    相关资源
    最近更新 更多