import {ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing'; import {ShowRelatedActivityModalComponent} from './show-related-activity-modal.component'; import {of} from 'rxjs'; import {ObservationType} from '@nspop/gm-web-facade-api'; import createSpy = jasmine.createSpy; import {AppModule} from '../../app.module'; import {DirtyGuard} from '../../confirm-navigation-if-dirty.framework'; import {MatIconTestingModule} from '@angular/material/icon/testing'; import {CarePlanService} from '../../api/care-plan.service'; describe('ShowRelatedActivityModalComponent', () => { let component: ShowRelatedActivityModalComponent; let fixture: ComponentFixture; let carePlanServiceStub: CarePlanService; beforeEach(async () => { carePlanServiceStub = { getActivityById: () => of({}) } as any; await TestBed.configureTestingModule({ declarations: [ ShowRelatedActivityModalComponent ], imports: [AppModule, MatIconTestingModule], providers: [ {provide: CarePlanService, useValue: carePlanServiceStub}, {provide: DirtyGuard, useValue: {canDeactivate: () => true}} ] }) .compileComponents(); }); beforeEach(() => { fixture = TestBed.createComponent(ShowRelatedActivityModalComponent); component = fixture.componentInstance; fixture.detectChanges(); }); it('should create', () => { expect(component).toBeTruthy(); }); it('handles missing observations', () => { component.activity = {observations: []} as any; fixture.detectChanges(); expect(component.getObservation(ObservationType.Flow)).toEqual('-'); }); it('handles observations with string values', () => { component.activity = {observations: [ {type: ObservationType.Flow, values: [{value: '22'}]} ]} as any; fixture.detectChanges(); expect(component.getObservation(ObservationType.Flow)).toEqual('22'); }); it('handles observations with numeric values and units', () => { component.activity = {observations: [ {type: ObservationType.FetusWeightEstimateUltrasound, values: [{value: '1500', unit: 'g'}]} ]} as any; fixture.detectChanges(); expect(component.getObservation(ObservationType.FetusWeightEstimateUltrasound)).toEqual('1500 g'); }); it('finds correct fetus', () => { component.activity = {observations: [ {type: ObservationType.FetusWeightEstimateUltrasound, values: [{value: '1500', unit: 'g'}], fetus: '12'}, {type: ObservationType.FetusWeightEstimateUltrasound, values: [{value: '1000', unit: 'g'}], fetus: '4'}, ]} as any; fixture.detectChanges(); expect(component.getObservation(ObservationType.FetusWeightEstimateUltrasound, '4')).toEqual('1000 g'); }); it('emits modalClosed-event on close', () => { const spy = createSpy('onClose', () => undefined); component.modalClosed.subscribe(() => spy()); component.isOpen = true; component.ngOnChanges({isOpen: {}} as any ); fixture.detectChanges(); expect(spy).not.toHaveBeenCalled(); component.isOpen = false; component.ngOnChanges({isOpen: {}} as any ); fixture.detectChanges(); expect(spy).toHaveBeenCalled(); }); it('does not emit modalClosed-event on open', () => { const spy = createSpy('onClose', () => undefined); component.modalClosed.subscribe(() => spy()); component.isOpen = true; component.ngOnChanges({isOpen: {}} as any ); fixture.detectChanges(); expect(spy).not.toHaveBeenCalled(); }); it('errors if activity id does not match any known activities', fakeAsync(() => { const spy = spyOn(carePlanServiceStub, 'getActivityById'); spy.and.returnValue(null); let err = null; component.activityId = 'something_that_does_not_exist'; fixture.detectChanges(); try { component.ngOnChanges({activityId: {}} as any); tick(); } catch (error) { err = error; } expect(err).toBeDefined('no error was thrown and we expect this case to error'); expect(err?.message).toEqual('Could not find activity with id \'something_that_does_not_exist\' in the care-plan'); })); it('extracts list of fetuses from activity', fakeAsync(() => { const spy = spyOn(carePlanServiceStub, 'getActivityById'); spy.and.returnValue( {id: '4', observations: [{fetus: '1'}, {fetus: '2'}, {fetus: '1'}, {fetus: '1'}, {fetus: '3'}, {fetus: '2'}]} as any ); component.activityId = '4'; fixture.detectChanges(); component.ngOnChanges({activityId: {}} as any); tick(); expect([...component.feti]).toEqual(['1', '2', '3']); })); });