import {ComponentFixture, TestBed} from '@angular/core/testing'; import {MedicalHistoryComponent} from './medical-history.component'; import {DataCard} from '@nspop/gm-web-facade-api'; import {JsonTransformerUtil} from '../../../utils/json-transformer.util'; import {AppModule} from '../../app.module'; describe('MedicalHistoryComponent', () => { let component: MedicalHistoryComponent; let fixture: ComponentFixture; beforeEach(async () => { await TestBed.configureTestingModule({ declarations: [ MedicalHistoryComponent ], imports: [AppModule] }) .compileComponents(); }); beforeEach(() => { fixture = TestBed.createComponent(MedicalHistoryComponent); component = fixture.componentInstance; fixture.detectChanges(); }); it('should create', () => { expect(component).toBeTruthy(); }); it('only registers changes if model has structural changes', () => { const dataCard1 = { previousAbortionsSection: { abortions: [] }, previousBirthsSection: { births: [] } } as any as DataCard; const dataCard2 = JsonTransformerUtil.transformDates(JSON.parse(JSON.stringify(dataCard1))) as any as DataCard; // tslint:disable-next-line:no-non-null-assertion It's not null because it's a copy of dataCard1 which has the property set... dataCard2.previousAbortionsSection!.abortions!.push({ year: undefined, type: 'induced', gestationLength: { value: 6, unit: 'w' }, indication: 'whatever' }); const onChange = jasmine.createSpy('onChange'); component.writeValue(dataCard1 ); component.registerOnChange(onChange); component.writeValue(JsonTransformerUtil.transformDates(JSON.parse(JSON.stringify(dataCard1))) as any as DataCard); expect(onChange).toHaveBeenCalledTimes(0); component.writeValue(dataCard2 as any as DataCard); expect(onChange).toHaveBeenCalledTimes(1); }); it('computes BMI correctly', () => { // correct values obtained by using 3rd party BMI calculator: https://netdoktor.dk/interactive/interactivetests/bmi.php component.writeValue({ bodyMeasuresSection: { bodyHeight: {value: 180, unit: 'cm'}, prenatalBodyWeight: {value: 90, unit: 'kg'}, } } as any); expect(component.bmi).toEqual(27.78); component.writeValue({ bodyMeasuresSection: { bodyHeight: {value: 160, unit: 'cm'}, prenatalBodyWeight: {value: 60, unit: 'kg'}, } } as any); expect(component.bmi).toEqual(23.44); component.writeValue({ bodyMeasuresSection: { bodyHeight: {value: 150, unit: 'cm'}, prenatalBodyWeight: {value: 40, unit: 'kg'}, } } as any); expect(component.bmi).toEqual(17.78); }); it('updates bmi on data-card', () => { component.writeValue({ bodyMeasuresSection: { bodyHeight: {value: 160, unit: 'cm'}, prenatalBodyWeight: {value: 60, unit: 'kg'}, } } as any); const bmi = component.bmi; expect(component.value?.bmi).toEqual(bmi as number); }); it('handles that BMI can be un-computable', () => { component.writeValue({ bodyMeasuresSection: { bodyHeight: {value: 0, unit: 'cm'}, prenatalBodyWeight: {value: 90, unit: 'kg'}, } } as any); expect(component.bmi).toEqual('?'); component.writeValue({ bodyMeasuresSection: { bodyHeight: {value: 160, unit: 'cm'}, prenatalBodyWeight: {value: 0, unit: 'kg'}, } } as any); expect(component.bmi).toEqual('?'); component.writeValue({ bodyMeasuresSection: { bodyHeight: {value: 0, unit: 'cm'}, prenatalBodyWeight: {value: 0, unit: 'kg'}, } } as any); expect(component.bmi).toEqual('?'); component.writeValue({} as any); expect(component.bmi).toEqual('?'); }); it('does not wite a non-number in the actual model', () => { component.writeValue({} as any); expect(component.bmi).toEqual('?'); expect(component.value?.bmi).toBeFalsy(); }); });