如何在Jest中监视window.scrollTo? [英] How to spy on window.scrollTo in Jest?

查看:476
本文介绍了如何在Jest中监视window.scrollTo?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的具有以下功能的react组件:

I got a very simple react component with following functionallity:

componentDidMount() {
  window.scrollTo(0, 0)
}

看来你不能做类似的事情

It seems that you cannot do something like

window.scrollTo = jest.fn()

监视scrollTo功能.

因此,我想知道什么是监视此功能并断言它已在我的测试中使用的最佳方法.

Therefor, I want to know what is the best way to spy on this function and assert that it has been used in my tests.

谢谢

推荐答案

至少必须由global而不是window访问全局名称空间.

In jest the global name space must be accessed by global not window.

global.scrollTo = jest.fn()

这篇关于如何在Jest中监视window.scrollTo?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆