Angular click select option in component test

后端 未结 3 1676
不思量自难忘°
不思量自难忘° 2021-02-05 14:06

I have tried the following to try to click an option in a select dropdown none of which work.

selectEl = fixture.debugElement.query(By.css(\'#dropdown\'));
sele         


        
3条回答
  •  后悔当初
    2021-02-05 14:20

    The way to change the selected option of a dropdown is to set the dropdown value and then dispatch a change event.

    You can use this answer as reference: Angular unit test select onChange spy on empty value

    In your case, you should do something like this:

      const select: HTMLSelectElement = fixture.debugElement.query(By.css('#dropdown')).nativeElement;
      select.value = select.options[3].value;  // <-- select a new value
      select.dispatchEvent(new Event('change'));
      fixture.detectChanges();
    

提交回复
热议问题