import { ComponentFixture, TestBed } from '@angular/core/testing'; import { ActivityCardComponent } from './activity-card.component'; import {MatIconModule} from '@angular/material/icon'; import {ActivitiesService} from '../activities.service'; import {DataCardService} from '../../api/data-card.service'; import {of} from 'rxjs'; import {Observation} from '@nspop/gm-web-facade-api'; describe('ActivityCardComponent', () => { let component: ActivityCardComponent; let fixture: ComponentFixture; let activitiesServiceStub: ActivitiesService; beforeEach(async () => { activitiesServiceStub = { emitOpenActivityModalEvent: () => {}, cancelActivity: () => {}, emitOpenNoteModalEvent: () => {}, emitOpenMeasurementsModalEvent: () => {}, } as any; await TestBed.configureTestingModule({ declarations: [ ActivityCardComponent ], imports: [MatIconModule], providers: [ {provide: ActivitiesService, useValue: activitiesServiceStub}, { provide: DataCardService, useValue: {isDataCardClosed: of(false)}} ], }) .compileComponents(); }); beforeEach(() => { fixture = TestBed.createComponent(ActivityCardComponent); component = fixture.componentInstance; fixture.detectChanges(); }); it('should create', () => { expect(component).toBeTruthy(); }); describe('when there is no activity set', () => { beforeEach(() => { component.activity = undefined; }); it('does not call emitOpenActivityModalEvent', () => { const spy = spyOn(activitiesServiceStub, 'emitOpenActivityModalEvent'); component.editActivity(); expect(spy).toHaveBeenCalledTimes(0); }); it('does not call cancelActivity', () => { const spy = spyOn(activitiesServiceStub, 'cancelActivity'); component.cancelActivity(); expect(spy).toHaveBeenCalledTimes(0); }); it('does not call emitOpenNoteModalEvent', () => { const spy = spyOn(activitiesServiceStub, 'emitOpenNoteModalEvent'); component.addOrEditNote(); expect(spy).toHaveBeenCalledTimes(0); }); it('does not call emitOpenMeasurementsModal', () => { const spy = spyOn(activitiesServiceStub, 'emitOpenMeasurementsModalEvent'); component.addMeasurements(); expect(spy).toHaveBeenCalledTimes(0); }); }); describe('when there is an activity set', () => { beforeEach(() => { component.activity = { id: '12345', status: 'expected', title: 'whatever'}; }); it('calls emitOpenActivityModalEvent', () => { const spy = spyOn(activitiesServiceStub, 'emitOpenActivityModalEvent'); component.editActivity(); expect(spy).toHaveBeenCalledTimes(1); }); it('calls cancelActivity', () => { const spy = spyOn(activitiesServiceStub, 'cancelActivity'); component.cancelActivity(); expect(spy).toHaveBeenCalledTimes(1); }); it('calls emitOpenNoteModalEvent', () => { const spy = spyOn(activitiesServiceStub, 'emitOpenNoteModalEvent'); component.addOrEditNote(); expect(spy).toHaveBeenCalledTimes(1); }); it('calls emitOpenMeasurementsModal', () => { const spy = spyOn(activitiesServiceStub, 'emitOpenMeasurementsModalEvent'); component.addMeasurements(); expect(spy).toHaveBeenCalledTimes(1); }); }); it('does not have a white statusColor when status = planned', () => { expect(component.getActivityStatusColor('planned')).not.toBe('#FFF'); }); it('does not have a white statusColor when status = expected', () => { expect(component.getActivityStatusColor('expected')).not.toBe('#FFF'); }); it('does not have a white statusColor when status = finished', () => { expect(component.getActivityStatusColor('finished')).not.toBe('#FFF'); }); it('does have a white statusColor when status is not well defined', () => { expect(component.getActivityStatusColor('whatever')).toBe('#FFF'); }); it('returns an observation of the right type upon getting measurement', () => { const observation1: Observation = { type: 'blood_pressure', values: [ {value: '0', name: 'systolicInput', unit: '[mm/Hg]'}, {value: '1', name: 'diastolicInput', unit: '[mm/Hg]'}, ], dateOfMeasurement: new Date(), } as any; component.activity = { id: '12345', status: 'expected', title: 'whatever', observations: [ observation1 ], } as any; expect(component.getMeasurement('blood_pressure')).toEqual(observation1); }); it('returns the latest observation of the right type upon getting measurement', () => { const observation1: Observation = { type: 'blood_pressure', values: [ {value: '1', name: 'systolicInput', unit: '[mm/Hg]'}, {value: '1', name: 'diastolicInput', unit: '[mm/Hg]'}, ], dateOfMeasurement: new Date('20100102'), } as any; const observation2: Observation = { type: 'blood_pressure', values: [ {value: '2', name: 'systolicInput', unit: '[mm/Hg]'}, {value: '2', name: 'diastolicInput', unit: '[mm/Hg]'}, ], dateOfMeasurement: new Date('20100101'), } as any; component.activity = { id: '12345', status: 'expected', title: 'whatever', observations: [ observation1, observation2 ], } as any; expect(component.getMeasurement('blood_pressure')?.values[0]?.value).toEqual('1'); }); it('returns undefined upon getting measurement if no activity is set', () => { component.activity = undefined; expect(component.getMeasurement('blood_pressure')).toEqual(undefined); }); it('gets the right format for bloodPressure', () => { const observation1: Observation = { type: 'blood_pressure', values: [ {value: 'systolic', name: 'systolicInput', unit: 'mmHg'}, {value: 'diastolic', name: 'diastolicInput', unit: 'mmHg'}, ], dateOfMeasurement: new Date(), } as any; component.activity = { id: '12345', status: 'expected', title: 'whatever', observations: [ observation1 ], } as any; expect(component.getBloodPressure()).toEqual('systolic / diastolic mmHg'); }); it('displays a placeholder if no observation was found', () => { component.activity = { id: '12345', status: 'expected', title: 'whatever', observations: [], } as any; expect(component.getBloodPressure()).toEqual('?'); }); it('displays a placeholder if no systolic value was found', () => { const observation1: Observation = { type: 'blood_pressure', values: [ ], dateOfMeasurement: new Date(), } as any; component.activity = { id: '12345', status: 'expected', title: 'whatever', observations: [observation1], } as any; expect(component.getBloodPressure()).toEqual('? / ? ?'); }); it('displays a placeholder if no systolic value was found', () => { const observation1: Observation = { type: 'blood_pressure', values: [ {value: 'systolic', name: 'systolicInput', unit: 'mmHg'}, ], dateOfMeasurement: new Date(), } as any; component.activity = { id: '12345', status: 'expected', title: 'whatever', observations: [observation1], } as any; expect(component.getBloodPressure()).toEqual('systolic / ? mmHg'); }); it('displays the right value when getting single measurement', () => { const observation1: Observation = { type: 'weight', values: [ {value: '50', name: '', unit: 'kg'}, ], dateOfMeasurement: new Date(), } as any; component.activity = { id: '12345', status: 'expected', title: 'whatever', observations: [observation1], } as any; expect(component.getSingleMeasurementValue('weight')).toEqual('50 kg'); }); it('displays a placeholder if no observation found when getting single measurement', () => { component.activity = { id: '12345', status: 'expected', title: 'whatever', observations: [], } as any; expect(component.getSingleMeasurementValue('weight')).toEqual('?'); }); it('displays nothing if no value or unit found when getting single measurement', () => { const observation1: Observation = { type: 'weight', values: [ {value: undefined, name: '', unit: undefined}, ], dateOfMeasurement: new Date(), } as any; component.activity = { id: '12345', status: 'expected', title: 'whatever', observations: [observation1], } as any; expect(component.getSingleMeasurementValue('weight')).toEqual(' '); }); });