簡體   English   中英

如何使用 jest 和(react-)testing-library 測試瀏覽器后退按鈕的行為?

[英]how can i test browser back-button behaviour using jest and (react-) testing-library?

我正在開發一個 React 應用程序,使用 Jest 和測試庫進行我的測試。 我想測試當用戶單擊瀏覽器的后退按鈕時會發生什么。 這可能嗎,如果可以,怎么做? 我知道我可以使用 Cypress,但我寧願避免將其添加到我的項目中。

如果您使用的是 React Router,首先您需要訪問測試中的 history 對象,然后您可以調用history.goBack()

有幾個選項可以獲取歷史對象:

  1. 從自定義路線
<Route
  path="*"
  render={props => {
     history = props.history;
     location = props.location;
     return null;
  }}
/>
  1. 使用 createMemoryHistory
import { createMemoryHistory } from 'history'
...
const history = createMemoryHistory()
...
<Router history={history}>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM