渲染重定向时,组件在react-test-renderer中更新

时间:2018-03-06 11:05:12

标签: reactjs react-redux jest react-router-v4 react-test-renderer

我目前正在使用react-router-dom为react-redux编写的登录表单编写测试。

我的组件如下所示:

class LoginForm extends Component {

  render() {
    const {
      isAuthenticated,
      ...
    } = this.props

    if (isAuthenticated) {
      return (
        <Redirect to="/" />
      )
    }

    return (
      <div style={{ ...styles.main }}>
        ...
      </div>
    )
  }
}

为了测试这个组件,我编写了以下测试:

import React from "react"
import renderer from "react-test-renderer"
import { MemoryRouter } from "react-router-dom"

import LoginPage from "./LoginPage"

it("renders a redirect if the user is authenticated", () => {

  const tree = renderer
    .create(
      <MemoryRouter>
        <LoginPage
          isAuthenticated={ true } />
      </MemoryRouter>
    )
    .toJSON()

  expect(tree).toMatchSnapshot()
})

此测试正确安装组件并且&#34;渲染&#34;重定向,但它似乎也触发更新。这会重新发送重定向并在我的测试输出中给出以下警告:

console.error node_modules\warning\warning.js:51
  Warning: You tried to redirect to the same route you're currently on: "/"

如何防止出现此警告?

谢谢!

0 个答案:

没有答案