import {fakeAsync, flush, TestBed} from '@angular/core/testing'; import { CarePlanService } from './care-plan.service'; import {HttpClientTestingModule} from '@angular/common/http/testing'; import {PatientContextService} from '../services/patient-context.service'; import {Subject} from 'rxjs'; import {of} from 'rxjs/internal/observable/of'; import {Activity, CarePlan} from '@nspop/gm-web-facade-api'; describe('CarePlanService', () => { let service: CarePlanService; let episodeOfCareStub: Subject; let patientContextServiceStub: PatientContextService; beforeEach(() => { episodeOfCareStub = new Subject(); patientContextServiceStub = { getCurrentEpisodeOfCare: () => '1', getCurrentXBreakTheGlassReason: () => undefined, episodeOfCare$: episodeOfCareStub, setHasPrivatelyMarkedData: () => {}, getCurrentHasPrivatelyMarkedData: () => false, } as any; TestBed.configureTestingModule({ imports: [HttpClientTestingModule], providers: [ {provide: PatientContextService, useValue: patientContextServiceStub}, ], }); service = TestBed.inject(CarePlanService); service.getAll = () => of({ id: '123', version: 777, // in CarePlan version is a number rather than a string activities: [ {id: '456'} as any, {id: '789'} as any, ]} as any); }); it('should be created', () => { expect(service).toBeTruthy(); }); it('updates carePlan$ when asked to refresh', fakeAsync(() => { spyOn(service, 'refresh').and.callThrough(); service.refresh(); flush(); let called = false; service.carePlan$.subscribe((carePlan: CarePlan | undefined) => { expect(carePlan).toBeDefined(); expect(carePlan?.id).toBe('123'); expect(carePlan?.version).toBe(777); called = true; }); expect(service.refresh).toHaveBeenCalledTimes(1); expect(called).toBeTrue(); })); it('updates activities$ when asked to refresh', fakeAsync(() => { spyOn(service, 'refresh').and.callThrough(); service.refresh(); flush(); let called = false; service.activities$.subscribe((activities: Activity[]) => { expect(activities).toBeDefined(); expect(activities?.[0].id).toBe('456'); called = true; }); expect(service.refresh).toHaveBeenCalledTimes(1); expect(called).toBeTrue(); })); });