How to properly test ngForm reset? (reset with default value)

Viewed 149

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!

0 Answers
Related