/* tslint:disable:max-line-length */ import {ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing'; import {AppComponent} from './app.component'; import {DataSourceService} from './services/data-source.service'; import {MatDialog} from '@angular/material/dialog'; import {of, Subject} from 'rxjs'; import {HttpClientTestingModule} from '@angular/common/http/testing'; import {UserContextService} from './services/user-context.service'; import {NamePipe} from './patient-information/name.pipe'; import {CprPipe} from './pipes/cpr.pipe'; import {AppModule} from './app.module'; import {PatientContextService} from './services/patient-context.service'; import {MatIconTestingModule} from '@angular/material/icon/testing'; import {DataCardResponse} from 'facade-api'; import {WindowService} from './services/window.service'; import {DialogService} from './modals/dialog/dialog.service'; import {RoleSelectorComponent} from './role-selector/role-selector.component'; import {CprSearchComponent} from './cpr-search/cpr-search.component'; describe('AppComponent', () => { let component: AppComponent; let fixture: ComponentFixture; let dataCardStub: DataCardResponse; let dataSourceServiceStub: DataSourceService; let userContextServiceStub: UserContextService; let windowServiceStub: WindowService; let patientContext: PatientContextService; let dialogServiceStub: DialogService; let styleStub: CSSStyleDeclaration; let sidebarStub: any; let cprSearchComponentStub: CprSearchComponent; beforeEach(async () => { dataCardStub = { identifier: '1', episodeOfCareIdentifier: '2', documentStatus: 'ACTIVE', recordTarget: { identifier: '1008821234', name: { firstNames: ['Peter'], middleNames: [], lastName: 'Parker' } }, personalDataSection: { cpr: '1008821234', name: { firstNames: ['Peter'], middleNames: [], lastName: 'Parker' } }, socialHistorySection: { partner: { translationLanguage: 'something', nationality: 'whetever', needsTranslation: 'yes' } }, expectedDateOfDeliverySection: { expectedDateOfDeliveryFromLastPeriod: {} } } as any; dataSourceServiceStub = { allDataCards$: of([dataCardStub]), isLoading$: of(false), refresh: () => {}, } as any; userContextServiceStub = { selectedRole$: of({authorizationCode: 'LK34Q', educationCode: '9495', educationType: 'Bandagist'}), userContext$: of({commonName: 'Lars Larsen', organizationName: 'Company A/S', eligibleRoles: [ {authorizationCode: 'J0184', educationCode: '7170', educationType: 'Læge'}, {authorizationCode: 'LK34Q', educationCode: '9495', educationType: 'Bandagist'} ]}), eligibleRoles$: of({authorizationCode: 'LK34Q', educationCode: '9495', educationType: 'Bandagist'}), contextIsInitialized$: of(true), selectedOrganizationSor$: of('sorId') } as any; windowServiceStub = { nativeWindow: () => {}, getXYScroll: () => {}, } as any; patientContext = new PatientContextService({nativeWindow: () => ({ document: {cookie: '2394'}, localStorage: { getItem: () => '1234567890', setItem: () => undefined, }, })} as any); dialogServiceStub = { show: () => {}, } as any; styleStub = { top: '69px', left: '69px', position: 'auto', } as any; sidebarStub = { nativeElement: { style: styleStub, } as any, } as any; cprSearchComponentStub = { focus: () => {}, } as any; await TestBed.configureTestingModule({ declarations: [ AppComponent, NamePipe, CprPipe ], imports: [ HttpClientTestingModule, MatIconTestingModule, AppModule, ], providers: [ {provide: DataSourceService, useValue: dataSourceServiceStub}, {provide: MatDialog, useValue: {}}, {provide: UserContextService, useValue: userContextServiceStub}, {provide: WindowService, useValue: windowServiceStub}, {provide: PatientContextService, useValue: patientContext}, {provide: DialogService, useValue: dialogServiceStub}, ] }) .compileComponents(); }); beforeEach(() => { fixture = TestBed.createComponent(AppComponent); component = fixture.componentInstance; fixture.detectChanges(); }); it('should create the app', () => { expect(component).toBeTruthy(); }); it(`shows correct title`, () => { expect(component.title).toEqual('Graviditetsportalen'); }); it('should render title', fakeAsync(() => { const compiled = fixture.nativeElement; tick(200); expect(compiled.querySelector('.patientContext > .content > .patientName').textContent).toContain('Peter Parker'); })); it('should render column tolkebistand', fakeAsync(() => { const compiled = fixture.nativeElement; tick(); expect(compiled.querySelector('.patientContext > .content > .patientInterpretingAssistance').textContent).toBeTruthy(); })); it('sets active episode of care if first datacard is active', () => { const spy = spyOn(patientContext, 'setEpisodeOfCare'); component.ngOnInit(); expect(spy).toHaveBeenCalledWith('2'); }); it('does not set active episode of care if first datacard is NEW', () => { dataCardStub.documentStatus = 'NEW'; const spy = spyOn(patientContext, 'setEpisodeOfCare'); component.ngOnInit(); expect(spy).not.toHaveBeenCalled(); }); it('does not set active episode of care if first datacard is CLOSED', () => { dataCardStub.documentStatus = 'CLOSED'; const spy = spyOn(patientContext, 'setEpisodeOfCare'); component.ngOnInit(); expect(spy).not.toHaveBeenCalled(); }); describe('when patientContext.hasPrivatelyMarkedData is false', () => { beforeEach(() => { patientContext.setHasPrivatelyMarkedData(false); patientContext.setXBreakTheGlassReason(undefined); fixture.detectChanges(); }); it('should render column tolkebistand', fakeAsync(() => { const compiled = fixture.nativeElement; tick(); expect(compiled.querySelector('.patientContext > .content > .patientInterpretingAssistance')).toBeTruthy(); })); }); describe('when patientContext.hasPrivatelyMarkedData is undefined', () => { beforeEach(() => { patientContext.setHasPrivatelyMarkedData(undefined); patientContext.setXBreakTheGlassReason(undefined); fixture.detectChanges(); }); it('should render column tolkebistand', fakeAsync(() => { const compiled = fixture.nativeElement; tick(); expect(compiled.querySelector('.patientContext > .content > .patientInterpretingAssistance')).toBeTruthy(); })); }); describe('when patientContext.hasPrivatelyMarkedData is true', () => { beforeEach(() => { patientContext.setHasPrivatelyMarkedData(true); patientContext.setXBreakTheGlassReason(undefined); fixture.detectChanges(); }); it('should not render column tolkebistand', fakeAsync(() => { const compiled = fixture.nativeElement; tick(); expect(compiled.querySelector('.patientContext > .content > .patientInterpretingAssistance')).toBeFalsy(); })); describe('and patientContext.xBreakTheGlassReason is truthy', () => { beforeEach(() => { patientContext.setXBreakTheGlassReason('stangspring'); fixture.detectChanges(); }); it('should render column tolkebistand', fakeAsync(() => { const compiled = fixture.nativeElement; tick(); expect(compiled.querySelector('.patientContext > .content > .patientInterpretingAssistance')).toBeTruthy(); })); }); }); describe('when windowService.getXYScroll returns y < 72', () => { beforeEach(async () => { component.sidebar = sidebarStub; spyOn(windowServiceStub, 'getXYScroll').and.returnValue({x: 0, y: 71}); }); it('sets style.top = 72-y px', () => { component.allowYScrollFrame(); expect(styleStub.top).toBe('1px'); }); }); describe('when windowService.getXYScroll returns y >= 72', () => { beforeEach(async () => { component.sidebar = sidebarStub; spyOn(windowServiceStub, 'getXYScroll').and.returnValue({x: 0, y: 72}); }); it('sets style.top = 0px', () => { component.allowYScrollFrame(); expect(styleStub.top).toBe('0px'); }); }); it('calls dialogService.show when openChangeRoleDialog is called', () => { spyOn(dialogServiceStub, 'show'); component.openChangeRoleDialog(); expect(dialogServiceStub.show).toHaveBeenCalledTimes(1); expect(dialogServiceStub.show).toHaveBeenCalledWith(RoleSelectorComponent, {width: 'fit-content'}); }); it('sets feedbackModalOpen = true when openFeedbackDialog is called', () => { component.feedbackModalOpen = false; component.openFeedbackDialog(); expect(component.feedbackModalOpen).toBe(true); }); describe('when patientContext.cpr$ is set to a non-empty cpr-string', () => { beforeEach(() => { spyOn(dataSourceServiceStub, 'refresh'); component.hasPatientInContext = false; }); it('sets local variable hasPatientInContext = true', () => { patientContext.setCpr('0101010101'); fixture.detectChanges(); expect(component.hasPatientInContext).toBe(true); }); describe('and dataCard is truthy', () => { beforeEach(() => { component.dataCard = {} as any; patientContext.setCpr('0101010101'); fixture.detectChanges(); }); it('calls dataSourceService.refresh', () => { expect(dataSourceServiceStub.refresh).toHaveBeenCalledTimes(1); }); }); describe('and dataCard is undefined', () => { beforeEach(() => { component.dataCard = undefined; patientContext.setCpr('0101010101'); fixture.detectChanges(); }); it('does not call dataSourceService.refresh', () => { expect(dataSourceServiceStub.refresh).toHaveBeenCalledTimes(0); }); }); }); describe('when patientContext.cpr$ is set to an empty string', () => { beforeEach(() => { spyOn(dataSourceServiceStub, 'refresh'); component.hasPatientInContext = true; patientContext.setCpr(''); fixture.detectChanges(); }); it('sets local variable hasPatientInContext = false', () => { expect(component.hasPatientInContext).toBe(false); }); it('does not call dataSourceService.refresh', () => { expect(dataSourceServiceStub.refresh).toHaveBeenCalledTimes(0); }); }); describe('when patientContext.patientExists$ is set to true', () => { beforeEach(() => { component.cprSearchComponent = cprSearchComponentStub; spyOn(cprSearchComponentStub, 'focus'); component.patientExists = false; patientContext.setPatientExists(true); fixture.detectChanges(); }); it('calls cprSearchComponent.focus', () => { expect(cprSearchComponentStub.focus).toHaveBeenCalledTimes(1); }); }); describe('when patientContext.patientExists$ is set to false', () => { beforeEach(() => { component.cprSearchComponent = cprSearchComponentStub; spyOn(cprSearchComponentStub, 'focus'); component.patientExists = true; patientContext.setPatientExists(false); fixture.detectChanges(); }); it('does not call cprSearchComponent.focus', () => { expect(cprSearchComponentStub.focus).toHaveBeenCalledTimes(0); }); }); });