From ec52d38cceb40cbdac556f67bdf9e67084a2efaa Mon Sep 17 00:00:00 2001 From: bnjmnm Date: Wed, 8 Dec 2021 16:22:44 -0500 Subject: [PATCH] Autocomplete: eslint and test updates --- tests/unit/autocomplete/core.js | 80 +++++++++++++++++++-------------- ui/widgets/autocomplete.js | 14 +++--- 2 files changed, 54 insertions(+), 40 deletions(-) diff --git a/tests/unit/autocomplete/core.js b/tests/unit/autocomplete/core.js index d78db3bb90e..d2779e11095 100644 --- a/tests/unit/autocomplete/core.js +++ b/tests/unit/autocomplete/core.js @@ -293,6 +293,7 @@ QUnit.test( "simultaneous searches (#9334)", function( assert ) { } ); QUnit.test( "ARIA", function( assert ) { + var ready = assert.async(); assert.expect( 13 ); var element = $( "#autocomplete" ).autocomplete( { source: [ "java", "javascript" ] @@ -308,43 +309,51 @@ QUnit.test( "ARIA", function( assert ) { "Live region's role attribute must be status" ); element.autocomplete( "search", "j" ); - assert.equal( liveRegion.children().first().text(), - "2 results are available, use up and down arrow keys to navigate.", - "Live region for multiple values" ); + setTimeout( function() { + assert.equal( liveRegion.children().first().text(), + "2 results are available, use up and down arrow keys to navigate.", + "Live region for multiple values" ); - element.simulate( "keydown", { keyCode: $.ui.keyCode.DOWN } ); - assert.equal( liveRegion.children().filter( ":visible" ).text(), "java", - "Live region changed on keydown to announce the highlighted value" ); + element.simulate( "keydown", { keyCode: $.ui.keyCode.DOWN } ); + setTimeout( function() { + assert.equal( liveRegion.children().filter( ":visible" ).text(), "java", + "Live region changed on keydown to announce the highlighted value" ); - element.one( "autocompletefocus", function( event ) { - event.preventDefault(); - } ); - element.simulate( "keydown", { keyCode: $.ui.keyCode.DOWN } ); - assert.equal( liveRegion.children().filter( ":visible" ).text(), "javascript", - "Live region updated when default focus is prevented" ); - - element.autocomplete( "search", "javas" ); - assert.equal( liveRegion.children().filter( ":visible" ).text(), - "1 result is available, use up and down arrow keys to navigate.", - "Live region for one value" ); - - element.autocomplete( "search", "z" ); - assert.equal( liveRegion.children().filter( ":visible" ).text(), "No search results.", - "Live region for no values" ); - - assert.equal( liveRegion.children().length, 5, - "Should be five children in the live region after the above" ); - assert.equal( liveRegion.children().filter( ":visible" ).length, 1, - "Only one should be still visible" ); - assert.ok( liveRegion.children().filter( ":visible" )[ 0 ] === liveRegion.children().last()[ 0 ], - "The last one should be the visible one" ); - - element.autocomplete( "destroy" ); - assert.equal( liveRegion.parent().length, 0, - "The liveRegion should be detached after destroy" ); + element.one( "autocompletefocus", function( event ) { + event.preventDefault(); + } ); + element.simulate( "keydown", { keyCode: $.ui.keyCode.DOWN } ); + setTimeout( function() { + assert.equal( liveRegion.children().filter( ":visible" ).text(), "javascript", + "Live region updated when default focus is prevented" ); + element.autocomplete( "search", "javas" ); + setTimeout( function() { + assert.equal( liveRegion.children().filter( ":visible" ).text(), + "1 result is available, use up and down arrow keys to navigate.", + "Live region for one value" ); + element.autocomplete( "search", "z" ); + setTimeout( function() { + assert.equal( liveRegion.children().filter( ":visible" ).text(), "No search results.", + "Live region for no values" ); + assert.equal( liveRegion.children().length, 1, + "Should be one child in the live region after the above" ); + assert.equal( liveRegion.children().filter( ":visible" ).length, 1, + "Only one should be still visible" ); + assert.ok( liveRegion.children().filter( ":visible" )[ 0 ] === liveRegion.children().last()[ 0 ], + "The last one should be the visible one" ); + element.autocomplete( "destroy" ); + assert.equal( liveRegion.parent().length, 0, + "The liveRegion should be detached after destroy" ); + ready(); + }, 500 ); + }, 500 ); + }, 500 ); + }, 500 ); + }, 500 ); } ); QUnit.test( "ARIA, aria-label announcement", function( assert ) { + var ready = assert.async(); assert.expect( 1 ); $.widget( "custom.catcomplete", $.ui.autocomplete, { _renderMenu: function( ul, items ) { @@ -361,8 +370,11 @@ QUnit.test( "ARIA, aria-label announcement", function( assert ) { liveRegion = element.catcomplete( "instance" ).liveRegion; element.catcomplete( "search", "a" ); element.simulate( "keydown", { keyCode: $.ui.keyCode.DOWN } ); - assert.equal( liveRegion.children().filter( ":visible" ).text(), "People : anders andersson", - "Live region changed on keydown to announce the highlighted value's aria-label attribute" ); + setTimeout( function() { + assert.equal( liveRegion.children().filter( ":visible" ).text(), "People : anders andersson", + "Live region changed on keydown to announce the highlighted value's aria-label attribute" ); + ready(); + }, 500 ); } ); QUnit.test( "ARIA, init on detached input", function( assert ) { diff --git a/ui/widgets/autocomplete.js b/ui/widgets/autocomplete.js index a3af2b6e185..ddfa92f6841 100644 --- a/ui/widgets/autocomplete.js +++ b/ui/widgets/autocomplete.js @@ -269,9 +269,10 @@ $.widget( "ui.autocomplete", { label = ui.item.attr( "aria-label" ) || item.value; if ( label && String.prototype.trim.call( label ).length ) { clearTimeout( this.liveRegionTimer ); - this.liveRegionTimer = setTimeout(() => { - this.liveRegion.html($( "
" ).text( label )); - }, 400); + var that = this; + this.liveRegionTimer = setTimeout( function() { + that.liveRegion.html( $( "
" ).text( label ) ); + }, 400 ); } }, menuselect: function( event, ui ) { @@ -667,9 +668,10 @@ $.widget( "ui.autocomplete", $.ui.autocomplete, { message = this.options.messages.noResults; } clearTimeout( this.liveRegionTimer ); - this.liveRegionTimer = setTimeout(() => { - this.liveRegion.html($( "
" ).text( message )); - }, 400); + var that = this; + this.liveRegionTimer = setTimeout( function() { + that.liveRegion.html( $( "
" ).text( message ) ); + }, 400 ); } } );