Home > Back-end >  Angular Unit Testing : Karma-Jasmine : ('onChange', (changeEvent: MatRadioChange) : Argume
Angular Unit Testing : Karma-Jasmine : ('onChange', (changeEvent: MatRadioChange) : Argume

Time:07-27

Hi was working on one of the tests for a call that I am using to emit the event:

onChange(eventName: MatRadioChange): void {
    this.eventName.emit(eventName.value);
  }

The test for the same is:

describe('onChange', (eventName: MatRadioChange) => {
    it('should emit true to open calendar modal for mobile view', () => {
      spyOn(component.eventName, 'emit');
      component.onChange(eventName.value);
      expect(component.eventName.emit).toHaveBeenCalled();
    });
  });

I keep getting the error:

Argument of type '(changeEvent: MatRadioChange) => void' is not assignable to parameter of type '() => void'.

describe('onChange', (changeEvent: MatRadioChange) => {

Any help will be great!

CodePudding user response:

This is a TypeScript issue. The issue is on this line:

component.onChange(eventName.value);

I assume eventName.value is a string and not MatRadioChange.

To get by this, do this:

component.onChange({ value: 'abc' } as any);

The as any casts the object to any type and it should suffice the MatRadioChange type.

  • Related