我目前正在使用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: "/"
如何防止出现此警告?
谢谢!