TriggerEventHandler in unit testing angular 4

守給你的承諾、 提交于 2019-12-13 01:24:26

问题


I had an element

<button class="next-btn" (mouseup)="someMethod()">Next</button>

I want to simulate mouseup event in testing and i do this

const nextBtnElem = fixture.debugElement.nativeElement;
const elem = nextBtnElem.getElementsByTagName('button')[1]
elem.triggerEventHandler('mouseup', null);

it doesn't work, but if i change it like this

const nextBtnElem = fixture.debugElement.query(By.css('.next-btn');
nextBtnElem.triggerEventHandler('mouseup', null);

Now its working. I can't understand why first optional doesn't work?


回答1:


triggerEventHandler is a function that exists on Angular's DebugElement. In your first code snippet you are calling triggerEventHandler on a DOM object, that does not provide this functionality. On DOM objects you could use dispatchEvent but you must call fixture.detectChanges(); between triggering your event and your assertions then.



来源:https://stackoverflow.com/questions/47753863/triggereventhandler-in-unit-testing-angular-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!