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

278
Visualizações
How to properly test ngForm reset? (reset with default value)

I have this ngForm with contans among other things a textfield for a message. This textfield should be prefilled with a previously chosen signature (Best regards, ...).

Also, there is a reset button which triggers following method:

reset() {
    this.form.resetForm( {message: this.getDefaultMessage()});
}

This resets every formfield and sets the default for the message field with following method:

getDefaultMessage() {
    if (this.user.signature) {
      return '\n\n' + this.user.signature;
    } else {
      return '';
    }
}

While testing the running application this works like a charm, but I'm struggling to test this successfully.

it('should reset form', () => {
    const signature = 'Best regards';                                  
    component.user.signature = signature;                               // setting user signature
    component.reset();                                                  // trigger reset
    expect(component.user.signature).toEqual(signature);               // user signature successfully set
    expect(component.getDefaultMessage()).toContain(signatur);          // default message contains specified signature
    expect(component.message).toEqual(component.getDefaultMessage());   // (FAILING) message field was set to the result of the getDefaultMessage-method 
});

The last expect block generates this error:

1) should reset form
     MaUploadComponent
     Expected '' to equal '

Best Regards'.

My best guess is, that the resetForm-method of ngForm is run at a specific point in the component-refresh-lifecycle, specically the injeciton of given default values because testing the reset fields without default is working without any problems.

But how do i test this properly? Thanks in advance!

over 4 years ago · Santiago Trujillo
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