Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular unit test - how to test reactive form reset method?

I'm trying to test this method:

doResetForm(){
   this.form.reset();
}

so I tried:

it('should reset the form', () => {
   const spyformReset = jest.spyOn(component.form, 'reset');
   
   fixture.detectChanges();
   component.doResetForm();
   expect(spyformReset).toHaveBeenCalled();
}

but I'm getting the error:

Cannot spyon on a primitive value, undefined give

like image 374
paulotarcio Avatar asked Feb 19 '26 23:02

paulotarcio


1 Answers

You can use spyOn to mock the method.

const spyformReset = spyOn(component.form, 'reset').and.callThrough();
component.doResetForm();
expect(spyformReset).toHaveBeenCalled();
like image 59
Mahdi Avatar answered Feb 21 '26 13:02

Mahdi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!