首页 > 解决方案 > 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();
  });
}

enter image description here

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.

标签: angularunit-testingobservablekarma-jasminemat-dialog

解决方案


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%');
});

推荐阅读