Menu: fix selecting wrong element in tests #1861
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Copied from https://bugs.jqueryui.com/ticket/15271:
During automated testing the autocomplete menu sometimes selects the wrong item upon click.
This can be reproduced by making a page with a normal autocomplete input with a couple of options. The tests populate the text of the input after which the menu opens. Then, the desired item is clicked. Even though the click event reports the correct item was clicked, the item which is actually selected by the autocomplete plugin may be a different one.
I could only reproduce this in automated tests and not by hand. The issue is most probably related to the tests not firing all events that would fire during normal operation. This is actually mentioned in a comment in Menu#select (https://github.com/jquery/jquery-ui/blob/master/ui/widgets/menu.js#L673):
The item to be selected is determined by the following line:
Thus, if this.active has not been properly set by preceding events, the wrong element may be selected.
I propose the following:
In other words, if the target element is a menu item, then it is used as the active element. Otherwise, determine the active element as per usual. One might even add an explicit check for the event type, if necessary.
I'm seeing this in 1.11.4, but the error is still present in master at the time of writing (1.12.1). Additionally: