diff --git a/js/utils.js b/js/utils.js index d8fe2a5..51f7fd4 100644 --- a/js/utils.js +++ b/js/utils.js @@ -299,10 +299,17 @@ // If we didn’t have an embedded getModel, fetch the getModel data. if ( ! getModel.get( embedCheckField ) ) { - getModel.fetch( { success: function( getModel ) { - deferred.resolve( getModel ); - } } ); + getModel.fetch( { + success: function( getModel ) { + deferred.resolve( getModel ); + }, + error: function( getModel, response ) { + deferred.reject( response ); + } + } ); } else { + + // Resolve with the embedded model. deferred.resolve( getModel ); } @@ -368,12 +375,17 @@ // If we didn’t have embedded getObjects, fetch the getObjects data. if ( _.isUndefined( getObjects.models[0] ) ) { - getObjects.fetch( { success: function( getObjects ) { - - // Add a helper 'parent_post' attribute onto the model. - setHelperParentPost( getObjects, postId ); - deferred.resolve( getObjects ); - } } ); + getObjects.fetch( { + success: function( getObjects ) { + + // Add a helper 'parent_post' attribute onto the model. + setHelperParentPost( getObjects, postId ); + deferred.resolve( getObjects ); + }, + error: function( getModel, response ) { + deferred.reject( response ); + } + } ); } else { // Add a helper 'parent_post' attribute onto the model. diff --git a/tests/wp-api.js b/tests/wp-api.js index 08f0e8d..b3e98c0 100644 --- a/tests/wp-api.js +++ b/tests/wp-api.js @@ -54,6 +54,7 @@ var collectionHelperTests = [ } ]; +// Check that we have and can get each collection type. _.each( collectionClassNames, function( className ) { QUnit.test( 'Testing ' + className + ' collection.', function( assert ) { var done = assert.async(); @@ -116,7 +117,7 @@ var modelsWithIdsClassNames = 'User' ]; - +// Check that we have and can get each model type. _.each( modelsWithIdsClassNames, function( className ) { QUnit.test( 'Checking ' + className + ' model.' , function( assert ) { @@ -148,6 +149,7 @@ var modelsWithIndexes = 'Type' ]; +// Check that we have and can get each model type. _.each( modelsWithIndexes, function( className ) { QUnit.test( 'Testing ' + className + ' model.' , function( assert ) { @@ -175,3 +177,38 @@ _.each( modelsWithIndexes, function( className ) { }); } ); + +// Check that getAuthorUser handles errors when the callback fails. +QUnit.test( 'Testing getAuthorUser ajax failure.' , function( assert ) { + var done = assert.async(); + + assert.expect( 1 ); + + wp.api.loadPromise.done( function() { + var post = new wp.api.models.Post( { 'id': 1 } ); + post.fetch().done( function() { + + var originalFetch = window.Backbone.Model.prototype.fetch; + + // Override Backbone.Model.fetch to force an error. + window.Backbone.Model.prototype.fetch = function( options ) { + var deferred = jQuery.Deferred(), + promise = deferred.promise(); + + if ( options.error ) { + assert.equal( 1, 1 , 'getAuthorUser should have error callback on failure.' ); + done(); + } else { + assert.equal( 1, 0 , 'getAuthorUser should have error callback on failure.' ); + done(); + } + + deferred.reject(); + return promise; + }; + + post.getAuthorUser(); + window.Backbone.Model.prototype.fetch = originalFetch; + } ); + } ); +} );