Opened 10 months ago

#15271 new bug

Autocomplete menu selecting wrong item on click in tests

Reported by: Bennie Swart Owned by:
Priority: minor Milestone: none
Component: ui.menu Version: 1.12.1
Keywords: Cc:
Blocked by: Blocking:

Description

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):

// TODO: It should never be possible to not have an active item at this
// point, but the tests don't trigger mouseenter before click.

The item to be selected is determined by the following line:

this.active = this.active || $( event.target ).closest( ".ui-menu-item" );

Thus, if this.active has not been properly set by preceding events, the wrong element may be selected.

I propose the following:

this.active = $( event.target ).is( ".ui-menu-item" ) ? $( event.target ) : this.active || $( event.target ).closest( ".ui-menu-item" );

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:

  • jQuery 2.1.4
  • Chrome 63
  • Ubuntu 16.04 and Arch

I will submit a PR if the proposed solution is deemed viable.

Change History (0)

Note: See TracTickets for help on using tickets.