mercredi 16 mai 2018

Cannot read property of undefined in angular

I am testing an angular app and especially this HTML input:

 <input type="text" name="nombre" id="field_nombre"
                        [(ngModel)]="paciente.nombre" required/>

I have this in my test:

describe('Paciente Management Dialog Component', () => {
        let comp: PacienteDialogComponent;
        let fixture: ComponentFixture<PacienteDialogComponent>;
        let debugElement: DebugElement; //create a debgElement for testing

        beforeEach(async(() => {
            TestBed.configureTestingModule({
                imports: [OncosupTestModule,
                    OncosupSharedModule,
                    BrowserModule,
                    FormsModule,
                  
                ],
                declarations: [PacienteDialogComponent,
                ],
                providers: [
                    ....
                     JhiAlertService, ],
                     schemas: [CUSTOM_ELEMENTS_SCHEMA]
            })
            .compileComponents();
        }));

  beforeEach(() => {
            fixture = TestBed.createComponent(PacienteDialogComponent);
            comp = fixture.componentInstance;
            debugElement = fixture.debugElement;
        });
         fit ('first test input', async(() => {
               // fixture.detectChanges(); 
                const input =   debugElement.query(By.css('#field_nombre'));
                const inputElement = input.nativeElement;
                inputElement.value = 'test_value';
                inputElement.dispatchEvent(new Event('input'));
                 console.log('Print input value' + inputElement);
                //expect(comp.paciente.nombre).toBe('test_value');
               
               }));
        
        
        

There is something tricky. If I let fixture.detectChanges(); console prints Cannot read property 'nombre' of undefined, meanwhile if I remove it it prints object HTMLInputElement which is correct. Instead my test expect.toBe outputs the same error in both cases with or wothout detectChanges(). Can someone explain how to properly test this input, or if I do something wrong here?

Aucun commentaire:

Enregistrer un commentaire