html - 用 Jest 测试 Angular 指令
问题描述
我们正在将单元测试从 Jasmine 迁移到 Jest。我正在重构测试,并且在涉及测试指令时必须处理一些意想不到的问题。
实际上,创建指令 HTML 内容似乎一切顺利,但无法使用选择器 sush as 访问元素By.css()
。它总是返回 null !
// shadow-top-on-scroll.directive.ts
@Directive({
selector: '[appShadowTopOnScroll]'
})
export class ShadowTopOnScrollDirective implements OnInit {
shadow = document.createElement('div');
constructor(private el: ElementRef) {
this.shadow.id = 'shadow';
// some css
}
@HostListener('scroll')
@throttle(500)
scroll() {
if (this.el.nativeElement.scrollTop === 0) {
this.shadow.classList.remove('shadow-bottom-sm');
} else {
this.shadow.classList.add('shadow-bottom-sm');
}
}
ngOnInit(): void {
this.el.nativeElement.parentNode?.insertBefore(this.shadow, this.el.nativeElement);
}
}
// shadow-top-on-scroll.directive.spec.ts
@Component({
templateUrl: './shadow-top-on-scroll.html',
changeDetection: ChangeDetectionStrategy.OnPush
})
class FakeHostComponent {}
describe('ShadowTopOnScrollDirective', () => {
let component: FakeHostComponent;
let fixture: ComponentFixture<FakeHostComponent>;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [FakeHostComponent, ShadowTopOnScrollDirective]
}).compileComponents();
fixture = TestBed.createComponent(FakeHostComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create component', () => {
expect(component).toBeTruthy(); // this test is OK
});
it('should xxx', () => {
console.log(fixture.debugElement.nativeElement.innerHTML);
/* prints
<div>
<div id="shadow" class="pb-2" style="margin-bottom: 0.125rem;"></div>
<div appshadowtoponscroll="" id="containerToScroll" style="height: 50px; overflow-y: auto">
<div style="height: 100px"></div>
</div>
</div> */
const shadowElement = fixture.debugElement.query(By.css('#shadow'));
expect(shadowElement).toBeTruthy(); // test fails because shadowElement is null
});
});
// shadow-top-on-scroll.html
<div>
<div id="containerToScroll" appShadowTopOnScroll style="height: 50px; overflow-y: auto">
<div style="height: 100px"></div>
</div>
</div>
我不明白为什么我的 css 选择器By.css()
不返回元素,而感觉就像成功生成了 HTML。关于发生了什么的任何想法?
我重构了一些其他组件测试,当我调用By.css('#anyId')
. 测试仅针对项目的 3 个指令测试失败
解决方案
推荐阅读
- asynchronous - 无法获取soapUI groovy脚本来监控测试套件并等到它完成
- javascript - 音频结束,重新洗牌变量并播放另一个音频
- node.js - npm不完整日志的nodejs错误
- r - R 升级后 DataFrame 输出未内联为 DT
- vue.js - 获取路由器链接的“活动”状态以匹配道具
- javascript - 无法读取未定义的属性“地图” - ReactJs
- cmake - CPACK_DEBIAN_
_PACKAGE_CONTROL_EXTRA 被忽略 - google-apps-script - 用于在两个 Google 电子表格之间查找新数据或唯一数据的应用脚本(如何附加)
- sequelize.js - 续集一对多查询
- go - GORM 按外键关联查找