Skip to content

Commit deba37e

Browse files
committed
Tests: Simulate events when CI hinders use of native ones
Ref jquerygh-3732
1 parent 928c580 commit deba37e

File tree

2 files changed

+107
-125
lines changed

2 files changed

+107
-125
lines changed

test/index.html

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,6 @@
4343
<!-- this iframe is outside the #qunit-fixture so it won't waste time by constantly reloading; the tests are "safe" and clean up after themselves -->
4444
<iframe id="loadediframe" name="loadediframe" style="display:none;" src="data/iframe.html"></iframe>
4545
<dl id="dl" style="position:absolute;top:-32767px;left:-32767px;width:1px;">
46-
<div id="donor-outer">
47-
<form id="donor-form">
48-
<input id="donor-input" type="radio" />
49-
</form>
50-
</div>
5146
<div id="qunit-fixture">
5247
<p id="firstp">See <a id="simon1" href="http://simon.incutio.com/archive/2003/03/25/#getElementsBySelector" rel="bookmark">this blog entry</a> for more information.</p>
5348
<p id="ap">

test/unit/event.js

Lines changed: 107 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -2812,162 +2812,149 @@ QUnit.test( "preventDefault() on focusin does not throw exception", function( as
28122812
} );
28132813
input.trigger( "focus" );
28142814

2815-
// DOM focus is unreliable in TestSwarm CI; set an abort timeout
2815+
// DOM focus is unreliable in TestSwarm; set a simulated event workaround timeout
28162816
setTimeout( function() {
28172817
if ( !done ) {
28182818
return;
28192819
}
2820-
assert.ok( true, "Did not intercept focusin" );
2821-
done();
2822-
done = null;
2820+
input[ 0 ].addEventListener( "click", function( nativeEvent ) {
2821+
jQuery.event.simulate( "focusin", this, jQuery.event.fix( nativeEvent ) );
2822+
} );
2823+
input[ 0 ].click();
28232824
}, QUnit.config.testTimeout / 4 || 1000 );
28242825
} );
28252826

28262827
QUnit.test( "Donor event interference", function( assert ) {
2827-
assert.expect( 10 );
2828-
2829-
var html = "<div id='donor-outer'>" +
2830-
"<form id='donor-form'>" +
2831-
"<input id='donor-input' type='radio' />" +
2832-
"</form>" +
2833-
"</div>";
2834-
2835-
jQuery( "#qunit-fixture" ).append( html );
2836-
2837-
jQuery( "#donor-outer" ).on( "click", function( event ) {
2838-
assert.ok( true, "click bubbled to outer div" );
2839-
assert.equal( typeof event.originalEvent, "object", "make sure originalEvent exist" );
2840-
assert.equal( event.type, "click", "make sure event type is correct" );
2841-
} );
2842-
jQuery( "#donor-input" ).on( "click", function( event ) {
2843-
assert.ok( true, "got a click event from the input" );
2844-
assert.ok( !event.isPropagationStopped(), "propagation says it's not stopped" );
2845-
assert.equal( event.type, "click", "make sure event type is correct" );
2846-
assert.equal( typeof event.originalEvent, "object", "make sure originalEvent exist" );
2847-
} );
2848-
jQuery( "#donor-input" ).on( "change", function( event ) {
2849-
assert.equal( typeof event.originalEvent, "object", "make sure originalEvent exist" );
2850-
assert.equal( event.type, "change", "make sure event type is correct" );
2851-
assert.ok( true, "got a change event from the input" );
2828+
assert.expect( 8 );
2829+
2830+
var outer = jQuery(
2831+
"<div id='donor-outer'>" +
2832+
"<form id='donor-form'>" +
2833+
"<input id='donor-input' type='checkbox' />" +
2834+
"</form>" +
2835+
"</div>"
2836+
).appendTo( "#qunit-fixture" ),
2837+
input = jQuery( "#donor-input" );
2838+
2839+
input.on( "click", function( event ) {
2840+
assert.equal( event.type, "click", "click event at input" );
2841+
assert.ok( !event.isPropagationStopped(), "click event at input is still propagating" );
2842+
assert.equal( typeof event.originalEvent, "object",
2843+
"click event at input has originalEvent property" );
2844+
} );
2845+
outer.on( "click", function( event ) {
2846+
assert.equal( event.type, "click", "click event at ancestor" );
2847+
assert.ok( !event.isPropagationStopped(), "click event at ancestor is still propagating" );
2848+
assert.equal( typeof event.originalEvent, "object",
2849+
"click event at ancestor has originalEvent property" );
2850+
} );
2851+
input.on( "change", function( event ) {
2852+
assert.equal( event.type, "change", "change event at input" );
2853+
assert.equal( typeof event.originalEvent, "object",
2854+
"change event at input has originalEvent property" );
28522855
event.stopPropagation();
28532856
} );
2854-
jQuery( "#donor-input" )[ 0 ].click();
2857+
input[ 0 ].click();
28552858
} );
28562859

28572860
QUnit.test(
28582861
"native stop(Immediate)Propagation/preventDefault methods shouldn't be called",
28592862
function( assert ) {
2860-
var userAgent = window.navigator.userAgent;
2861-
2862-
if ( !( /firefox/i.test( userAgent ) || /safari/i.test( userAgent ) ) ) {
2863-
assert.expect( 1 );
2864-
assert.ok( true, "Assertions should run only in Chrome, Safari, Fx & Edge" );
2865-
return;
2866-
}
2867-
28682863
assert.expect( 3 );
28692864

2870-
var checker = {};
2871-
2872-
var html = "<div id='donor-outer'>" +
2873-
"<form id='donor-form'>" +
2874-
"<input id='donor-input' type='radio' />" +
2875-
"</form>" +
2876-
"</div>";
2877-
2878-
jQuery( "#qunit-fixture" ).append( html );
2879-
var outer = jQuery( "#donor-outer" );
2865+
var done = assert.async(),
2866+
outer = jQuery(
2867+
"<div id='donor-outer'>" +
2868+
"<form id='donor-form'>" +
2869+
"<input id='donor-input' type='checkbox' />" +
2870+
"</form>" +
2871+
"</div>"
2872+
).appendTo( "#qunit-fixture" ),
2873+
input = jQuery( "#donor-input" ),
2874+
spy = {},
2875+
finish = function() {
2876+
finish = null;
2877+
assert.strictEqual( spy.prevent.called, false, "Native preventDefault not called" );
2878+
assert.strictEqual( spy.stop.called, false, "Native stopPropagation not called" );
2879+
assert.strictEqual( spy.immediate.called, false,
2880+
"Native stopImmediatePropagation not called" );
2881+
2882+
// Remove jQuery handlers to ensure removal of capturing handlers on the document
2883+
outer.off( "focusin" );
2884+
2885+
done();
2886+
};
28802887

28812888
outer
28822889
.on( "focusin", function( event ) {
2883-
checker.prevent = sinon.stub( event.originalEvent, "preventDefault" );
2890+
spy.prevent = sinon.stub( event.originalEvent, "preventDefault" );
28842891
event.preventDefault();
2892+
setTimeout( finish );
28852893
} )
28862894
.on( "focusin", function( event ) {
2887-
checker.simple = sinon.stub( event.originalEvent, "stopPropagation" );
2895+
spy.stop = sinon.stub( event.originalEvent, "stopPropagation" );
28882896
event.stopPropagation();
28892897
} )
28902898
.on( "focusin", function( event ) {
2891-
checker.immediate = sinon.stub( event.originalEvent, "stopImmediatePropagation" );
2899+
spy.immediate = sinon.stub( event.originalEvent, "stopImmediatePropagation" );
28922900
event.stopImmediatePropagation();
28932901
} );
2902+
input.trigger( "focus" );
28942903

2895-
jQuery( "#donor-input" ).trigger( "focus" );
2896-
assert.strictEqual( checker.simple.called, false );
2897-
assert.strictEqual( checker.immediate.called, false );
2898-
assert.strictEqual( checker.prevent.called, false );
2899-
2900-
// We need to "off" it, since yes QUnit always update the fixtures
2901-
// but "focus" event listener is attached to document for focus(in | out)
2902-
// event and document doesn't get cleared obviously :)
2903-
outer.off( "focusin" );
2904-
}
2905-
);
2906-
2907-
QUnit.test(
2908-
"isSimulated property always exist on event object",
2909-
function( assert ) {
2910-
var userAgent = window.navigator.userAgent;
2911-
2912-
if ( !( /firefox/i.test( userAgent ) || /safari/i.test( userAgent ) ) ) {
2913-
assert.expect( 1 );
2914-
assert.ok( true, "Assertions should run only in Chrome, Safari, Fx & Edge" );
2915-
return;
2916-
}
2917-
2918-
assert.expect( 1 );
2919-
2920-
var element = jQuery( "<input/>" );
2921-
2922-
jQuery( "#qunit-fixture" ).append( element );
2923-
2924-
element.on( "focus", function( event ) {
2925-
assert.notOk( event.isSimulated );
2926-
} );
2927-
2928-
element.trigger( "focus" );
2904+
// DOM focus is unreliable in TestSwarm; set a simulated event workaround timeout
2905+
setTimeout( function() {
2906+
if ( !finish ) {
2907+
return;
2908+
}
2909+
input[ 0 ].addEventListener( "click", function( nativeEvent ) {
2910+
jQuery.event.simulate( "focusin", this, jQuery.event.fix( nativeEvent ) );
2911+
} );
2912+
input[ 0 ].click();
2913+
}, QUnit.config.testTimeout / 4 || 1000 );
29292914
}
29302915
);
29312916

2932-
QUnit.test( "originalEvent property for Chrome, Safari, Fx & Edge of simulated event", function( assert ) {
2933-
var userAgent = window.navigator.userAgent;
2934-
2935-
if ( !( /firefox/i.test( userAgent ) || /safari/i.test( userAgent ) ) ) {
2936-
assert.expect( 1 );
2937-
assert.ok( true, "Assertions should run only in Chrome, Safari, Fx & Edge" );
2938-
return;
2939-
}
2940-
2941-
assert.expect( 4 );
2942-
var done = assert.async();
2917+
QUnit.test( "originalEvent type of simulated event", function( assert ) {
2918+
assert.expect( 2 );
29432919

2944-
var html = "<div id='donor-outer'>" +
2945-
"<form id='donor-form'>" +
2946-
"<input id='donor-input' type='radio' />" +
2947-
"</form>" +
2948-
"</div>";
2949-
2950-
jQuery( "#qunit-fixture" ).append( html );
2951-
var outer = jQuery( "#donor-outer" );
2952-
2953-
outer
2954-
.on( "focusin", function( event ) {
2955-
assert.ok( true, "focusin bubbled to outer div" );
2956-
assert.equal( event.originalEvent.type, "focus",
2957-
"make sure originalEvent type is correct" );
2958-
assert.equal( event.type, "focusin", "make sure type is correct" );
2959-
} );
2920+
var done = assert.async(),
2921+
outer = jQuery(
2922+
"<div id='donor-outer'>" +
2923+
"<form id='donor-form'>" +
2924+
"<input id='donor-input' type='checkbox' />" +
2925+
"</form>" +
2926+
"</div>"
2927+
).appendTo( "#qunit-fixture" ),
2928+
input = jQuery( "#donor-input" ),
2929+
expectedType = "focus",
2930+
finish = function() {
2931+
finish = null;
2932+
2933+
// Remove jQuery handlers to ensure removal of capturing handlers on the document
2934+
outer.off( "focusin" );
2935+
2936+
done();
2937+
};
29602938

2961-
jQuery( "#donor-input" ).on( "focus", function() {
2962-
assert.ok( true, "got a focus event from the input" );
2963-
done();
2939+
outer.on( "focusin", function( event ) {
2940+
assert.equal( event.type, "focusin", "focusin event at ancestor" );
2941+
assert.equal( event.originalEvent.type, expectedType,
2942+
"focus event at ancestor has correct originalEvent type" );
2943+
setTimeout( finish );
29642944
} );
2965-
jQuery( "#donor-input" ).trigger( "focus" );
2945+
input.trigger( "focus" );
29662946

2967-
// We need to "off" it, since yes QUnit always update the fixtures
2968-
// but "focus" event listener is attached to document for focus(in | out)
2969-
// event and document doesn't get cleared obviously :)
2970-
outer.off( "focusin" );
2947+
// DOM focus is unreliable in TestSwarm; set a simulated event workaround timeout
2948+
setTimeout( function() {
2949+
if ( !finish ) {
2950+
return;
2951+
}
2952+
input[ 0 ].addEventListener( "click", function( nativeEvent ) {
2953+
expectedType = nativeEvent.type;
2954+
jQuery.event.simulate( "focusin", this, jQuery.event.fix( nativeEvent ) );
2955+
} );
2956+
input[ 0 ].click();
2957+
}, QUnit.config.testTimeout / 4 || 1000 );
29712958
} );
29722959

29732960
QUnit.test( "trigger('click') on radio passes extra params", function( assert ) {

0 commit comments

Comments
 (0)