From 5826cafb95624321882ecfbe87073e507b77431d Mon Sep 17 00:00:00 2001 From: Oleg Gaidarenko Date: Tue, 22 Dec 2015 17:07:04 +0300 Subject: [PATCH] Revert "Deferred: Backwards-compatible standards interoperability" This reverts commit 34f25631795e0641cc706012cfe5a88dacdedfec. --- Gruntfile.js | 6 +- external/requirejs/require.js | 4 +- package.json | 4 +- src/deferred.js | 281 ++--------- test/index.html | 1 - test/unit/deferred.js | 887 ++++++++++------------------------ 6 files changed, 293 insertions(+), 890 deletions(-) diff --git a/Gruntfile.js b/Gruntfile.js index 474c42847..ad59f16df 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -80,8 +80,6 @@ module.exports = function( grunt ) { "sizzle/dist": "sizzle/dist", "sizzle/LICENSE.txt": "sizzle/LICENSE.txt", - "npo/npo.js": "native-promise-only/npo.js", - "qunit/qunit.js": "qunitjs/qunit/qunit.js", "qunit/qunit.css": "qunitjs/qunit/qunit.css", "qunit/LICENSE.txt": "qunitjs/LICENSE.txt", @@ -205,9 +203,7 @@ module.exports = function( grunt ) { // jQuery on Windows. (see gh-2519) grunt.registerTask( "test_fast", runJsdomTests ? [ "node_smoke_tests" ] : [] ); - grunt.registerTask( "test", [ "test_fast" ].concat( - runJsdomTests ? [ "promises_aplus_tests" ] : [] - ) ); + grunt.registerTask( "test", [ "test_fast" ] ); // Short list as a high frequency watch task grunt.registerTask( "dev", [ "build:*:*", "lint", "uglify", "remove_map_comment", "dist:*" ] ); diff --git a/external/requirejs/require.js b/external/requirejs/require.js index 77a5bb1d3..7f31fa204 100644 --- a/external/requirejs/require.js +++ b/external/requirejs/require.js @@ -1,5 +1,5 @@ /** vim: et:ts=4:sw=4:sts=4 - * @license RequireJS 2.1.15 Copyright (c) 2010-2014, The Dojo Foundation All Rights Reserved. + * @license RequireJS 2.1.14 Copyright (c) 2010-2014, The Dojo Foundation All Rights Reserved. * Available via the MIT or new BSD license. * see: http://github.com/jrburke/requirejs for details */ @@ -12,7 +12,7 @@ var requirejs, require, define; (function (global) { var req, s, head, baseElement, dataMain, src, interactiveScript, currentlyAddingScript, mainScript, subPath, - version = '2.1.15', + version = '2.1.14', commentRegExp = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg, cjsRequireRegExp = /[^.]\s*require\s*\(\s*["']([^'"\s]+)["']\s*\)/g, jsSuffixRegExp = /\.js$/, diff --git a/package.json b/package.json index 4cc7e59af..8db74a3e6 100644 --- a/package.json +++ b/package.json @@ -41,9 +41,7 @@ "gzip-js": "0.3.2", "jsdom": "5.6.1", "load-grunt-tasks": "1.0.0", - "native-promise-only": "0.7.8-a", - "promises-aplus-tests": "2.1.0", - "q": "1.1.2", + "npm": "2.1.12", "qunitjs": "1.17.1", "qunit-assert-step": "1.0.3", "requirejs": "2.1.17", diff --git a/src/deferred.js b/src/deferred.js index c4d61446b..51a40c7cb 100644 --- a/src/deferred.js +++ b/src/deferred.js @@ -4,26 +4,14 @@ define( [ "./callbacks" ], function( jQuery, slice ) { -function Identity( v ) { - return v; -} -function Thrower( ex ) { - throw ex; -} - -jQuery.extend( { +jQuery.extend({ Deferred: function( func ) { var tuples = [ - - // action, add listener, callbacks, - // ... .then handlers, argument index, [final state] - [ "notify", "progress", jQuery.Callbacks( "memory" ), - jQuery.Callbacks( "memory" ), 2 ], - [ "resolve", "done", jQuery.Callbacks( "once memory" ), - jQuery.Callbacks( "once memory" ), 0, "resolved" ], - [ "reject", "fail", jQuery.Callbacks( "once memory" ), - jQuery.Callbacks( "once memory" ), 1, "rejected" ] + // action, add listener, listener list, final state + [ "resolve", "done", jQuery.Callbacks("once memory"), "resolved" ], + [ "reject", "fail", jQuery.Callbacks("once memory"), "rejected" ], + [ "notify", "progress", jQuery.Callbacks("memory") ] ], state = "pending", promise = { @@ -34,21 +22,13 @@ jQuery.extend( { deferred.done( arguments ).fail( arguments ); return this; }, - - // Keep pipe for back-compat - pipe: function( /* fnDone, fnFail, fnProgress */ ) { + then: function( /* fnDone, fnFail, fnProgress */ ) { var fns = arguments; - - return jQuery.Deferred( function( newDefer ) { + return jQuery.Deferred(function( newDefer ) { jQuery.each( tuples, function( i, tuple ) { - - // Map tuples (progress, done, fail) to arguments (done, fail, progress) - var fn = jQuery.isFunction( fns[ tuple[ 4 ] ] ) && fns[ tuple[ 4 ] ]; - - // deferred.progress(function() { bind to newDefer or newDefer.notify }) - // deferred.done(function() { bind to newDefer or newDefer.resolve }) - // deferred.fail(function() { bind to newDefer or newDefer.reject }) - deferred[ tuple[ 1 ] ]( function() { + var fn = jQuery.isFunction( fns[ i ] ) && fns[ i ]; + // deferred[ done | fail | progress ] for forwarding actions to newDefer + deferred[ tuple[1] ](function() { var returned = fn && fn.apply( this, arguments ); if ( returned && jQuery.isFunction( returned.promise ) ) { returned.promise() @@ -66,162 +46,6 @@ jQuery.extend( { fns = null; } ).promise(); }, - then: function( onFulfilled, onRejected, onProgress ) { - var maxDepth = 0; - function resolve( depth, deferred, handler, special ) { - return function() { - var that = this === promise ? undefined : this, - args = arguments, - mightThrow = function() { - var returned, then; - - // Support: Promises/A+ section 2.3.3.3.3 - // https://promisesaplus.com/#point-59 - // Ignore double-resolution attempts - if ( depth < maxDepth ) { - return; - } - - returned = handler.apply( that, args ); - - // Support: Promises/A+ section 2.3.1 - // https://promisesaplus.com/#point-48 - if ( returned === deferred.promise() ) { - throw new TypeError( "Thenable self-resolution" ); - } - - // Support: Promises/A+ sections 2.3.3.1, 3.5 - // https://promisesaplus.com/#point-54 - // https://promisesaplus.com/#point-75 - // Retrieve `then` only once - then = returned && - - // Support: Promises/A+ section 2.3.4 - // https://promisesaplus.com/#point-64 - // Only check objects and functions for thenability - ( typeof returned === "object" || - typeof returned === "function" ) && - returned.then; - - // Handle a returned thenable - if ( jQuery.isFunction( then ) ) { - - // Special processors (notify) just wait for resolution - if ( special ) { - then.call( - returned, - resolve( maxDepth, deferred, Identity, special ), - resolve( maxDepth, deferred, Thrower, special ) - ); - - // Normal processors (resolve) also hook into progress - } else { - - // ...and disregard older resolution values - maxDepth++; - - then.call( - returned, - resolve( maxDepth, deferred, Identity, special ), - resolve( maxDepth, deferred, Thrower, special ), - resolve( maxDepth, deferred, Identity, - deferred.notify ) - ); - } - - // Handle all other returned values - } else { - - // Only substitue handlers pass on context - // and multiple values (non-spec behavior) - if ( handler !== Identity ) { - that = undefined; - args = [ returned ]; - } - - // Process the value(s) - // Default process is resolve - ( special || deferred.resolveWith )( - that || deferred.promise(), args ); - } - }, - - // Only normal processors (resolve) catch and reject exceptions - process = special ? - mightThrow : - function() { - try { - mightThrow(); - } catch ( e ) { - - // Support: Promises/A+ section 2.3.3.3.4.1 - // https://promisesaplus.com/#point-61 - // Ignore post-resolution exceptions - if ( depth + 1 >= maxDepth ) { - - // Only substitue handlers pass on context - // and multiple values (non-spec behavior) - if ( handler !== Thrower ) { - that = undefined; - args = [ e ]; - } - - deferred.rejectWith( that || deferred.promise(), - args ); - } - } - }; - - // Support: Promises/A+ section 2.3.3.3.1 - // https://promisesaplus.com/#point-57 - // Re-resolve promises immediately to dodge false rejection from - // subsequent errors - if ( depth ) { - process(); - } else { - window.setTimeout( process ); - } - }; - } - - return jQuery.Deferred( function( newDefer ) { - - // progress_handlers.add( ... ) - tuples[ 0 ][ 3 ].add( - resolve( - 0, - newDefer, - jQuery.isFunction( onProgress ) ? - onProgress : - Identity, - newDefer.notifyWith - ) - ); - - // fulfilled_handlers.add( ... ) - tuples[ 1 ][ 3 ].add( - resolve( - 0, - newDefer, - jQuery.isFunction( onFulfilled ) ? - onFulfilled : - Identity - ) - ); - - // rejected_handlers.add( ... ) - tuples[ 2 ][ 3 ].add( - resolve( - 0, - newDefer, - jQuery.isFunction( onRejected ) ? - onRejected : - Thrower - ) - ); - } ).promise(); - }, - // Get a promise for this deferred // If obj is provided, the promise aspect is added to the object promise: function( obj ) { @@ -230,53 +54,34 @@ jQuery.extend( { }, deferred = {}; + // Keep pipe for back-compat + promise.pipe = promise.then; + // Add list-specific methods jQuery.each( tuples, function( i, tuple ) { var list = tuple[ 2 ], - stateString = tuple[ 5 ]; + stateString = tuple[ 3 ]; - // promise.progress = list.add - // promise.done = list.add - // promise.fail = list.add - promise[ tuple[ 1 ] ] = list.add; + // promise[ done | fail | progress ] = list.add + promise[ tuple[1] ] = list.add; // Handle state if ( stateString ) { - list.add( - function() { - - // state = "resolved" (i.e., fulfilled) - // state = "rejected" - state = stateString; - }, + list.add(function() { + // state = [ resolved | rejected ] + state = stateString; - // rejected_callbacks.disable - // fulfilled_callbacks.disable - tuples[ 3 - i ][ 2 ].disable, - - // progress_callbacks.lock - tuples[ 0 ][ 2 ].lock - ); + // [ reject_list | resolve_list ].disable; progress_list.lock + }, tuples[ i ^ 1 ][ 2 ].disable, tuples[ 2 ][ 2 ].lock ); } - // progress_handlers.fire - // fulfilled_handlers.fire - // rejected_handlers.fire - list.add( tuple[ 3 ].fire ); - - // deferred.notify = function() { deferred.notifyWith(...) } - // deferred.resolve = function() { deferred.resolveWith(...) } - // deferred.reject = function() { deferred.rejectWith(...) } - deferred[ tuple[ 0 ] ] = function() { - deferred[ tuple[ 0 ] + "With" ]( this === deferred ? promise : this, arguments ); + // deferred[ resolve | reject | notify ] + deferred[ tuple[0] ] = function() { + deferred[ tuple[0] + "With" ]( this === deferred ? promise : this, arguments ); return this; }; - - // deferred.notifyWith = list.fireWith - // deferred.resolveWith = list.fireWith - // deferred.rejectWith = list.fireWith - deferred[ tuple[ 0 ] + "With" ] = list.fireWith; - } ); + deferred[ tuple[0] + "With" ] = list.fireWith; + }); // Make the deferred a promise promise.promise( deferred ); @@ -292,8 +97,7 @@ jQuery.extend( { // Deferred helper when: function( subordinate /* , ..., subordinateN */ ) { - var method, - i = 0, + var i = 0, resolveValues = slice.call( arguments ), length = resolveValues.length, @@ -303,7 +107,7 @@ jQuery.extend( { // the master Deferred. // If resolveValues consist of only a single Deferred, just use that. - master = remaining === 1 ? subordinate : jQuery.Deferred(), + deferred = remaining === 1 ? subordinate : jQuery.Deferred(), // Update function for both resolve and progress values updateFunc = function( i, contexts, values ) { @@ -311,12 +115,14 @@ jQuery.extend( { contexts[ i ] = this; values[ i ] = arguments.length > 1 ? slice.call( arguments ) : value; if ( values === progressValues ) { - master.notifyWith( contexts, values ); - } else if ( !( --remaining ) ) { - master.resolveWith( contexts, values ); + deferred.notifyWith( contexts, values ); + + } else if ( !(--remaining) ) { + deferred.resolveWith( contexts, values ); } }; }, + progressValues, progressContexts, resolveContexts; // add listeners to Deferred subordinates; treat others as resolved @@ -325,22 +131,11 @@ jQuery.extend( { progressContexts = new Array( length ); resolveContexts = new Array( length ); for ( ; i < length; i++ ) { - if ( resolveValues[ i ] && - jQuery.isFunction( ( method = resolveValues[ i ].promise ) ) ) { - - method.call( resolveValues[ i ] ) + if ( resolveValues[ i ] && jQuery.isFunction( resolveValues[ i ].promise ) ) { + resolveValues[ i ].promise() .progress( updateFunc( i, progressContexts, progressValues ) ) .done( updateFunc( i, resolveContexts, resolveValues ) ) - .fail( master.reject ); - } else if ( resolveValues[ i ] && - jQuery.isFunction( ( method = resolveValues[ i ].then ) ) ) { - - method.call( - resolveValues[ i ], - updateFunc( i, resolveContexts, resolveValues ), - master.reject, - updateFunc( i, progressContexts, progressValues ) - ); + .fail( deferred.reject ); } else { --remaining; } @@ -349,10 +144,10 @@ jQuery.extend( { // if we're not waiting on anything, resolve the master if ( !remaining ) { - master.resolveWith( resolveContexts, resolveValues ); + deferred.resolveWith( resolveContexts, resolveValues ); } - return master.promise(); + return deferred.promise(); } } ); diff --git a/test/index.html b/test/index.html index df1bdaa5b..b55e16725 100644 --- a/test/index.html +++ b/test/index.html @@ -16,7 +16,6 @@ - diff --git a/test/unit/deferred.js b/test/unit/deferred.js index 9541ca16d..40f936219 100644 --- a/test/unit/deferred.js +++ b/test/unit/deferred.js @@ -1,6 +1,6 @@ -QUnit.module( "deferred", { +module( "deferred", { teardown: moduleTeardown -} ); +}); jQuery.each( [ "", " - new operator" ], function( _, withNew ) { @@ -8,618 +8,318 @@ jQuery.each( [ "", " - new operator" ], function( _, withNew ) { return withNew ? new jQuery.Deferred( fn ) : jQuery.Deferred( fn ); } - QUnit.test( "jQuery.Deferred" + withNew, function( assert ) { + test( "jQuery.Deferred" + withNew, function() { - assert.expect( 23 ); + expect( 23 ); var defer = createDeferred(); - assert.ok( jQuery.isFunction( defer.pipe ), "defer.pipe is a function" ); - - createDeferred().resolve().done( function() { - assert.ok( true, "Success on resolve" ); - assert.strictEqual( this.state(), "resolved", "Deferred is resolved (state)" ); - } ).fail( function() { - assert.ok( false, "Error on resolve" ); - } ).always( function() { - assert.ok( true, "Always callback on resolve" ); - } ); - - createDeferred().reject().done( function() { - assert.ok( false, "Success on reject" ); - } ).fail( function() { - assert.ok( true, "Error on reject" ); - assert.strictEqual( this.state(), "rejected", "Deferred is rejected (state)" ); - } ).always( function() { - assert.ok( true, "Always callback on reject" ); - } ); - - createDeferred( function( defer ) { - assert.ok( this === defer, "Defer passed as this & first argument" ); - this.resolve( "done" ); - } ).done( function( value ) { - assert.strictEqual( value, "done", "Passed function executed" ); - } ); - - createDeferred( function( defer ) { + strictEqual( defer.pipe, defer.then, "pipe is an alias of then" ); + + createDeferred().resolve().done(function() { + ok( true, "Success on resolve" ); + strictEqual( this.state(), "resolved", "Deferred is resolved (state)" ); + }).fail(function() { + ok( false, "Error on resolve" ); + }).always(function() { + ok( true, "Always callback on resolve" ); + }); + + createDeferred().reject().done(function() { + ok( false, "Success on reject" ); + }).fail(function() { + ok( true, "Error on reject" ); + strictEqual( this.state(), "rejected", "Deferred is rejected (state)" ); + }).always(function() { + ok( true, "Always callback on reject" ); + }); + + createDeferred(function( defer ) { + ok( this === defer, "Defer passed as this & first argument" ); + this.resolve("done"); + }).done(function( value ) { + strictEqual( value, "done", "Passed function executed" ); + }); + + createDeferred(function( defer ) { var promise = defer.promise(), func = function() {}, funcPromise = defer.promise( func ); - assert.strictEqual( defer.promise(), promise, "promise is always the same" ); - assert.strictEqual( funcPromise, func, "non objects get extended" ); + strictEqual( defer.promise(), promise, "promise is always the same" ); + strictEqual( funcPromise, func, "non objects get extended" ); jQuery.each( promise, function( key ) { if ( !jQuery.isFunction( promise[ key ] ) ) { - assert.ok( false, key + " is a function (" + jQuery.type( promise[ key ] ) + ")" ); + ok( false, key + " is a function (" + jQuery.type( promise[ key ] ) + ")" ); } if ( promise[ key ] !== func[ key ] ) { - assert.strictEqual( func[ key ], promise[ key ], key + " is the same" ); + strictEqual( func[ key ], promise[ key ], key + " is the same" ); } - } ); - } ); + }); + }); jQuery.expandedEach = jQuery.each; - jQuery.expandedEach( "resolve reject".split( " " ), function( _, change ) { - createDeferred( function( defer ) { - assert.strictEqual( defer.state(), "pending", "pending after creation" ); + jQuery.expandedEach( "resolve reject".split(" "), function( _, change ) { + createDeferred(function( defer ) { + strictEqual( defer.state(), "pending", "pending after creation" ); var checked = 0; - defer.progress( function( value ) { - assert.strictEqual( value, checked, "Progress: right value (" + value + ") received" ); - } ); + defer.progress(function( value ) { + strictEqual( value, checked, "Progress: right value (" + value + ") received" ); + }); for ( checked = 0; checked < 3; checked++ ) { defer.notify( checked ); } - assert.strictEqual( defer.state(), "pending", "pending after notification" ); + strictEqual( defer.state(), "pending", "pending after notification" ); defer[ change ](); - assert.notStrictEqual( defer.state(), "pending", "not pending after " + change ); + notStrictEqual( defer.state(), "pending", "not pending after " + change ); defer.notify(); - } ); - } ); - } ); -} ); + }); + }); + }); +}); -QUnit.test( "jQuery.Deferred - chainability", function( assert ) { + +test( "jQuery.Deferred - chainability", function() { var defer = jQuery.Deferred(); - assert.expect( 10 ); + expect( 10 ); jQuery.expandedEach = jQuery.each; - jQuery.expandedEach( "resolve reject notify resolveWith rejectWith notifyWith done fail progress always".split( " " ), function( _, method ) { + jQuery.expandedEach( "resolve reject notify resolveWith rejectWith notifyWith done fail progress always".split(" "), function( _, method ) { var object = { m: defer[ method ] }; - assert.strictEqual( object.m(), object, method + " is chainable" ); - } ); -} ); + strictEqual( object.m(), object, method + " is chainable" ); + }); +}); -QUnit.test( "jQuery.Deferred.then - filtering (done)", function( assert ) { +test( "jQuery.Deferred.then - filtering (done)", function() { - assert.expect( 4 ); + expect( 4 ); var value1, value2, value3, defer = jQuery.Deferred(), - piped = defer.then( function( a, b ) { + piped = defer.then(function( a, b ) { return a * b; - } ), - done = jQuery.map( new Array( 3 ), function() { return assert.async(); } ); + }); - piped.done( function( result ) { + piped.done(function( result ) { value3 = result; - } ); + }); - defer.done( function( a, b ) { + defer.done(function( a, b ) { value1 = a; value2 = b; - } ); + }); + + defer.resolve( 2, 3 ); - defer.resolve( 2, 3 ).then( function() { - assert.strictEqual( value1, 2, "first resolve value ok" ); - assert.strictEqual( value2, 3, "second resolve value ok" ); - assert.strictEqual( value3, 6, "result of filter ok" ); - done.pop().call(); - } ); + strictEqual( value1, 2, "first resolve value ok" ); + strictEqual( value2, 3, "second resolve value ok" ); + strictEqual( value3, 6, "result of filter ok" ); - jQuery.Deferred().reject().then( function() { - assert.ok( false, "then should not be called on reject" ); - } ).then( null, done.pop() ); + jQuery.Deferred().reject().then(function() { + ok( false, "then should not be called on reject" ); + }); - jQuery.Deferred().resolve().then( jQuery.noop ).done( function( value ) { - assert.strictEqual( value, undefined, "then done callback can return undefined/null" ); - done.pop().call(); - } ); -} ); + jQuery.Deferred().resolve().then( jQuery.noop ).done(function( value ) { + strictEqual( value, undefined, "then done callback can return undefined/null" ); + }); +}); -QUnit.test( "jQuery.Deferred.then - filtering (fail)", function( assert ) { +test( "jQuery.Deferred.then - filtering (fail)", function() { - assert.expect( 4 ); + expect( 4 ); var value1, value2, value3, defer = jQuery.Deferred(), piped = defer.then( null, function( a, b ) { return a * b; - } ), - done = jQuery.map( new Array( 3 ), function() { return assert.async(); } ); + }); - piped.done( function( result ) { + piped.fail(function( result ) { value3 = result; - } ); + }); - defer.fail( function( a, b ) { + defer.fail(function( a, b ) { value1 = a; value2 = b; - } ); - - defer.reject( 2, 3 ).then( null, function() { - assert.strictEqual( value1, 2, "first reject value ok" ); - assert.strictEqual( value2, 3, "second reject value ok" ); - assert.strictEqual( value3, 6, "result of filter ok" ); - done.pop().call(); - } ); - - jQuery.Deferred().resolve().then( null, function() { - assert.ok( false, "then should not be called on resolve" ); - } ).then( done.pop() ); - - jQuery.Deferred().reject().then( null, jQuery.noop ).done( function( value ) { - assert.strictEqual( value, undefined, "then fail callback can return undefined/null" ); - done.pop().call(); - } ); -} ); - -QUnit.test( "[PIPE ONLY] jQuery.Deferred.pipe - filtering (fail)", function( assert ) { - assert.expect( 4 ); + }); - var value1, value2, value3, - defer = jQuery.Deferred(), - piped = defer.pipe( null, function( a, b ) { - return a * b; - } ), - done = jQuery.map( new Array( 3 ), function() { return assert.async(); } ); - - piped.fail( function( result ) { - value3 = result; - } ); - - defer.fail( function( a, b ) { - value1 = a; - value2 = b; - } ); + defer.reject( 2, 3 ); - defer.reject( 2, 3 ).pipe( null, function() { - assert.strictEqual( value1, 2, "first reject value ok" ); - assert.strictEqual( value2, 3, "second reject value ok" ); - assert.strictEqual( value3, 6, "result of filter ok" ); - done.pop().call(); - } ); + strictEqual( value1, 2, "first reject value ok" ); + strictEqual( value2, 3, "second reject value ok" ); + strictEqual( value3, 6, "result of filter ok" ); - jQuery.Deferred().resolve().pipe( null, function() { - assert.ok( false, "then should not be called on resolve" ); - } ).then( done.pop() ); + jQuery.Deferred().resolve().then( null, function() { + ok( false, "then should not be called on resolve" ); + }); - jQuery.Deferred().reject().pipe( null, jQuery.noop ).fail( function( value ) { - assert.strictEqual( value, undefined, "then fail callback can return undefined/null" ); - done.pop().call(); - } ); -} ); + jQuery.Deferred().reject().then( null, jQuery.noop ).fail(function( value ) { + strictEqual( value, undefined, "then fail callback can return undefined/null" ); + }); +}); -QUnit.test( "jQuery.Deferred.then - filtering (progress)", function( assert ) { +test( "jQuery.Deferred.then - filtering (progress)", function() { - assert.expect( 3 ); + expect( 3 ); var value1, value2, value3, defer = jQuery.Deferred(), piped = defer.then( null, null, function( a, b ) { return a * b; - } ), - done = assert.async(); + }); - piped.progress( function( result ) { + piped.progress(function( result ) { value3 = result; - } ); + }); - defer.progress( function( a, b ) { + defer.progress(function( a, b ) { value1 = a; value2 = b; - } ); + }); - defer.notify( 2, 3 ).then( null, null, function() { - assert.strictEqual( value1, 2, "first progress value ok" ); - assert.strictEqual( value2, 3, "second progress value ok" ); - assert.strictEqual( value3, 6, "result of filter ok" ); - done(); - } ); -} ); + defer.notify( 2, 3 ); -QUnit.test( "jQuery.Deferred.then - deferred (done)", function( assert ) { + strictEqual( value1, 2, "first progress value ok" ); + strictEqual( value2, 3, "second progress value ok" ); + strictEqual( value3, 6, "result of filter ok" ); +}); - assert.expect( 3 ); +test( "jQuery.Deferred.then - deferred (done)", function() { + + expect( 3 ); var value1, value2, value3, defer = jQuery.Deferred(), - piped = defer.then( function( a, b ) { - return jQuery.Deferred( function( defer ) { + piped = defer.then(function( a, b ) { + return jQuery.Deferred(function( defer ) { defer.reject( a * b ); - } ); - } ), - done = assert.async(); + }); + }); - piped.fail( function( result ) { + piped.fail(function( result ) { value3 = result; - } ); + }); - defer.done( function( a, b ) { + defer.done(function( a, b ) { value1 = a; value2 = b; - } ); + }); defer.resolve( 2, 3 ); - piped.fail( function() { - assert.strictEqual( value1, 2, "first resolve value ok" ); - assert.strictEqual( value2, 3, "second resolve value ok" ); - assert.strictEqual( value3, 6, "result of filter ok" ); - done(); - } ); -} ); + strictEqual( value1, 2, "first resolve value ok" ); + strictEqual( value2, 3, "second resolve value ok" ); + strictEqual( value3, 6, "result of filter ok" ); +}); -QUnit.test( "jQuery.Deferred.then - deferred (fail)", function( assert ) { +test( "jQuery.Deferred.then - deferred (fail)", function() { - assert.expect( 3 ); + expect( 3 ); var value1, value2, value3, defer = jQuery.Deferred(), piped = defer.then( null, function( a, b ) { - return jQuery.Deferred( function( defer ) { + return jQuery.Deferred(function( defer ) { defer.resolve( a * b ); - } ); - } ), - done = assert.async(); + }); + }); - piped.done( function( result ) { + piped.done(function( result ) { value3 = result; - } ); + }); - defer.fail( function( a, b ) { + defer.fail(function( a, b ) { value1 = a; value2 = b; - } ); + }); defer.reject( 2, 3 ); - piped.done( function() { - assert.strictEqual( value1, 2, "first reject value ok" ); - assert.strictEqual( value2, 3, "second reject value ok" ); - assert.strictEqual( value3, 6, "result of filter ok" ); - done(); - } ); -} ); + strictEqual( value1, 2, "first reject value ok" ); + strictEqual( value2, 3, "second reject value ok" ); + strictEqual( value3, 6, "result of filter ok" ); +}); -QUnit.test( "jQuery.Deferred.then - deferred (progress)", function( assert ) { +test( "jQuery.Deferred.then - deferred (progress)", function() { - assert.expect( 3 ); + expect( 3 ); var value1, value2, value3, defer = jQuery.Deferred(), piped = defer.then( null, null, function( a, b ) { - return jQuery.Deferred( function( defer ) { - defer.resolve( a * b ); - } ); - } ), - done = assert.async(); - - piped.progress( function( result ) { - return jQuery.Deferred().resolve().then( function() { - return result; - } ).then( function( result ) { - value3 = result; - } ); - } ); - - defer.progress( function( a, b ) { - value1 = a; - value2 = b; - } ); - - defer.notify( 2, 3 ); - - piped.then( null, null, function( result ) { - return jQuery.Deferred().resolve().then( function() { - return result; - } ).then( function() { - assert.strictEqual( value1, 2, "first progress value ok" ); - assert.strictEqual( value2, 3, "second progress value ok" ); - assert.strictEqual( value3, 6, "result of filter ok" ); - done(); - } ); - } ); -} ); - -QUnit.test( "[PIPE ONLY] jQuery.Deferred.pipe - deferred (progress)", function( assert ) { - - assert.expect( 3 ); - - var value1, value2, value3, - defer = jQuery.Deferred(), - piped = defer.pipe( null, null, function( a, b ) { - return jQuery.Deferred( function( defer ) { + return jQuery.Deferred(function( defer ) { defer.resolve( a * b ); - } ); - } ), - done = assert.async(); + }); + }); - piped.done( function( result ) { + piped.done(function( result ) { value3 = result; - } ); + }); - defer.progress( function( a, b ) { + defer.progress(function( a, b ) { value1 = a; value2 = b; - } ); + }); defer.notify( 2, 3 ); - piped.done( function() { - assert.strictEqual( value1, 2, "first progress value ok" ); - assert.strictEqual( value2, 3, "second progress value ok" ); - assert.strictEqual( value3, 6, "result of filter ok" ); - done(); - } ); -} ); + strictEqual( value1, 2, "first progress value ok" ); + strictEqual( value2, 3, "second progress value ok" ); + strictEqual( value3, 6, "result of filter ok" ); +}); -QUnit.test( "jQuery.Deferred.then - context", function( assert ) { +test( "jQuery.Deferred.then - context", function() { - assert.expect( 7 ); + expect( 7 ); var defer, piped, defer2, piped2, - context = {}, - done = jQuery.map( new Array( 4 ), function() { return assert.async(); } ); + context = {}; - jQuery.Deferred().resolveWith( context, [ 2 ] ).then( function( value ) { + jQuery.Deferred().resolveWith( context, [ 2 ] ).then(function( value ) { return value * 3; - } ).done( function( value ) { - assert.notStrictEqual( this, context, "custom context not propagated through .then" ); - assert.strictEqual( value, 6, "proper value received" ); - done.pop().call(); - } ); - - jQuery.Deferred().resolve().then( function() { - return jQuery.Deferred().resolveWith( context ); - } ).done( function() { - assert.strictEqual( this, context, - "custom context of returned deferred correctly propagated" ); - done.pop().call(); - } ); + }).done(function( value ) { + strictEqual( this, context, "custom context correctly propagated" ); + strictEqual( value, 6, "proper value received" ); + }); + + jQuery.Deferred().resolve().then(function() { + return jQuery.Deferred().resolveWith(context); + }).done(function() { + strictEqual( this, context, "custom context of returned deferred correctly propagated" ); + }); defer = jQuery.Deferred(); - piped = defer.then( function( value ) { + piped = defer.then(function( value ) { return value * 3; - } ); + }); defer.resolve( 2 ); - piped.done( function( value ) { - assert.strictEqual( this, piped, - "default context gets updated to latest promise in the chain" ); - assert.strictEqual( value, 6, "proper value received" ); - done.pop().call(); - } ); + piped.done(function( value ) { + strictEqual( this, piped, "default context gets updated to latest promise in the chain" ); + strictEqual( value, 6, "proper value received" ); + }); defer2 = jQuery.Deferred(); piped2 = defer2.then(); defer2.resolve( 2 ); - piped2.done( function( value ) { - assert.strictEqual( this, piped2, - "default context updated to latest promise in the chain (without passing function)" ); - assert.strictEqual( value, 2, "proper value received (without passing function)" ); - done.pop().call(); - } ); -} ); + piped2.done(function( value ) { + strictEqual( this, piped2, "default context gets updated to latest promise in the chain (without passing function)" ); + strictEqual( value, 2, "proper value received (without passing function)" ); + }); +}); -QUnit.test( "[PIPE ONLY] jQuery.Deferred.pipe - context", function( assert ) { +test( "jQuery.when", function() { - assert.expect( 7 ); - - var defer, piped, defer2, piped2, - context = {}, - done = jQuery.map( new Array( 4 ), function() { return assert.async(); } ); - - jQuery.Deferred().resolveWith( context, [ 2 ] ).pipe( function( value ) { - return value * 3; - } ).done( function( value ) { - assert.strictEqual( this, context, "[PIPE ONLY] custom context correctly propagated" ); - assert.strictEqual( value, 6, "proper value received" ); - done.pop().call(); - } ); - - jQuery.Deferred().resolve().pipe( function() { - return jQuery.Deferred().resolveWith( context ); - } ).done( function() { - assert.strictEqual( this, context, - "custom context of returned deferred correctly propagated" ); - done.pop().call(); - } ); - - defer = jQuery.Deferred(); - piped = defer.pipe( function( value ) { - return value * 3; - } ); - - defer.resolve( 2 ); - - piped.done( function( value ) { - assert.strictEqual( this, piped, - "default context gets updated to latest promise in the chain" ); - assert.strictEqual( value, 6, "proper value received" ); - done.pop().call(); - } ); - - defer2 = jQuery.Deferred(); - piped2 = defer2.pipe(); - - defer2.resolve( 2 ); - - piped2.done( function( value ) { - assert.strictEqual( this, piped2, - "default context updated to latest promise in the chain (without passing function)" ); - assert.strictEqual( value, 2, "proper value received (without passing function)" ); - done.pop().call(); - } ); -} ); - -QUnit.test( "jQuery.Deferred.then - spec compatibility", function( assert ) { - - assert.expect( 1 ); - - var done = assert.async(); - - var defer = jQuery.Deferred().done( function() { - setTimeout( done ); - throw new Error(); - } ); - - defer.then( function() { - assert.ok( true, "errors in .done callbacks don't stop .then handlers" ); - } ); - - try { - defer.resolve(); - } catch ( _ ) {} -} ); - -QUnit.test( "jQuery.Deferred - 1.x/2.x compatibility", function( assert ) { - - assert.expect( 8 ); - - var context = { id: "callback context" }, - thenable = jQuery.Deferred().resolve( "thenable fulfillment" ).promise(), - done = jQuery.map( new Array( 8 ), function() { return assert.async(); } ); - - thenable.unwrapped = false; - - jQuery.Deferred().resolve( 1, 2 ).then( function() { - assert.deepEqual( [].slice.call( arguments ), [ 1, 2 ], - ".then fulfillment callbacks receive all resolution values" ); - done.pop().call(); - } ); - jQuery.Deferred().reject( 1, 2 ).then( null, function() { - assert.deepEqual( [].slice.call( arguments ), [ 1, 2 ], - ".then rejection callbacks receive all rejection values" ); - done.pop().call(); - } ); - jQuery.Deferred().notify( 1, 2 ).then( null, null, function() { - assert.deepEqual( [].slice.call( arguments ), [ 1, 2 ], - ".then progress callbacks receive all progress values" ); - done.pop().call(); - } ); - - jQuery.Deferred().resolveWith( context ).then( function() { - assert.deepEqual( this, context, ".then fulfillment callbacks receive context" ); - done.pop().call(); - } ); - jQuery.Deferred().rejectWith( context ).then( null, function() { - assert.deepEqual( this, context, ".then rejection callbacks receive context" ); - done.pop().call(); - } ); - jQuery.Deferred().notifyWith( context ).then( null, null, function() { - assert.deepEqual( this, context, ".then progress callbacks receive context" ); - done.pop().call(); - } ); - - jQuery.Deferred().resolve( thenable ).done( function( value ) { - assert.strictEqual( value, thenable, ".done doesn't unwrap thenables" ); - done.pop().call(); - } ); - - jQuery.Deferred().notify( thenable ).then().then( null, null, function( value ) { - assert.strictEqual( value, "thenable fulfillment", - ".then implicit progress callbacks unwrap thenables" ); - done.pop().call(); - } ); -} ); - -QUnit.test( "jQuery.Deferred.then - progress and thenables", function( assert ) { - - assert.expect( 2 ); - - var trigger = jQuery.Deferred().notify(), - expectedProgress = [ "baz", "baz" ], - done = jQuery.map( new Array( 2 ), function() { return assert.async(); } ), - failer = function( evt ) { - return function() { - assert.ok( false, "no unexpected " + evt ); - }; - }; - - trigger.then( null, null, function() { - var notifier = jQuery.Deferred().notify( "foo" ); - setTimeout( function() { - notifier.notify( "bar" ).resolve( "baz" ); - } ); - return notifier; - } ).then( failer( "fulfill" ), failer( "reject" ), function( v ) { - assert.strictEqual( v, expectedProgress.shift(), "expected progress value" ); - done.pop().call(); - } ); - trigger.notify(); -} ); - -QUnit.test( "jQuery.Deferred - notify and resolve", function( assert ) { - - assert.expect( 7 ); - - var notifiedResolved = jQuery.Deferred().notify( "foo" )/*xxx .resolve( "bar" )*/, - done = jQuery.map( new Array( 3 ), function() { return assert.async(); } ); - - notifiedResolved.progress( function( v ) { - assert.strictEqual( v, "foo", "progress value" ); - } ); - - notifiedResolved.pipe().progress( function( v ) { - assert.strictEqual( v, "foo", "piped progress value" ); - } ); - - notifiedResolved.pipe( null, null, function() { - return "baz"; - } ).progress( function( v ) { - assert.strictEqual( v, "baz", "replaced piped progress value" ); - } ); - - notifiedResolved.pipe( null, null, function() { - return jQuery.Deferred().notify( "baz" ).resolve( "quux" ); - } ).progress( function( v ) { - assert.strictEqual( v, "baz", "deferred replaced piped progress value" ); - } ); - - notifiedResolved.then().progress( function( v ) { - assert.strictEqual( v, "foo", "then'd progress value" ); - done.pop().call(); - } ); - - notifiedResolved.then( null, null, function() { - return "baz"; - } ).progress( function( v ) { - assert.strictEqual( v, "baz", "replaced then'd progress value" ); - done.pop().call(); - } ); - - notifiedResolved.then( null, null, function() { - return jQuery.Deferred().notify( "baz" ).resolve( "quux" ); - } ).progress( function( v ) { - - // Progress from the surrogate deferred is ignored - assert.strictEqual( v, "quux", "deferred replaced then'd progress value" ); - done.pop().call(); - } ); -} ); - -QUnit.test( "jQuery.when", function( assert ) { - - assert.expect( 37 ); + expect( 37 ); // Some other objects - jQuery.each( { + jQuery.each({ "an empty string": "", "a non-empty string": "some string", "zero": 0, @@ -632,23 +332,23 @@ QUnit.test( "jQuery.when", function( assert ) { "an array": [ 1, 2, 3 ] }, function( message, value ) { - assert.ok( + ok( jQuery.isFunction( - jQuery.when( value ).done( function( resolveValue ) { - assert.strictEqual( this, window, "Context is the global object with " + message ); - assert.strictEqual( resolveValue, value, "Test the promise was resolved with " + message ); - } ).promise + jQuery.when( value ).done(function( resolveValue ) { + strictEqual( this, window, "Context is the global object with " + message ); + strictEqual( resolveValue, value, "Test the promise was resolved with " + message ); + }).promise ), "Test " + message + " triggers the creation of a new Promise" ); - } ); + }); - assert.ok( + ok( jQuery.isFunction( - jQuery.when().done( function( resolveValue ) { - assert.strictEqual( this, window, "Test the promise was resolved with window as its context" ); - assert.strictEqual( resolveValue, undefined, "Test the promise was resolved with no parameter" ); - } ).promise + jQuery.when().done(function( resolveValue ) { + strictEqual( this, window, "Test the promise was resolved with window as its context" ); + strictEqual( resolveValue, undefined, "Test the promise was resolved with no parameter" ); + }).promise ), "Test calling when with no parameter triggers the creation of a new Promise" ); @@ -656,63 +356,50 @@ QUnit.test( "jQuery.when", function( assert ) { var cache, context = {}; - jQuery.when( jQuery.Deferred().resolveWith( context ) ).done( function() { - assert.strictEqual( this, context, "when( promise ) propagates context" ); - } ); + jQuery.when( jQuery.Deferred().resolveWith( context ) ).done(function() { + strictEqual( this, context, "when( promise ) propagates context" ); + }); - jQuery.each( [ 1, 2, 3 ], function( k, i ) { + jQuery.each([ 1, 2, 3 ], function( k, i ) { - jQuery.when( cache || jQuery.Deferred( function() { + jQuery.when( cache || jQuery.Deferred(function() { this.resolve( i ); - } ) - ).done( function( value ) { + }) + ).done(function( value ) { - assert.strictEqual( value, 1, "Function executed" + ( i > 1 ? " only once" : "" ) ); + strictEqual( value, 1, "Function executed" + ( i > 1 ? " only once" : "" ) ); cache = value; - } ); + }); - } ); -} ); + }); +}); -QUnit.test( "jQuery.when - joined", function( assert ) { +test( "jQuery.when - joined", function() { - assert.expect( 195 ); + expect( 119 ); var deferreds = { - rawValue: 1, - fulfilled: jQuery.Deferred().resolve( 1 ), - rejected: jQuery.Deferred().reject( 0 ), - notified: jQuery.Deferred().notify( true ), - eventuallyFulfilled: jQuery.Deferred().notify( true ), - eventuallyRejected: jQuery.Deferred().notify( true ), - fulfilledStandardPromise: Promise.resolve( 1 ), - rejectedStandardPromise: Promise.reject( 0 ) + value: 1, + success: jQuery.Deferred().resolve( 1 ), + error: jQuery.Deferred().reject( 0 ), + futureSuccess: jQuery.Deferred().notify( true ), + futureError: jQuery.Deferred().notify( true ), + notify: jQuery.Deferred().notify( true ) }, willSucceed = { - rawValue: true, - fulfilled: true, - eventuallyFulfilled: true, - fulfilledStandardPromise: true + value: true, + success: true, + futureSuccess: true }, willError = { - rejected: true, - eventuallyRejected: true, - rejectedStandardPromise: true + error: true, + futureError: true }, willNotify = { - notified: true, - eventuallyFulfilled: true, - eventuallyRejected: true - }, - counter = 49; - - QUnit.stop(); - - function restart() { - if ( !--counter ) { - QUnit.start(); - } - } + futureSuccess: true, + futureError: true, + notify: true + }; jQuery.each( deferreds, function( id1, defer1 ) { jQuery.each( deferreds, function( id2, defer2 ) { @@ -721,125 +408,53 @@ QUnit.test( "jQuery.when - joined", function( assert ) { shouldNotify = willNotify[ id1 ] || willNotify[ id2 ], expected = shouldResolve ? [ 1, 1 ] : [ 0, undefined ], expectedNotify = shouldNotify && [ willNotify[ id1 ], willNotify[ id2 ] ], - code = "jQuery.when( " + id1 + ", " + id2 + " )", - context1 = defer1 && jQuery.isFunction( defer1.promise ) ? defer1.promise() : - ( defer1.then ? window : undefined ), - context2 = defer2 && jQuery.isFunction( defer2.promise ) ? defer2.promise() : - ( defer2.then ? window : undefined ); + code = id1 + "/" + id2, + context1 = defer1 && jQuery.isFunction( defer1.promise ) ? defer1.promise() : undefined, + context2 = defer2 && jQuery.isFunction( defer2.promise ) ? defer2.promise() : undefined; - jQuery.when( defer1, defer2 ).done( function( a, b ) { + jQuery.when( defer1, defer2 ).done(function( a, b ) { if ( shouldResolve ) { - assert.deepEqual( [ a, b ], expected, code + " => resolve" ); - assert.strictEqual( this[ 0 ], context1, code + " => first context OK" ); - assert.strictEqual( this[ 1 ], context2, code + " => second context OK" ); + deepEqual( [ a, b ], expected, code + " => resolve" ); + strictEqual( this[ 0 ], context1, code + " => first context OK" ); + strictEqual( this[ 1 ], context2, code + " => second context OK" ); } else { - assert.ok( false, code + " => resolve" ); + ok( false, code + " => resolve" ); } - } ).fail( function( a, b ) { + }).fail(function( a, b ) { if ( shouldError ) { - assert.deepEqual( [ a, b ], expected, code + " => reject" ); + deepEqual( [ a, b ], expected, code + " => reject" ); } else { - assert.ok( false, code + " => reject" ); + ok( false, code + " => reject" ); } - } ).progress( function( a, b ) { - assert.deepEqual( [ a, b ], expectedNotify, code + " => progress" ); - assert.strictEqual( this[ 0 ], expectedNotify[ 0 ] ? context1 : undefined, code + " => first context OK" ); - assert.strictEqual( this[ 1 ], expectedNotify[ 1 ] ? context2 : undefined, code + " => second context OK" ); - } ).always( restart ); - } ); - } ); - deferreds.eventuallyFulfilled.resolve( 1 ); - deferreds.eventuallyRejected.reject( 0 ); -} ); + }).progress(function( a, b ) { + deepEqual( [ a, b ], expectedNotify, code + " => progress" ); + strictEqual( this[ 0 ], expectedNotify[ 0 ] ? context1 : undefined, code + " => first context OK" ); + strictEqual( this[ 1 ], expectedNotify[ 1 ] ? context2 : undefined, code + " => second context OK" ); + }); + }); + }); + deferreds.futureSuccess.resolve( 1 ); + deferreds.futureError.reject( 0 ); +}); -QUnit.test( "jQuery.when - resolved", function( assert ) { +test( "jQuery.when - resolved", function() { - assert.expect( 6 ); + expect( 6 ); var a = jQuery.Deferred().notify( 1 ).resolve( 4 ), b = jQuery.Deferred().notify( 2 ).resolve( 5 ), c = jQuery.Deferred().notify( 3 ).resolve( 6 ); - jQuery.when( a, b, c ).progress( function( a, b, c ) { - assert.strictEqual( a, 1, "first notify value ok" ); - assert.strictEqual( b, 2, "second notify value ok" ); - assert.strictEqual( c, 3, "third notify value ok" ); - } ).done( function( a, b, c ) { - assert.strictEqual( a, 4, "first resolve value ok" ); - assert.strictEqual( b, 5, "second resolve value ok" ); - assert.strictEqual( c, 6, "third resolve value ok" ); - } ).fail( function() { - assert.ok( false, "Error on resolve" ); - } ); -} ); - -QUnit.test( "jQuery.when - filtering", function( assert ) { - - assert.expect( 2 ); - - function increment( x ) { - return x + 1; - } - - QUnit.stop(); - - jQuery.when( - jQuery.Deferred().resolve( 3 ).then( increment ), - jQuery.Deferred().reject( 5 ).then( null, increment ) - ).done( function( four, six ) { - assert.strictEqual( four, 4, "resolved value incremented" ); - assert.strictEqual( six, 6, "rejected value incremented" ); - QUnit.start(); - } ); -} ); - -QUnit.test( "jQuery.when - exceptions", function( assert ) { - - assert.expect( 2 ); - - function woops() { - throw "exception thrown"; - } - - QUnit.stop(); - - jQuery.Deferred().resolve().then( woops ).fail( function( doneException ) { - assert.strictEqual( doneException, "exception thrown", "throwing in done handler" ); - jQuery.Deferred().reject().then( null, woops ).fail( function( failException ) { - assert.strictEqual( failException, "exception thrown", "throwing in fail handler" ); - QUnit.start(); - } ); - } ); -} ); - -QUnit.test( "jQuery.when - chaining", function( assert ) { - - assert.expect( 4 ); - - var defer = jQuery.Deferred(); - - function chain() { - return defer; - } - - function chainStandard() { - return Promise.resolve( "std deferred" ); - } - - QUnit.stop(); - - jQuery.when( - jQuery.Deferred().resolve( 3 ).then( chain ), - jQuery.Deferred().reject( 5 ).then( null, chain ), - jQuery.Deferred().resolve( 3 ).then( chainStandard ), - jQuery.Deferred().reject( 5 ).then( null, chainStandard ) - ).done( function( v1, v2, s1, s2 ) { - assert.strictEqual( v1, "other deferred", "chaining in done handler" ); - assert.strictEqual( v2, "other deferred", "chaining in fail handler" ); - assert.strictEqual( s1, "std deferred", "chaining thenable in done handler" ); - assert.strictEqual( s2, "std deferred", "chaining thenable in fail handler" ); - QUnit.start(); - } ); - - defer.resolve( "other deferred" ); -} ); + jQuery.when( a, b, c ).progress(function( a, b, c ) { + strictEqual( a, 1, "first notify value ok" ); + strictEqual( b, 2, "second notify value ok" ); + strictEqual( c, 3, "third notify value ok" ); + }).done(function( a, b, c ) { + strictEqual( a, 4, "first resolve value ok" ); + strictEqual( b, 5, "second resolve value ok" ); + strictEqual( c, 6, "third resolve value ok" ); + }).fail(function() { + ok( false, "Error on resolve" ); + }); + +}); -- 2.39.5