angular - scrollIntoView 函数调用需要角茉莉花测试用例
问题描述
这是我想测试但无法理解如何模拟 scrollIntoView 的方法。是否有任何文档可以帮助我理解茉莉花测试。我收到错误消息说 ele.scrollIntoView 不是函数。
scrollIntoViewMethod(): void {
const offsetTopDiv = '.highlight';
const ele = this.el.nativeElement.querySelector(`${offsetTopDiv}`);
ele.scrollIntoView({
behavior: 'smooth',
block: 'center',
inline: 'nearest',
});
}
解决方案
据我了解el
是一个ViewChild
,那么你可以嘲笑它querySelector
。
it('test', () => {
// assuming that env is set and ready
fixture.detectChanges();
// adding spy
const querySelectorSpy = spyOn(component.el.nativeElement, 'querySelector');
// creating a stub
const eleSpy = {
scrollIntoView: jasmine.crateSpy('eleSpy.scrollIntoView'),
};
// setting the return value
querySelectorSpy.and.returnValue(eleSpy);
// action
component.scrollIntoViewMethod();
// assertion
expect(querySelectorSpy).toHaveBeenCalledWith('.highlight');
expect(eleSpy.scrollIntoView).toHaveBeenCalledWith({
behavior: 'smooth',
block: 'center',
inline: 'nearest',
});
});
推荐阅读
- sql-server - 批处理文件不记录 SQL 错误
- sql-server - 哪个 SQL Profiler Trace 模板显示表类型参数值
- javascript - D3.js - 数组的第一个值丢失
- python - 如何将python版本从google colab的默认3.5更改为3.8
- git - 解决冲突 Git LFS
- spring - JPA。在sql select中强制转换@Entity的字段
- python - 使用 GmailAPI 在帐户设置中添加自动转发时出现问题
- autocomplete - AMP amp-autocomplete - 单击时转到 url
- python - ModuleNotFoundError:没有名为“Projectfolder.MAINFOLDER”的模块;'Projectfolder' 不是一个包
- c# - Visual Studio 2017 Pro - XAML 设计器因创建新项目而崩溃