Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

288
Visualizações
Cannot read properties of undefined (reading 'nativeElement') by ViewChildren unit tests

My unit test doesn't pass because of this error: "Failed: Cannot read properties of undefined (reading 'nativeElement')". I think that the problem is incorrect mocking of ViewChildren, but I don't know how do it right. Can someone please help to fix it?

ts

@ViewChildren('locationInput') inputs: QueryList<ElementRef>;
addCity(): void {
this.items = this.locationForm.get('items') as FormArray;
if (this.regionSelected && this.checkIfAllCitysAreSelected()) {
  this.items.push(this.createItem());
  this.quantityOfLocations++;
  this.cdr.detectChanges();
  const tempInput = this.inputs.toArray()[this.quantityOfLocations - 1].nativeElement;
  this.tempAutocomplete = new google.maps.places.Autocomplete(tempInput, this.localityOptions);
  this.tempAutocomplete.setBounds(this.regionBounds);
  this.tempAutocomplete.setOptions(this.localityOptions);
  this.autocomplete.push(this.tempAutocomplete);
  this.addEventToAutocomplete(this.quantityOfLocations - 1);
}

}

spec.ts

 it('method addCity should add new item and add autocomplete', async(() => {
  let array: FormGroup[] = [];
  array.push(mockedForm);
  let formArray = new FormArray(array);
  component.locationForm = new FormGroup({
   items: formArray
  });

  const spy = spyOn(component, 'addEventToAutocomplete');
  spyOn(component, 'checkIfAllCitysAreSelected').and.returnValue(true);
  component.regionSelected = true;
  fixture.detectChanges();
  component.inputs[1] = new ElementRef({ nativeElement: { value: 'fake' } });
  component.addCity();

  expect(component.quantityOfLocations).toBe(1);
  expect((component.locationForm.controls['items'] as FormArray).length).toBe(2);
  expect(component.tempAutocomplete).toBeTruthy();
  expect(spy).toHaveBeenCalled();
 }));

Thanks for the help!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The problem occurs with the changes on ViewChildren element. It can be solved easily just by instead of:

@ViewChildren('locationInput') inputs: QueryList<ElementRef>;

to:

@ViewChildren('locationInput', {static: true}) inputs: QueryList<ElementRef>;

It should solve your problem.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda