Search code examples
javascriptangularkarma-jasmineangular-formsangular-unit-test

How to test resetForm function for template driven forms in angular using Jasmine Karm test?


I am trying to unit test resetform() function [used as TemplateVar.resetForm()] where loginVar is used as template variable for template driven form in angular. I am not getting any sources oridea ho to do that. Please guide. Thanks in advance!

component.html

<form #loginVar="ngForm">
```code form fields```
</form>
<button mat-raised-button color="primary" (click)="onReset(loginVar)">Reset</button>

component.ts

  onReset(e) {
    e.resetForm();
  }

Solution

  •   it('should reset form', () => {
        const debugElement = fixture.debugElement;
        const form: NgForm = debugElement.children[0].injector.get(NgForm);
        const spy = spyOn(form, 'resetForm');
        component.ClearForm(form);
        expect(spy).toHaveBeenCalled();
      });
    

    This worked!