Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

275
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!