0

I am trying to write a unit test case to clear the checked values of radio button

By clicking the delete icon link it should clear the values

HTML file

 <mat-icon  class="remove-icon" (click)="resetradioValues('gender')">delete</mat-icon>

ts file

 resetradioValues(name: string){
    this.form.get(name).patchValue(null);
  }

I have written a unit test case for the above code but it is not working for me

it('should clear radio button values', () => {
    const param = Object.assign({},radio, { name: 'test' });
    console.log(param);
    component.resetradioValues(param.name);

    });

Please let me know anybody can solve this issue

1 Answer 1

1

We will be doing an integration test by actually clicking the icon and seeing what happens.

Try:

it('should clear radio button values', () => {
      // arrangement
      const matIconElement = fixture.debugElement.query(By.css('mat-icon.remove-icon')).nativeElement;
      // click
      matIconElement.click();
      // assertions, you can assert how you like
      expect(component.form.get('test').value).toBe(null);
    });
// ========== Edit (Unit test) ==============
it('should clear radio button values', () => {
      // arrangement
      component.resetradioValues('test);
      // assertions, you can assert how you like
      expect(component.form.get('test').value).toBe(null);
    });
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your reply. I want to use resetradioValues function in the unit test case.
I am getting error TypeError: Cannot read property 'forEach' of null
Somewhere in your component, I bet there is a forEach and that variable is null. Make sure you instantiate it in your unit test.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.