![](/img/trans.png)
[英]How can I write a unit test using jest, react, testing-library for this component?
[英]how can i test browser back-button behaviour using jest and (react-) testing-library?
我正在開發一個 React 應用程序,使用 Jest 和測試庫進行我的測試。 我想測試當用戶單擊瀏覽器的后退按鈕時會發生什么。 這可能嗎,如果可以,怎么做? 我知道我可以使用 Cypress,但我寧願避免將其添加到我的項目中。
如果您使用的是 React Router,首先您需要訪問測試中的 history 對象,然后您可以調用history.goBack() 。
有幾個選項可以獲取歷史對象:
<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.