-
Notifications
You must be signed in to change notification settings - Fork 24.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
listeners property in DebugElement instance does not represent actual event listeners of the element #22148
Comments
Possibly it is a feature... I researched a lot and I couldn't find the solution how to correctly handle this. Please, provide some information if so. Any workaround how to handle this? How to get correct handlers in listeners property of DebugElement? |
I ran into the same problem. It seems .triggerEventHandler does not trigger a real event on the DOM element, but will fake a call to any HostListners. I've found two workarounds:
It sucks, i would really like .triggerEventHander to instead wrap the logic from my second workaround. |
I run into the same problem again in absolutely another case. I have some component with addEventListener (elementRef.nativeElement) and scroll event. When I trigger event with triggerEventHandler @Hojou yep, your solution works. |
Any ideas how this work at the moment? Maybe I can take & fix this issue. I refactored my PR so I add listeners just in template. And this option on how to bind events is not influenced by this bug. Everything works correctly. But this is huge perf problem. Using template bindings for events like scroll spams |
I researched a bit how this works. So:
Am I right with my last statement? So the option is to use native functions like click/blur/focus of HTMLElement. We can use them though Is it correct way to trigger events for HTMLElements? Or triggerEventHandler should handle any types of event bindings (template, addEventListener and stuff). Weeeell, we also can find this (not explicitly) in the docs: https://angular.io/guide/testing#click-helper. |
So I finished with that code at the moment: Unit:
And the code:
And for blur. Unit:
Code:
Is that correct way to unit test events which are listened with |
This is working as intended. The Angular
instead of
|
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
I'm submitting a...
Current behavior
I have a component. There is some HTMLElement in it. At some moment I add some event listener to this element (addEventListener, RxJS subscription of
Observable.fromEvent
or whatever). Everything works perfectly in production. The problem is in unit tests. Specifically in DebugElement (I suppose). The problem is that listeners array do not represent actual state of event listeners on the element.I find html element by
de.query(By.css('.some-class'))
at some moment in unit test. After that something happens, that element gets new event listener (rxjs subscription for instance). After that I want to emulate some event bytriggerEventHandler
and I want to check, that component reacted correctly. Example:And the
createKeydownSubscription
code is:This unit test does not work.
Also, if you compare eventListeners() array of the element and listeners property of DebugElement of that element, they will be different.
Expected behavior
I expect that listeners array in DebugElement instance represents actual listeners of the element.
Minimal reproduction of the problem with instructions
I created repro as github repo. So the repro is:
git clone git@github.com:sharikovvladislav/ng-rxjs-from-event-test.git
You will get:
Executed 2 of 2 (2 FAILED) ERROR
(a bit more verbose).All code is provided in the repro: component and spec
What is the motivation / use case for changing the behavior?
It is the bug. It has to be fixed. Until it is not fixed, it is not comfortable to unit test some cases.
Environment
The text was updated successfully, but these errors were encountered: