import { ComponentFixture, TestBed } from '@angular/core/testing'; import { ScreeningsComponent } from './screenings.component'; import {DataCard} from 'facade-api'; import {JsonTransformerUtil} from '../../../utils/json-transformer.util'; import {FormsModule, NgForm} from '@angular/forms'; import {AppModule} from '../../app.module'; import {MatIconTestingModule} from '@angular/material/icon/testing'; describe('ScreeningsComponent', () => { let component: ScreeningsComponent; let fixture: ComponentFixture; beforeEach(async () => { await TestBed.configureTestingModule({ declarations: [ ScreeningsComponent ], imports: [FormsModule, AppModule, MatIconTestingModule], providers: [ {provide: NgForm, useValue: new NgForm([], [])}, ] }) .compileComponents(); }); beforeEach(() => { fixture = TestBed.createComponent(ScreeningsComponent); 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({ date: 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); }); });