2015-08-16 03:45:28 +00:00
|
|
|
QUnit.module( "callbacks", {
|
2019-02-18 18:02:38 +00:00
|
|
|
afterEach: moduleTeardown
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-09-12 15:37:41 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
( function() {
|
2011-09-12 15:37:41 +00:00
|
|
|
|
2022-07-12 15:12:27 +00:00
|
|
|
if ( !includesModule( "callbacks" ) ) {
|
2020-01-27 17:54:47 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
( function() {
|
|
|
|
|
2011-09-12 15:37:41 +00:00
|
|
|
var output,
|
|
|
|
addToOutput = function( string ) {
|
|
|
|
return function() {
|
|
|
|
output += string;
|
|
|
|
};
|
|
|
|
},
|
2015-08-16 06:59:58 +00:00
|
|
|
outputA = addToOutput( "A" ),
|
|
|
|
outputB = addToOutput( "B" ),
|
|
|
|
outputC = addToOutput( "C" ),
|
2023-09-20 22:18:42 +00:00
|
|
|
/* eslint-disable key-spacing */
|
2011-09-12 15:37:41 +00:00
|
|
|
tests = {
|
2012-10-06 20:00:43 +00:00
|
|
|
"": "XABC X XABCABCC X XBB X XABA X XX",
|
|
|
|
"once": "XABC X X X X X XABA X XX",
|
|
|
|
"memory": "XABC XABC XABCABCCC XA XBB XB XABA XC XX",
|
|
|
|
"unique": "XABC X XABCA X XBB X XAB X X",
|
|
|
|
"stopOnFalse": "XABC X XABCABCC X XBB X XA X XX",
|
|
|
|
"once memory": "XABC XABC X XA X XA XABA XC XX",
|
|
|
|
"once unique": "XABC X X X X X XAB X X",
|
|
|
|
"once stopOnFalse": "XABC X X X X X XA X XX",
|
|
|
|
"memory unique": "XABC XA XABCA XA XBB XB XAB XC X",
|
|
|
|
"memory stopOnFalse": "XABC XABC XABCABCCC XA XBB XB XA X XX",
|
|
|
|
"unique stopOnFalse": "XABC X XABCA X XBB X XA X X"
|
2011-09-12 15:37:41 +00:00
|
|
|
},
|
|
|
|
filters = {
|
|
|
|
"no filter": undefined,
|
|
|
|
"filter": function( fn ) {
|
|
|
|
return function() {
|
|
|
|
return fn.apply( this, arguments );
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
2012-10-16 14:17:14 +00:00
|
|
|
|
2012-04-01 23:03:34 +00:00
|
|
|
function showFlags( flags ) {
|
|
|
|
if ( typeof flags === "string" ) {
|
2012-10-16 14:17:14 +00:00
|
|
|
return "'" + flags + "'";
|
2012-04-01 23:03:34 +00:00
|
|
|
}
|
|
|
|
var output = [], key;
|
|
|
|
for ( key in flags ) {
|
2012-10-16 14:17:14 +00:00
|
|
|
output.push( "'" + key + "': " + flags[ key ] );
|
2012-04-01 23:03:34 +00:00
|
|
|
}
|
|
|
|
return "{ " + output.join( ", " ) + " }";
|
|
|
|
}
|
2011-09-12 15:37:41 +00:00
|
|
|
|
2012-04-01 23:03:34 +00:00
|
|
|
jQuery.each( tests, function( strFlags, resultString ) {
|
2011-09-12 15:37:41 +00:00
|
|
|
|
2012-04-01 23:03:34 +00:00
|
|
|
var objectFlags = {};
|
2011-09-12 15:37:41 +00:00
|
|
|
|
2012-04-01 23:03:34 +00:00
|
|
|
jQuery.each( strFlags.split( " " ), function() {
|
|
|
|
if ( this.length ) {
|
|
|
|
objectFlags[ this ] = true;
|
|
|
|
}
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2011-09-12 15:37:41 +00:00
|
|
|
|
2013-04-09 15:45:09 +00:00
|
|
|
jQuery.each( filters, function( filterLabel ) {
|
2011-09-12 15:37:41 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
jQuery.each( {
|
2012-10-16 20:43:30 +00:00
|
|
|
"string": strFlags,
|
|
|
|
"object": objectFlags
|
|
|
|
}, function( flagsTypes, flags ) {
|
2012-04-01 23:03:34 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "jQuery.Callbacks( " + showFlags( flags ) + " ) - " + filterLabel, function( assert ) {
|
2012-04-01 23:03:34 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.expect( 29 );
|
2012-04-01 23:03:34 +00:00
|
|
|
|
2012-04-30 01:04:45 +00:00
|
|
|
var cblist,
|
2012-04-01 23:03:34 +00:00
|
|
|
results = resultString.split( /\s+/ );
|
|
|
|
|
|
|
|
// Basic binding and firing
|
|
|
|
output = "X";
|
|
|
|
cblist = jQuery.Callbacks( flags );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( cblist.locked(), false, ".locked() initially false" );
|
|
|
|
assert.strictEqual( cblist.disabled(), false, ".disabled() initially false" );
|
|
|
|
assert.strictEqual( cblist.fired(), false, ".fired() initially false" );
|
2015-08-16 06:59:58 +00:00
|
|
|
cblist.add( function( str ) {
|
2012-04-01 23:03:34 +00:00
|
|
|
output += str;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( cblist.fired(), false, ".fired() still false after .add" );
|
2015-01-02 14:42:06 +00:00
|
|
|
cblist.fire( "A" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( output, "XA", "Basic binding and firing" );
|
|
|
|
assert.strictEqual( cblist.fired(), true, ".fired() detects firing" );
|
2012-04-01 23:03:34 +00:00
|
|
|
output = "X";
|
|
|
|
cblist.disable();
|
2015-08-16 06:59:58 +00:00
|
|
|
cblist.add( function( str ) {
|
2012-04-01 23:03:34 +00:00
|
|
|
output += str;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( output, "X", "Adding a callback after disabling" );
|
2015-08-16 06:59:58 +00:00
|
|
|
cblist.fire( "A" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( output, "X", "Firing after disabling" );
|
|
|
|
assert.strictEqual( cblist.disabled(), true, ".disabled() becomes true" );
|
|
|
|
assert.strictEqual( cblist.locked(), true, "disabling locks" );
|
2012-04-01 23:03:34 +00:00
|
|
|
|
2022-01-12 22:23:10 +00:00
|
|
|
// Emptying while firing (trac-13517)
|
2013-02-27 15:12:16 +00:00
|
|
|
cblist = jQuery.Callbacks( flags );
|
|
|
|
cblist.add( cblist.empty );
|
|
|
|
cblist.add( function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( false, "not emptied" );
|
2013-02-27 15:12:16 +00:00
|
|
|
} );
|
|
|
|
cblist.fire();
|
|
|
|
|
|
|
|
// Disabling while firing
|
|
|
|
cblist = jQuery.Callbacks( flags );
|
|
|
|
cblist.add( cblist.disable );
|
|
|
|
cblist.add( function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( false, "not disabled" );
|
2013-02-27 15:12:16 +00:00
|
|
|
} );
|
|
|
|
cblist.fire();
|
|
|
|
|
2012-04-01 23:03:34 +00:00
|
|
|
// Basic binding and firing (context, arguments)
|
|
|
|
output = "X";
|
|
|
|
cblist = jQuery.Callbacks( flags );
|
2015-08-16 06:59:58 +00:00
|
|
|
cblist.add( function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( this, window, "Basic binding and firing (context)" );
|
2012-04-01 23:03:34 +00:00
|
|
|
output += Array.prototype.join.call( arguments, "" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2012-04-01 23:03:34 +00:00
|
|
|
cblist.fireWith( window, [ "A", "B" ] );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( output, "XAB", "Basic binding and firing (arguments)" );
|
2012-04-01 23:03:34 +00:00
|
|
|
|
|
|
|
// fireWith with no arguments
|
|
|
|
output = "";
|
|
|
|
cblist = jQuery.Callbacks( flags );
|
2015-08-16 06:59:58 +00:00
|
|
|
cblist.add( function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.equal( this, window, "fireWith with no arguments (context is window)" );
|
|
|
|
assert.strictEqual( arguments.length, 0, "fireWith with no arguments (no arguments)" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2012-04-01 23:03:34 +00:00
|
|
|
cblist.fireWith();
|
|
|
|
|
|
|
|
// Basic binding, removing and firing
|
|
|
|
output = "X";
|
|
|
|
cblist = jQuery.Callbacks( flags );
|
|
|
|
cblist.add( outputA, outputB, outputC );
|
|
|
|
cblist.remove( outputB, outputC );
|
|
|
|
cblist.fire();
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( output, "XA", "Basic binding, removing and firing" );
|
2012-04-01 23:03:34 +00:00
|
|
|
|
|
|
|
// Empty
|
|
|
|
output = "X";
|
|
|
|
cblist = jQuery.Callbacks( flags );
|
|
|
|
cblist.add( outputA );
|
|
|
|
cblist.add( outputB );
|
2011-09-12 15:37:41 +00:00
|
|
|
cblist.add( outputC );
|
2012-04-01 23:03:34 +00:00
|
|
|
cblist.empty();
|
|
|
|
cblist.fire();
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( output, "X", "Empty" );
|
2012-04-01 23:03:34 +00:00
|
|
|
|
|
|
|
// Locking
|
|
|
|
output = "X";
|
|
|
|
cblist = jQuery.Callbacks( flags );
|
2015-08-16 06:59:58 +00:00
|
|
|
cblist.add( function( str ) {
|
2012-04-01 23:03:34 +00:00
|
|
|
output += str;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2012-04-01 23:03:34 +00:00
|
|
|
cblist.lock();
|
2015-08-16 06:59:58 +00:00
|
|
|
cblist.add( function( str ) {
|
2012-04-01 23:03:34 +00:00
|
|
|
output += str;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
|
|
|
cblist.fire( "A" );
|
|
|
|
cblist.add( function( str ) {
|
2012-04-01 23:03:34 +00:00
|
|
|
output += str;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( output, "X", "Lock early" );
|
|
|
|
assert.strictEqual( cblist.locked(), true, "Locking reflected in accessor" );
|
2012-04-01 23:03:34 +00:00
|
|
|
|
2015-01-04 00:55:31 +00:00
|
|
|
// Locking while firing (gh-1990)
|
|
|
|
output = "X";
|
|
|
|
cblist = jQuery.Callbacks( flags );
|
|
|
|
cblist.add( cblist.lock );
|
2015-08-16 06:59:58 +00:00
|
|
|
cblist.add( function( str ) {
|
2015-01-04 00:55:31 +00:00
|
|
|
output += str;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2015-01-04 00:55:31 +00:00
|
|
|
cblist.fire( "A" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( output, "XA", "Locking doesn't abort execution (gh-1990)" );
|
2015-01-04 00:55:31 +00:00
|
|
|
|
2012-04-01 23:03:34 +00:00
|
|
|
// Ordering
|
|
|
|
output = "X";
|
|
|
|
cblist = jQuery.Callbacks( flags );
|
2015-08-16 06:59:58 +00:00
|
|
|
cblist.add( function() {
|
2012-04-01 23:03:34 +00:00
|
|
|
cblist.add( outputC );
|
|
|
|
outputA();
|
|
|
|
}, outputB );
|
|
|
|
cblist.fire();
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( output, results.shift(), "Proper ordering" );
|
2012-04-01 23:03:34 +00:00
|
|
|
|
|
|
|
// Add and fire again
|
|
|
|
output = "X";
|
2015-08-16 06:59:58 +00:00
|
|
|
cblist.add( function() {
|
2012-04-01 23:03:34 +00:00
|
|
|
cblist.add( outputC );
|
|
|
|
outputA();
|
|
|
|
}, outputB );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( output, results.shift(), "Add after fire" );
|
2012-04-01 23:03:34 +00:00
|
|
|
|
|
|
|
output = "X";
|
|
|
|
cblist.fire();
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( output, results.shift(), "Fire again" );
|
2012-04-01 23:03:34 +00:00
|
|
|
|
|
|
|
// Multiple fire
|
|
|
|
output = "X";
|
|
|
|
cblist = jQuery.Callbacks( flags );
|
2015-08-16 06:59:58 +00:00
|
|
|
cblist.add( function( str ) {
|
2012-04-01 23:03:34 +00:00
|
|
|
output += str;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
|
|
|
cblist.fire( "A" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( output, "XA", "Multiple fire (first fire)" );
|
2012-04-01 23:03:34 +00:00
|
|
|
output = "X";
|
2015-08-16 06:59:58 +00:00
|
|
|
cblist.add( function( str ) {
|
2012-04-01 23:03:34 +00:00
|
|
|
output += str;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( output, results.shift(), "Multiple fire (first new callback)" );
|
2012-04-01 23:03:34 +00:00
|
|
|
output = "X";
|
2015-08-16 06:59:58 +00:00
|
|
|
cblist.fire( "B" );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( output, results.shift(), "Multiple fire (second fire)" );
|
2012-04-01 23:03:34 +00:00
|
|
|
output = "X";
|
2015-08-16 06:59:58 +00:00
|
|
|
cblist.add( function( str ) {
|
2012-04-01 23:03:34 +00:00
|
|
|
output += str;
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( output, results.shift(), "Multiple fire (second new callback)" );
|
2012-04-01 23:03:34 +00:00
|
|
|
|
|
|
|
// Return false
|
|
|
|
output = "X";
|
|
|
|
cblist = jQuery.Callbacks( flags );
|
2023-09-20 22:18:42 +00:00
|
|
|
cblist.add( outputA, function() {
|
|
|
|
return false;
|
|
|
|
}, outputB );
|
2012-04-01 23:03:34 +00:00
|
|
|
cblist.add( outputA );
|
|
|
|
cblist.fire();
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( output, results.shift(), "Callback returning false" );
|
2012-04-01 23:03:34 +00:00
|
|
|
|
|
|
|
// Add another callback (to control lists with memory do not fire anymore)
|
|
|
|
output = "X";
|
2011-09-12 15:37:41 +00:00
|
|
|
cblist.add( outputC );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( output, results.shift(), "Adding a callback after one returned false" );
|
2011-09-12 15:37:41 +00:00
|
|
|
|
2012-10-06 20:00:43 +00:00
|
|
|
// Callbacks are not iterated
|
|
|
|
output = "";
|
2013-04-09 15:45:09 +00:00
|
|
|
function handler() {
|
2012-10-06 20:00:43 +00:00
|
|
|
output += "X";
|
|
|
|
}
|
|
|
|
handler.method = function() {
|
|
|
|
output += "!";
|
|
|
|
};
|
|
|
|
cblist = jQuery.Callbacks( flags );
|
|
|
|
cblist.add( handler );
|
|
|
|
cblist.add( handler );
|
|
|
|
cblist.fire();
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( output, results.shift(), "No callback iteration" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|
2011-09-12 15:37:41 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
} )();
|
2012-04-25 00:13:26 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "jQuery.Callbacks( options ) - options are copied", function( assert ) {
|
2012-04-25 00:13:26 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.expect( 1 );
|
2012-04-25 00:13:26 +00:00
|
|
|
|
|
|
|
var options = {
|
2012-07-05 19:52:13 +00:00
|
|
|
"unique": true
|
2012-04-25 00:13:26 +00:00
|
|
|
},
|
2012-04-25 13:50:26 +00:00
|
|
|
cb = jQuery.Callbacks( options ),
|
|
|
|
count = 0,
|
|
|
|
fn = function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( !( count++ ), "called once" );
|
2012-04-25 13:50:26 +00:00
|
|
|
};
|
2023-09-20 22:18:42 +00:00
|
|
|
options.unique = false;
|
2012-04-25 13:50:26 +00:00
|
|
|
cb.add( fn, fn );
|
|
|
|
cb.fire();
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2012-04-25 13:50:26 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "jQuery.Callbacks.fireWith - arguments are copied", function( assert ) {
|
2012-04-25 13:50:26 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.expect( 1 );
|
2012-04-25 00:13:26 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
var cb = jQuery.Callbacks( "memory" ),
|
|
|
|
args = [ "hello" ];
|
2012-04-25 00:13:26 +00:00
|
|
|
|
2012-04-25 13:50:26 +00:00
|
|
|
cb.fireWith( null, args );
|
|
|
|
args[ 0 ] = "world";
|
2012-04-25 00:13:26 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
cb.add( function( hello ) {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( hello, "hello", "arguments are copied internally" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
|
|
|
} );
|
2012-04-25 14:08:38 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "jQuery.Callbacks.remove - should remove all instances", function( assert ) {
|
2012-04-25 14:08:38 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.expect( 1 );
|
2012-04-25 14:08:38 +00:00
|
|
|
|
|
|
|
var cb = jQuery.Callbacks();
|
|
|
|
|
|
|
|
function fn() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( false, "function wasn't removed" );
|
2012-04-25 14:08:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cb.add( fn, fn, function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "end of test" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} ).remove( fn ).fire();
|
|
|
|
} );
|
2012-08-16 17:12:59 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "jQuery.Callbacks.has", function( assert ) {
|
2013-01-03 22:05:00 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.expect( 13 );
|
2013-01-03 22:05:00 +00:00
|
|
|
|
|
|
|
var cb = jQuery.Callbacks();
|
|
|
|
function getA() {
|
|
|
|
return "A";
|
|
|
|
}
|
|
|
|
function getB() {
|
|
|
|
return "B";
|
|
|
|
}
|
|
|
|
function getC() {
|
|
|
|
return "C";
|
|
|
|
}
|
2015-08-16 06:59:58 +00:00
|
|
|
cb.add( getA, getB, getC );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( cb.has(), true, "No arguments to .has() returns whether callback function(s) are attached or not" );
|
2015-08-16 06:59:58 +00:00
|
|
|
assert.strictEqual( cb.has( getA ), true, "Check if a specific callback function is in the Callbacks list" );
|
2013-01-03 22:05:00 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
cb.remove( getB );
|
|
|
|
assert.strictEqual( cb.has( getB ), false, "Remove a specific callback function and make sure its no longer there" );
|
|
|
|
assert.strictEqual( cb.has( getA ), true, "Remove a specific callback function and make sure other callback function is still there" );
|
2013-01-03 22:05:00 +00:00
|
|
|
|
|
|
|
cb.empty();
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( cb.has(), false, "Empty list and make sure there are no callback function(s)" );
|
2015-08-16 06:59:58 +00:00
|
|
|
assert.strictEqual( cb.has( getA ), false, "Check for a specific function in an empty() list" );
|
2013-01-03 22:05:00 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
cb.add( getA, getB, function() {
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( cb.has(), true, "Check if list has callback function(s) from within a callback function" );
|
2015-08-16 06:59:58 +00:00
|
|
|
assert.strictEqual( cb.has( getA ), true, "Check if list has a specific callback from within a callback function" );
|
|
|
|
} ).fire();
|
2013-01-03 22:05:00 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( cb.has(), true, "Callbacks list has callback function(s) after firing" );
|
2013-01-03 22:05:00 +00:00
|
|
|
|
|
|
|
cb.disable();
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( cb.has(), false, "disabled() list has no callback functions (returns false)" );
|
2015-08-16 06:59:58 +00:00
|
|
|
assert.strictEqual( cb.has( getA ), false, "Check for a specific function in a disabled() list" );
|
2013-01-03 22:05:00 +00:00
|
|
|
|
2015-08-16 06:59:58 +00:00
|
|
|
cb = jQuery.Callbacks( "unique" );
|
|
|
|
cb.add( getA );
|
|
|
|
cb.add( getA );
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( cb.has(), true, "Check if unique list has callback function(s) attached" );
|
2013-01-03 22:05:00 +00:00
|
|
|
cb.lock();
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.strictEqual( cb.has(), false, "locked() list is empty and returns false" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2013-01-03 22:05:00 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "jQuery.Callbacks() - adding a string doesn't cause a stack overflow", function( assert ) {
|
2012-08-16 17:12:59 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.expect( 1 );
|
2012-08-16 17:12:59 +00:00
|
|
|
|
|
|
|
jQuery.Callbacks().add( "hello world" );
|
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( true, "no stack overflow" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2014-12-08 01:51:04 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
QUnit.test( "jQuery.Callbacks() - disabled callback doesn't fire (gh-1790)", function( assert ) {
|
2014-12-08 01:51:04 +00:00
|
|
|
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.expect( 1 );
|
2014-12-08 01:51:04 +00:00
|
|
|
|
|
|
|
var cb = jQuery.Callbacks(),
|
|
|
|
fired = false,
|
2023-09-20 22:18:42 +00:00
|
|
|
shot = function() {
|
|
|
|
fired = true;
|
|
|
|
};
|
2014-12-08 01:51:04 +00:00
|
|
|
|
|
|
|
cb.disable();
|
|
|
|
cb.empty();
|
|
|
|
cb.add( shot );
|
|
|
|
cb.fire();
|
2015-08-16 03:45:28 +00:00
|
|
|
assert.ok( !fired, "Disabled callback function didn't fire" );
|
2015-08-16 06:59:58 +00:00
|
|
|
} );
|
2017-01-09 19:33:39 +00:00
|
|
|
|
|
|
|
QUnit.test( "jQuery.Callbacks() - list with memory stays locked (gh-3469)", function( assert ) {
|
|
|
|
|
|
|
|
assert.expect( 3 );
|
|
|
|
|
|
|
|
var cb = jQuery.Callbacks( "memory" ),
|
|
|
|
fired = 0,
|
2023-09-20 22:18:42 +00:00
|
|
|
count1 = function() {
|
|
|
|
fired += 1;
|
|
|
|
},
|
|
|
|
count2 = function() {
|
|
|
|
fired += 10;
|
|
|
|
};
|
2017-01-09 19:33:39 +00:00
|
|
|
|
|
|
|
cb.add( count1 );
|
|
|
|
cb.fire();
|
|
|
|
assert.equal( fired, 1, "Pre-lock() fire" );
|
|
|
|
|
|
|
|
cb.lock();
|
|
|
|
cb.add( count2 );
|
|
|
|
assert.equal( fired, 11, "Post-lock() add" );
|
|
|
|
|
|
|
|
cb.fire();
|
|
|
|
assert.equal( fired, 11, "Post-lock() fire ignored" );
|
|
|
|
} );
|
2020-01-27 17:54:47 +00:00
|
|
|
|
|
|
|
} )();
|