Skip to content

Commit 5fdb938

Browse files
authored
Add files via upload
1 parent 4b918ee commit 5fdb938

File tree

1 file changed

+50
-45
lines changed

1 file changed

+50
-45
lines changed

test/unit/manipulation.js

Lines changed: 50 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3100,65 +3100,70 @@ testIframe(
31003100
}
31013101
);
31023102

3103+
// Custom event handler test case
31033104

3104-
// new test cases
3105+
QUnit.test( "should handle custom '_se-custom-destroy' event correctly", function( assert ) {
31053106

3106-
function nodeName( elem, name ) {
3107-
return elem.nodeName && elem.nodeName.toLowerCase() === name.toLowerCase();
3108-
}
3107+
// Set up the HTML structure using innerHTML within #qunit-fixture
31093108

3110-
function getAll( context, tag ) {
3109+
assert.expect( 2 );
31113110

3112-
// Support: IE <=9 - 11+
3113-
// Use typeof to avoid zero-argument method invocation on host objects (trac-15151)
3114-
var ret;
3111+
var fixture = jQuery( "#qunit-fixture" );
3112+
3113+
fixture.html( `
3114+
<div id="container">
3115+
<div class="guarded removeself" data-elt="one">
3116+
Guarded 1
3117+
</div>
3118+
<div class="guarded" data-elt="two">
3119+
Guarded 2
3120+
</div>
3121+
<div class="guarded" data-elt="three">
3122+
Guarded 3
3123+
</div>
3124+
</div>
3125+
` );
3126+
3127+
// JavaScript code to be tested
3128+
3129+
jQuery.event.special[ "_se-custom-destroy" ] = {
3130+
remove: function( _handleObj ) {
3131+
var $t = jQuery( this );
3132+
console.log( $t.data( "elt" ) );
3133+
if ( $t.is( ".removeself" ) ) {
3134+
$t.remove();
3135+
}
3136+
}
3137+
};
31153138

3116-
if ( typeof context.querySelectorAll !== "undefined" ) {
3117-
ret = context.querySelectorAll( tag || "*" );
3139+
// Attach the custom event handler
31183140

3119-
} else {
3120-
ret = [];
3121-
}
3141+
jQuery( ".guarded" ).on( "_se-custom-destroy", function( ) { } );
31223142

3123-
if ( tag === undefined || tag && nodeName( context, tag ) ) {
3124-
return jQuery.merge( [ context ], ret );
3125-
}
3143+
// Spy on console.log
31263144

3127-
return ret;
3128-
}
3145+
var consoleLog = console.log;
31293146

3130-
QUnit.test( "should retrieve all elements with a specific tag", function( assert ) {
3131-
assert.expect( 1 );
3132-
var context = document.createElement( "div" );
3133-
context.innerHTML = "<span></span><span></span>";
3147+
var logMessages = [];
31343148

3135-
var result = getAll( context, "span" );
3136-
assert.equal( result.length, 2, "Found two span elements" );
3137-
} );
3149+
console.log = function( message ) {
3150+
logMessages.push( message );
3151+
};
31383152

3139-
QUnit.test( "should retrieve all elements if no tag is specified", function( assert ) {
3140-
assert.expect( 1 );
3141-
var context = document.createElement( "div" );
3142-
context.innerHTML = "<span></span><span></span>";
3153+
// Trigger the event by emptying the container
31433154

3144-
var result = getAll( context );
3145-
assert.equal( result.length, 3, "Found three elements including the context itself" );
3146-
} );
3155+
jQuery( "#container" ).empty( );
31473156

3148-
QUnit.test( "should retrieve elements including the context if it matches the tag", function( assert ) {
3149-
assert.expect( 1 );
3150-
var context = document.createElement( "div" );
3151-
context.innerHTML = "<div></div><span></span>";
3157+
// Verify that the elements with class 'removeself' were removed
31523158

3153-
var result = getAll( context, "div" );
3154-
assert.equal( result.length, 2, "Found two div elements including the context itself" );
3155-
} );
3159+
assert.equal( jQuery( ".removeself" ).length, 0, "Elements with class 'removeself' should be removed" );
3160+
3161+
// Verify console logs
3162+
3163+
assert.deepEqual( logMessages, [ "one", "two", "three" ], "Console logs should match the data-elt attributes" );
3164+
3165+
// Restore console.log
31563166

3157-
QUnit.test( "should return an empty array if no matching elements are found", function( assert ) {
3158-
assert.expect( 1 );
3159-
var context = document.createElement( "div" );
3160-
context.innerHTML = "<span></span><span></span>";
3167+
console.log = consoleLog;
31613168

3162-
var result = getAll( context, "p" );
3163-
assert.equal( result.length, 0, "Found no p elements" );
31643169
} );

0 commit comments

Comments
 (0)