React-Testing-Library - 使用 Redux 和 Router 包装组件

2024-02-16

我正在尝试设置一个测试文件来在我的应用程序上呈现路线/页面。我试图用 Redux 和 Router 包装所有内容,这就是我所拥有的:

import React from 'react';
import { render } from 'react-testing-library';
import { createStore } from 'redux';
import { Provider } from 'react-redux';
import reducer from '../../store/reducer';
import {Link, Route, Router, Switch} from 'react-router-dom'
import {createMemoryHistory} from 'history'

import ViewNode from '../Pages/ViewNode';


const customRender = (
  ui,
  {
    route = '/',
    history = createMemoryHistory({ initialEntries: [route] }),
    initialState,
    store = createStore(reducer, initialState),
    ...options
  } = {}
) => ({
  ...render(
    <Provider store={store}>
      <Router history={history}>{ui}</Router>
    </Provider>,
    options
  ),
  history,
});

test('can render with redux and router', () => {
  const { getByTestId } = customRender(
    <Route path="/server/:env/:nodeName">
      <ViewNode />
    </Route>,
    {
      route: '/server/prod/some.server.name.com',
    }
  );

  expect(getByTestId('page-content')).toBeVisible()
})

然后我收到以下错误:

Error: Uncaught [TypeError: Cannot read property 'params' of undefined]

抛出错误的原因是它找不到 React Router 参数。当我初始化状态时,组件构造函数失败:

this.state = {
            modal: false,
            activeTab: '1',
            imageStatus: "loading",
            env: props.match.params.env, //failing here
            nodeName: props.match.params.nodeName,
            environments: props.environments,
           }

我上面的实现似乎没有正确包装路由器。

我如何使用 Redux 和 Router 正确包装我的页面组件,以便它可以获得这些路由器参数?


您已将您的<ViewNode />里面的组件Route但忘记传递它收到的道具。因此props.match在您的组件中未定义。

你可以这样做:

    <Route path="/server/:env/:nodeName">
      {props => <ViewNode {...props} />}
    </Route>

基本上,你可以使用使用 a 渲染某些内容的 3 种方法之一<Route> https://reacttraining.com/react-router/web/api/Route.


这是一个工作示例:

import React from 'react'
import {Route, Router} from 'react-router-dom'
import {createMemoryHistory} from 'history'
import {render, fireEvent} from '@testing-library/react'
import {createStore} from 'redux'
import {Provider, connect} from 'react-redux'

function reducer(state = {count: 0}, action) {
  switch (action.type) {
    case 'INCREMENT':
      return {
        count: state.count + 1,
      }
    case 'DECREMENT':
      return {
        count: state.count - 1,
      }
    default:
      return state
  }
}

class Counter extends React.Component {
  increment = () => {
    this.props.dispatch({type: 'INCREMENT'})
  }

  decrement = () => {
    this.props.dispatch({type: 'DECREMENT'})
  }

  render() {
    return (
      <div>
        <div data-testid="env-display">{this.props.match.params.env}</div>
        <div data-testid="location-display">{this.props.location.pathname}</div>
        <div>
          <button onClick={this.decrement}>-</button>
          <span data-testid="count-value">{this.props.count}</span>
          <button onClick={this.increment}>+</button>
        </div>
      </div>
    )
  }
}

const ConnectedCounter = connect(state => ({count: state.count}))(Counter)

function customRender(
  ui,
  {
    initialState,
    store = createStore(reducer, initialState),
    route = '/',
    history = createMemoryHistory({initialEntries: [route]}),
  } = {},
) {
  return {
    ...render(
      <Provider store={store}>
        <Router history={history}>{ui}</Router>
      </Provider>,
    ),
    store,
    history,
  }
}

test('can render with redux and router', () => {
  const {getByTestId, getByText} = customRender(
    <Route path="/server/:env/:nodeName">
      {props => <ConnectedCounter {...props} />}
    </Route>,
    {
      route: '/server/prod/some.server.name.com',
    },
  )

  expect(getByTestId('env-display')).toHaveTextContent('prod')

  expect(getByTestId('location-display')).toHaveTextContent(
    '/server/prod/some.server.name.com',
  )

  fireEvent.click(getByText('+'))
  expect(getByTestId('count-value')).toHaveTextContent('1')
})

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

React-Testing-Library - 使用 Redux 和 Router 包装组件 的相关文章

随机推荐

  • 结构体中的指针和值有什么区别?

    给定以下结构 type Exp struct foo int bar int 在结构中使用指针或值时 性能有何区别 是否有任何开销或者这只是 Go 编程的两个流派 我会使用指针来实现链式结构 但这是否是我们必须在结构中使用指针以获得性能的唯
  • jQuery - SlideToggle() 和切换文本

    我有一个可以使用隐藏的联系表 slideToggle 但我希望该选项卡用于Toggle根据表单是否更改文本的表单in view or hidden 这是 jQuery slider click function form wrap anim
  • 如何将 SQLSRV 扩展安装到 php XAMPP

    I m trying to install a SQLSRV extension My PHP version is 7 2 XAMPP 3 2 2 and I use windows 10 I downloaded the dll fro
  • myisam_sort_buffer_size 与 sort_buffer_size

    我的服务器是 MySQL 内存为 6GB 我需要知道 myisam sort buffer size 和 sort buffer size 之间有什么区别 我为它们设置了以下尺寸 myisam sort buffer size 8M 排序缓
  • 错误:找不到Python模块tensorflow.python.keras

    我想将一些数值转换为类别 我正在使用 keras 包进行图像分类 当我使用 to categorical trainy 时 收到 错误 未找到 Python 模块tensorflow python keras 我已经采取了 trainy l
  • Firebase - 对多个节点进行更新/扇出时设置优先级

    我正在一次更新中写入 Firebase 数据库的多个部分 节点 这样做时是否可以设置节点的优先级 例子 firebaseRef update some node value some other node other value 如果我想同
  • 使用 Gluon 移动环境时如何在运行时请求权限?

    我是新来的 因此我还不知道该网站的所有规则 对此感到抱歉 我正在使用 Eclipse 工具中的 Gluon 移动插件构建桌面和 Android 的 Java 应用程序 我有针对桌面和 Android 的不同代码 如示例中所述 http do
  • PyCharm 使用 Mypy 吗?

    PyCharm 使用 Mypy 还是 JetBrains 实现PEP 484 https www python org dev peps pep 0484 分别地 看来他们采用了自己的实现方式 https github com python
  • Django - 重写 Model.create() 方法?

    The Django 文档 http docs djangoproject com en dev topics db models overriding predefined model methods仅列出覆盖的示例save and de
  • 在 RHEL 6.5 上安装 RPostgreSQL libpq-fe.h 错误

    我有 RHEL 6 5 服务器 安装了 R 3 1 1 和 RStudioServer 0 98 1062 我安装了 postgresql 9 3 并处理一个大型数据库 为了将 R 连接到 PostgreSQL 我过去使用过 RPostgr
  • 通过 python/JS 发送 Whatsapp 消息

    我编写了一个程序 它从 excel 获取信息并通过 python 发送消息 我使用 selenium 和 span 来查找我需要的元素 现在 WhatsApp 更改了 HTML 不再有跨度 旧代码在这里 import time import
  • HTML 中
    标签的使用:版权还是网站管理员?

    在我客户网站的页脚上有他的地址和版权声明 下面是网站管理员的地址 对于两者 我将使用 hCard 微格式 我应该使用其中的哪一个
  • 将 netCDF 文件转换为 csv

    我正在努力将几个 Berkeley Earth netCDF 文件转换为 CSV 或其他表格格式 我意识到以前曾提出过类似的问题 但我无法应用我遇到的任何解决方案 例如 这个数据集 http berkeleyearth lbl gov au
  • 将图像绕一个圆圈

    在这个例子中我想做的是将图像包裹在一个圆圈上 如下所示 为了包装图像 我简单地使用 trig 计算了 x y 坐标 问题是计算出的 X 和 Y 位置被四舍五入以使其成为整数 这会导致上面的包裹图像中出现空白像素 x y 位置必须是整数 因为
  • 如何在 JSF 托管 bean 中创建、访问和销毁会话?

    目前 我正在为在线购物车创建一个 Web 应用程序 我需要在每个 jsf 页面上维护会话 我的问题是 如何在托管 bean 中创建和销毁会话 如何访问存储在会话变量中的值 像这样 FacesContext getCurrentInstanc
  • 我的react-native expo 应用程序的已编译 typescript js 文件在哪里?

    我刚刚创建了一个默认的打字稿博览会项目expo init 并且它有一个简单的App tsx主目录中的文件 我可以对文件进行更改并将这些更改反映在我的应用程序中 但我似乎找不到相应的输出文件 App js文件随处可见 我觉得这很令人困惑 深入
  • 选择器 div + p(加号)和 div ~ p(波形符)之间的区别

    那样的方式w3学校 http www w3schools com 短语它 它们听起来是一样的 W3Schools 的 CSS 参考 http www w3schools com cssref css selectors asp div p选
  • 如何在 Nginx 服务器上允许 PUT 文件请求?

    我正在使用一个需要的应用程序PUTHTTP 服务器上的文件 我使用 Nginx 作为服务器 但得到了一个405 Not Allowed错误返回 以下是使用 cURL 进行测试的示例 curl X PUT H Content Type app
  • IIS 连接池询问/泄漏跟踪

    根据这个有用的文章 http www 15seconds com issue 040830 htm我已确认运行 W2k3 的 IIS 6 服务器上的某些应用程序存在连接池泄漏 困难的部分是 我正在从该服务器的 6 个应用程序池中为 700
  • React-Testing-Library - 使用 Redux 和 Router 包装组件

    我正在尝试设置一个测试文件来在我的应用程序上呈现路线 页面 我试图用 Redux 和 Router 包装所有内容 这就是我所拥有的 import React from react import render from react testi