reactjs - 如何从笑话中调用 IntersectionObserver 回调函数?
问题描述
用于延迟图像加载的新 Intersection Observer 的 Js 代码。
_observer = new IntersectionObserver(function (entries) {
entries.forEach(function (entry) {
if (entry.intersectionRatio > 0) {
let lazyImage = entry.target;
lazyImage.removeAttribute('class');
_observer.unobserve(lazyImage);
}
});
笑话代码:
beforeAll(() => {
const options = {
threshold: 0.1,
};
window.IntersectionObserver = jest.fn(function () {
observe, unobserve;
}, options);
wrapper = mount(<Image url="test.jpg" alt="test" />);
});
afterAll(() => {
window.IntersectionObserver = windowIntersectionObserver;
});
it('creates an observer on the element ref', () => {
expect(observe).toHaveBeenCalled();
});
o/p:
Expected number of calls: >= 1
Received number of calls: 0
这对我不起作用。如何从 Jest 代码中调用交叉点观察者。我检查了类似的问题,它在这里不起作用
解决方案
推荐阅读
- python - 关于离线绘图和更新 jupyter notebook 中的图表的困惑
- javafx - JavaFX:X 轴上的对数刻度
- jpa - 从 Java 10.0 NullPointer 开始搜索持久性 - JPA eclipselink 2.6.5
- python - 如何仅访问列表的特定元素
- c# - Xamarin 登录页面和 C#
- linux - 哪个Linux程序将失败的sshd登录尝试写入/var/log/btmp Centos 7
- php - Laravel 5.6 RESRful Webervice 更新和删除
- asp.net-core-mvc - 在控制器内调用 add-migration
- blockchain - 去中心化存储系统 Storj 是如何工作的?
- swift - 为什么 CLGeoCoder 什么都得不到?