[英]how can i test browser back-button behaviour using jest and (react-) testing-library?
I'm working on a React app, using Jest and Testing Library for my tests.我正在开发一个 React 应用程序,使用 Jest 和测试库进行我的测试。 I'd like to test what happens when the user clicks the browser's back button.
我想测试当用户单击浏览器的后退按钮时会发生什么。 Is that possible, and if so, how?
这可能吗,如果可以,怎么做? I know I could use Cypress but I'd prefer to avoid adding that to my project.
我知道我可以使用 Cypress,但我宁愿避免将其添加到我的项目中。
If you're using React Router, first you need access to the history object in your test and then you can call history.goBack() .如果您使用的是 React Router,首先您需要访问测试中的 history 对象,然后您可以调用history.goBack() 。
There are a few options to get the history object:有几个选项可以获取历史对象:
<Route
path="*"
render={props => {
history = props.history;
location = props.location;
return null;
}}
/>
import { createMemoryHistory } from 'history'
...
const history = createMemoryHistory()
...
<Router history={history}>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.