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

200
Visualizações
Jasmine Unit testing a function that use's document query

I need some help to get my code coverage up. I have this function in my ts file and need to understand how to have this if statement execute.

setWidth(): void {

let maxHeight = 0;

document.querySelectorAll<HTMLElement>(`.body`).forEach(element => {

  if (element.offsetHeight > maxHeight){

    maxHeight = element.offsetHeight;

  }

});

I am not clear on how I would get the forEach to execute in my spec.ts file.

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

0

Try this:

it('should set maxHeight', () => {
  // set a spy and return a mocked array.
  // add as any here below as well.
  spyOn(document, 'querySelectorAll').and.returnValue([{ offsetHeight: 20 }] as any);
  service.setWidth();
  // below is a bad expectation but I am not sure what to expect for you from the
  // code provided. Do an assertion that relates to setWidth.
  expect(1).toBe(1);
});
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