angular - how to write unit test to cover all the condition of breakpoint observer?
问题描述
I try to write unit test breakpoint observer. but the positive condition is not covered
isSmallScreen: Observable<BreakpointState> = this.breakpointObserver.observe('(max-width: 767px)');
openEntityDetailDialog(): void {
this.entityCreationComponent.close();
this.dialogRef = this.dialog.open(HomeBusinessEntityDetail, {
maxWidth: '767px', disableClose: true
});
const dialogSubscription = this.isSmallScreen.subscribe(result => {
if (result.matches) {
this.dialogRef.updateSize('100%', '100%');
} else {
this.dialogRef.updateSize('560px');
}
});
this.dialogRef.afterClosed().subscribe(result => {
dialogSubscription.unsubscribe();
});
}
Help me to write unit test for the line
const dialogSubscription = this.isSmallScreen.subscribe(result => {
if (result.matches) {
this.dialogRef.updateSize('100%', '100%');
}
Thanks in advance.
解决方案
You can try something like this
it('should update the dialog size', () => {
component.isSmallScreen = of({ matches: true });
const spy: Jasmine.Spy = spyOn(component.dialogRef, 'updateSize'); // considering you have mocked the dialogRef in your test
component.openEntityDetailDialog();
expect(spy).toHaveBeenCalledWith('100%', '100%');
});
推荐阅读
- c# - 如何将整个datagridview导出成word文档(在C#中使用office word interop DLL)
- python - pandas 为多类列绘制 CDF
- ubuntu - MPI 和 libevent 混合代码导致 Ubuntu 上的分段错误
- java - 按下按钮时触发要执行的操作
- hazelcast - Hazelcast IMDG 部分网络拆分
- python - 使用 Pandas 从 Google Cloud Storage 读取 parquet 元数据
- c# - 如何从 C# 中的向量 3 列表中删除向量以实现统一
- java - 在 Java 中将数组添加到队列(指针问题?)
- r - 根据间隔时的R情况
- string - 如何在 Scala 中处理巨大的字符串?