import {fakeAsync, flush, TestBed} from '@angular/core/testing'; import { DataCardService } from './data-card.service'; import {HttpClientTestingModule} from '@angular/common/http/testing'; import {DataCard} from '@nspop/gm-web-facade-api'; import {of} from 'rxjs/internal/observable/of'; import {PatientContextService} from '../services/patient-context.service'; import {Subject} from 'rxjs'; describe('DataCardService', () => { let service: DataCardService; 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(DataCardService); service.getAll = () => of([ {documentUniqueId: '1234', version: '5', episodeOfCareIdentifier: '1', documentStatus: 'SINGLE'} as any, {documentUniqueId: '789', version: '3', episodeOfCareIdentifier: '2', documentStatus: 'ITSCOMPLICATED'} as any, ]); }); it('should be created', () => { expect(service).toBeTruthy(); }); it('updates allDataCards$ when asked to refresh', fakeAsync(() => { spyOn(service, 'refresh').and.callThrough(); service.refresh(); flush(); let called = false; service.allDataCards$.subscribe((dataCard: DataCard[]) => { expect(dataCard).toBeDefined(); expect(dataCard?.[0].documentUniqueId).toBe('1234'); expect(dataCard?.[0].version).toBe('5'); called = true; }); expect(service.refresh).toHaveBeenCalledTimes(1); expect(called).toBeTrue(); })); it('updates selectedDataCard$ when asked to refresh', fakeAsync(() => { spyOn(service, 'refreshActiveDataCard').and.callThrough(); service.refresh(); flush(); let called = false; service.selectedDataCard$.subscribe((dataCard: DataCard | undefined) => { expect(dataCard).toBeDefined(); expect(dataCard?.documentUniqueId).toBe('1234'); expect(dataCard?.version).toBe('5'); called = true; }); expect(service.refreshActiveDataCard).toHaveBeenCalledTimes(1); expect(called).toBeTrue(); })); });