🌐 AI搜索 & 代理 主页
Skip to content

Commit 12ff6a7

Browse files
committed
Tests: Add a few tests
Tests added: * sequences triggering focus and/or blur (authored by Richard Gibson) * focus does not bubble * some test stability fixes
1 parent 2ef764d commit 12ff6a7

File tree

1 file changed

+103
-2
lines changed

1 file changed

+103
-2
lines changed

test/unit/event.js

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2172,12 +2172,12 @@ QUnit.test( "focusin bubbles", function( assert ) {
21722172

21732173
// Removed since DOM focus is unreliable on test swarm
21742174
// DOM focus method
2175-
// input[0].focus();
2175+
// input[ 0 ].focus();
21762176

21772177
// To make the next focus test work, we need to take focus off the input.
21782178
// This will fire another focusin event, so set order to reflect that.
21792179
// order = 1;
2180-
// jQuery("#text1")[0].focus();
2180+
// jQuery( "#text1" )[ 0 ].focus();
21812181

21822182
// jQuery trigger, which calls DOM focus
21832183
order = 0;
@@ -2187,6 +2187,42 @@ QUnit.test( "focusin bubbles", function( assert ) {
21872187
jQuery( "body" ).off( "focusin.focusinBubblesTest" );
21882188
} );
21892189

2190+
QUnit.test( "focus does not bubble", function( assert ) {
2191+
assert.expect( 1 );
2192+
2193+
var done = assert.async(),
2194+
input = jQuery( "<input type='text' />" ).prependTo( "body" );
2195+
2196+
// focus the element so DOM focus won't fire
2197+
input[ 0 ].focus();
2198+
2199+
jQuery( "body" ).on( "focus.focusDoesNotBubbleTest", function() {
2200+
assert.ok( false, "focus doesn't fire on body" );
2201+
} );
2202+
2203+
input.on( "focus.focusDoesNotBubbleTest", function() {
2204+
assert.ok( true, "focus on the element" );
2205+
} );
2206+
2207+
// Removed since DOM focus is unreliable on test swarm
2208+
// DOM focus method
2209+
// input[ 0 ].focus();
2210+
2211+
// To make the next focus test work, we need to take focus off the input.
2212+
// This will fire another focusin event, so set order to reflect that.
2213+
// jQuery( "#text1" )[ 0 ].focus();
2214+
2215+
// jQuery trigger, which calls DOM focus
2216+
input.trigger( "focus" );
2217+
2218+
input.remove();
2219+
jQuery( "body" ).off( "focus.focusDoesNotBubbleTest" );
2220+
2221+
setTimeout( function() {
2222+
done();
2223+
}, 50 );
2224+
} );
2225+
21902226
QUnit.test( "custom events with colons (trac-3533, trac-8272)", function( assert ) {
21912227
assert.expect( 1 );
21922228

@@ -2652,6 +2688,10 @@ QUnit.test( "element removed during focusout (gh-4417)", function( assert ) {
26522688
button[ 0 ].blur = function() {
26532689
jQuery.cleanData( [ this ] );
26542690
this.parentNode.removeChild( this );
2691+
2692+
// Redefine `blur` to avoid a hard crash in Karma tests that stop
2693+
// the test runner in case this test fails.
2694+
this.blur = jQuery.noop;
26552695
};
26562696

26572697
button[ 0 ].click();
@@ -3212,6 +3252,17 @@ QUnit.test( "Event handling works with multiple async focus events (gh-4350)", f
32123252
if ( remaining > 0 ) {
32133253
input.trigger( "blur" );
32143254
} else {
3255+
3256+
if ( QUnit.isIE ) {
3257+
3258+
// Support: <=IE 11+
3259+
// In IE, one of the blurs sometimes triggers a focus on body
3260+
// which in turn restores focus to the input, leading to 4 assertions
3261+
// firing instead of three. This only happens if other tests are
3262+
// running on the same test page. Avoid this issue in tests by removing
3263+
// the handler early.
3264+
input.off( "focus" );
3265+
}
32153266
done();
32163267
}
32173268
} )
@@ -3237,6 +3288,45 @@ QUnit.test( "Event handling works with multiple async focus events (gh-4350)", f
32373288
} );
32383289
} );
32393290

3291+
// Support: IE <=9 - 11+
3292+
// focus and blur events are asynchronous.
3293+
// The browser window must be topmost for this to work properly!!
3294+
QUnit.test( "async focus queues properly (gh-4859)", function( assert ) {
3295+
assert.expect( 1 );
3296+
3297+
var $text = jQuery( "#text1" ),
3298+
$radio = jQuery( "#radio1" ),
3299+
done = assert.async();
3300+
3301+
$text.trigger( "focus" );
3302+
$radio.trigger( "focus" );
3303+
$text.trigger( "focus" );
3304+
3305+
setTimeout( function() {
3306+
assert.equal( document.activeElement, $text[ 0 ], "focus follows the last trigger" );
3307+
done();
3308+
}, 500 );
3309+
} );
3310+
3311+
// Support: IE <=9 - 11+
3312+
// focus and blur events are asynchronous.
3313+
// The browser window must be topmost for this to work properly!!
3314+
QUnit.test( "async focus queues properly with blur (gh-4856)", function( assert ) {
3315+
assert.expect( 1 );
3316+
3317+
var $text = jQuery( "#text1" ),
3318+
done = assert.async();
3319+
3320+
$text.trigger( "focus" );
3321+
$text.trigger( "blur" );
3322+
$text.trigger( "focus" );
3323+
3324+
setTimeout( function() {
3325+
assert.equal( document.activeElement, $text[ 0 ], "focus-after-blur is respected" );
3326+
done();
3327+
}, 500 );
3328+
} );
3329+
32403330
QUnit.test( "native-backed events preserve trigger data (gh-1741, gh-4139)", function( assert ) {
32413331
assert.expect( 17 );
32423332

@@ -3277,6 +3367,17 @@ QUnit.test( "native-backed events preserve trigger data (gh-1741, gh-4139)", fun
32773367
var type = event.type;
32783368
assert.deepEqual( slice.call( arguments, 1 ), data,
32793369
type + " handler received correct data" );
3370+
3371+
if ( QUnit.isIE && type === "focus" ) {
3372+
3373+
// Support: <=IE 11+
3374+
// In IE, one of the blurs sometimes triggers a focus on body
3375+
// which in turn restores focus to the input, leading to 4 assertions
3376+
// firing instead of three. This only happens if other tests are
3377+
// running on the same test page. Avoid this issue in tests by removing
3378+
// the handler early.
3379+
checkbox.off( "focus" );
3380+
}
32803381
} );
32813382
checkbox.trigger( "focus", data );
32823383
setTimeout( function() {

0 commit comments

Comments
 (0)