Units Testing events in Karma angular 2 application

I am trying to unit test abc (Event) method using karma. I have a dropdown in my view page that has 4 options that will call the abc (Event) method when changed.

Here is my view page:

<div class="myClass">
<select (change)="abc($event)" id="my">
    <option value="one">One</option>
    <option value="two">Two</option>
    <option value="three">Three</option>
    <option value="four">Four</option>
</select>

      

My component.ts file contains the definition of the abc method.

import {Component,OnInit} from '@angular/core';
import { LocaleService } from '../../../services/locale.service';
@Component({
selector: 'app-localeselector',
templateUrl: './localeselector.component.html',
styleUrls: ['./localeselector.component.scss']
})


export class LocaleselectorComponent implements OnInit{

 private localeService: any;
 private language;
 constructor() { }

 ngOnInit() { }

}

abc(ev: Event) {
    //something...
}
}

      

How can I test the abc method? Also how to mock $ event? Thanks in advance.

+3


source to share


1 answer


Thanks, it worked !!! :) I found a solution. Could make fun of the event by sending an object.

it('should work', () => {
  component.abc({ srcElement: { value: 'xyz' } });
  expect(someMethod.getValue()).toEqual('xyz');
});

      



I also changed the abc method in component.ts file to

abc($event){
  //do something...
}

      

+2


source







All Articles