From 3dff93298960f5a9b0f26cadae3fa958dee3e997 Mon Sep 17 00:00:00 2001 From: Connor Ferguson <68167430+cpfergus1@users.noreply.github.com> Date: Tue, 13 Sep 2022 06:45:17 -0600 Subject: [PATCH 1/3] Use fail/then instead of error/done From the jQuery upgrade guide: The jqXHR object returned from jQuery.ajax() is a jQuery Deferred and has historically had three extra methods with names matching the arguments object of success, error, and complete. This often confused people who did not realize that the returned object should be treated like a Deferred. As of jQuery 3.0 these methods have been removed. As replacements, use the Deferred standard methods of done, fail, and always, or use the new then and catch methods for Promises/A+ compliance. Note that this does not have any impact at all on the ajax callbacks of the same name passed through the options object, which continue to exist and are not deprecated. This only affects the jqXHR methods. --- .../spree/backend/models/image_upload.js | 4 ++-- .../javascripts/spree/backend/shipments.js | 22 ++++++++----------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/backend/app/assets/javascripts/spree/backend/models/image_upload.js b/backend/app/assets/javascripts/spree/backend/models/image_upload.js index 11524fa3794..92a117de5f5 100644 --- a/backend/app/assets/javascripts/spree/backend/models/image_upload.js +++ b/backend/app/assets/javascripts/spree/backend/models/image_upload.js @@ -67,9 +67,9 @@ Spree.Models.ImageUpload = Backbone.Model.extend({ } return xhr; } - }).done(function() { + }).then(function() { that.set({progress: 100}) - }).error(function(jqXHR, textStatus, errorThrown) { + }).fail(function() { that.set({serverError: true}); }); } diff --git a/backend/app/assets/javascripts/spree/backend/shipments.js b/backend/app/assets/javascripts/spree/backend/shipments.js index 202a647b8b8..2ab3d5a7b12 100644 --- a/backend/app/assets/javascripts/spree/backend/shipments.js +++ b/backend/app/assets/javascripts/spree/backend/shipments.js @@ -100,30 +100,26 @@ var ShipmentSplitItemView = Backbone.View.extend({ variant_id: this.variant.id, quantity: quantity }; - var jqXHR; + var path; if (target_type == 'stock_location') { // transfer to a new location split_attr.stock_location_id = target_id; - jqXHR = Spree.ajax({ - type: "POST", - url: Spree.pathFor('api/shipments/transfer_to_location'), - data: split_attr - }); + path = Spree.pathFor("api/shipments/transfer_to_location"); } else if (target_type == 'shipment') { // transfer to an existing shipment split_attr.target_shipment_number = target_id; - jqXHR = Spree.ajax({ - type: "POST", - url: Spree.pathFor('api/shipments/transfer_to_shipment'), - data: split_attr - }); + path = Spree.pathFor('api/shipments/transfer_to_shipment'); } else { alert('Please select the split destination.'); return false; } - jqXHR.error(function(msg) { + Spree.ajax({ + type: "POST", + url: path, + data: split_attr + }).fail(function(msg) { alert(Spree.t("split_failed")); - }).done(function(response) { + }).then(function(response) { if (response.success) { window.Spree.advanceOrder(); } else { From 93caa3e5f5acc1b81d1094ffde19cae54e7f7e02 Mon Sep 17 00:00:00 2001 From: Connor Ferguson <68167430+cpfergus1@users.noreply.github.com> Date: Tue, 13 Sep 2022 06:46:45 -0600 Subject: [PATCH 2/3] Update deprecated method to #ajaxSetup --- .../app/assets/javascripts/spree/backend/models/image_upload.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/app/assets/javascripts/spree/backend/models/image_upload.js b/backend/app/assets/javascripts/spree/backend/models/image_upload.js index 92a117de5f5..01e6721cb32 100644 --- a/backend/app/assets/javascripts/spree/backend/models/image_upload.js +++ b/backend/app/assets/javascripts/spree/backend/models/image_upload.js @@ -56,7 +56,7 @@ Spree.Models.ImageUpload = Backbone.Model.extend({ processData: false, // tell jQuery not to process the data contentType: false, // tell jQuery not to set contentType xhr: function () { - var xhr = $.ajaxSettings.xhr(); + var xhr = $.ajaxSetup.xhr(); // Using default ajax builder but inputting upload settings if (xhr.upload) { xhr.upload.onprogress = function (event) { if (event.lengthComputable) { From 57823b24ed228925f3569312e8b96e74dbba9ea8 Mon Sep 17 00:00:00 2001 From: Connor Ferguson <68167430+cpfergus1@users.noreply.github.com> Date: Wed, 14 Sep 2022 12:08:27 -0600 Subject: [PATCH 3/3] Update Spec to force AJAX Error Specs did not catch the deprecated method "error" because there were not any tests that checked for an AJAX error. Added steps to a current current test to ensure an AJAX call fails to cover the method "fail" and "then" incase those methods might be deprecated in the future --- backend/spec/features/admin/orders/shipments_spec.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/backend/spec/features/admin/orders/shipments_spec.rb b/backend/spec/features/admin/orders/shipments_spec.rb index 2043dbbab64..3ef15183e6f 100644 --- a/backend/spec/features/admin/orders/shipments_spec.rb +++ b/backend/spec/features/admin/orders/shipments_spec.rb @@ -155,9 +155,13 @@ def ship_shipment alert_text = page.driver.browser.switch_to.alert.text expect(alert_text).to include 'Please select the split destination' page.driver.browser.switch_to.alert.accept + find('#item_quantity').fill_in(with: 'text') find('.select2-container').click find(:xpath, '//body').all('.select2-drop li.select2-result', text: "#{location_name} (0 on hand)")[1].click find('.save-split').click + accept_alert 'Quantity must be greater than 0' # Test Ajax error handling + find('#item_quantity').fill_in(with: '1') + find('.save-split').click end expect(page).to have_content /Pending package from '#{location_name}'/i end