import {ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing'; import { PrivatelyMarkedComponent } from './privately-marked.component'; import {AngularMaterialModule} from '../modules/angular-material.module'; import {DialogService} from '../modals/dialog/dialog.service'; import {of} from 'rxjs/internal/observable/of'; import {Observable} from 'rxjs'; import {PatientContextService} from '../services/patient-context.service'; import {TemplateRef} from '@angular/core'; import {WindowService} from '../services/window.service'; import {appUrls} from '../../utils/URL_STRING_LITERALS'; describe('PrivatelyMarkedComponent', () => { let component: PrivatelyMarkedComponent; let fixture: ComponentFixture; let dialogStub: TemplateRef; let dialogServiceStub: DialogService; let dialogObservableStub: Observable; let patientContextServiceStub: PatientContextService; let windowLocationStub: typeof location; let windowServiceStub: WindowService; beforeEach(async () => { dialogServiceStub = { show: () => dialogObservableStub } as any; patientContextServiceStub = { setXBreakTheGlassReason: () => {} } as any; dialogStub = { } as any; windowLocationStub = { assign: () => {}, } as any; windowServiceStub = { nativeWindow: () => ({ location: windowLocationStub, } as Window), } as any; await TestBed.configureTestingModule({ declarations: [ PrivatelyMarkedComponent ], imports: [AngularMaterialModule], providers: [ {provide: DialogService, useValue: dialogServiceStub}, {provide: PatientContextService, useValue: patientContextServiceStub}, {provide: WindowService, useValue: windowServiceStub}, ], }).compileComponents(); }); beforeEach(() => { fixture = TestBed.createComponent(PrivatelyMarkedComponent); component = fixture.componentInstance; fixture.detectChanges(); }); it('should create', () => { expect(component).toBeTruthy(); }); describe('when the open dialog button is clicked', () => { it('should call openDialog', fakeAsync(() => { spyOn(component, 'openDialog'); const button = fixture.debugElement.nativeElement.querySelector('button'); button.click(); tick(); expect(component.openDialog).toHaveBeenCalled(); })); }); describe('when dialog closes with "yes" option', () => { beforeEach(() => { dialogObservableStub = of(true); }); it('should set patientContext.XBreakTheGlassReason', () => { spyOn(patientContextServiceStub, 'setXBreakTheGlassReason'); component.openDialog(); expect(patientContextServiceStub.setXBreakTheGlassReason).toHaveBeenCalledTimes(1); }); it('should reload the page', () => { spyOn(windowLocationStub, 'assign'); component.openDialog(); expect(windowLocationStub.assign).toHaveBeenCalledTimes(1); expect(windowLocationStub.assign).toHaveBeenCalledWith(appUrls.base); }); }); describe('when dialog closes with "no" option', () => { beforeEach(() => { dialogObservableStub = of(false); }); it('should not set patientContext.XBreakTheGlassReason', () => { spyOn(patientContextServiceStub, 'setXBreakTheGlassReason'); component.openDialog(); expect(patientContextServiceStub.setXBreakTheGlassReason).toHaveBeenCalledTimes(0); }); }); });