我正在开发一个React应用,使用Jest和Testing Library进行测试。我想测试一下用户单击浏览器的后退按钮时会发生什么。那有可能吗?我知道我可以使用赛普拉斯,但我希望避免将其添加到我的项目中。
答案 0 :(得分:0)
如果您使用的是React Router,首先需要访问测试中的历史记录对象,然后才能调用history.goBack()。
有一些获取历史对象的选项:
<Route
path="*"
render={props => {
history = props.history;
location = props.location;
return null;
}}
/>
import { createMemoryHistory } from 'history'
...
const history = createMemoryHistory()
...
<Router history={history}>