From d5dca97438d0a4713a257a0521d72c0ce5e0a6c4 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Thu, 2 Nov 2023 16:22:21 +1100 Subject: [PATCH 1/3] Spec tiny bug which caused a flaky spec It still needs solving. --- spec/system/admin/order_spec.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/spec/system/admin/order_spec.rb b/spec/system/admin/order_spec.rb index bc398e7673..5ef3ec7fe1 100644 --- a/spec/system/admin/order_spec.rb +++ b/spec/system/admin/order_spec.rb @@ -350,7 +350,17 @@ describe ' fill_in(:quantity, with: max_quantity + 1) find("a.save-item").click end - click_button("OK") + + pending "Reload bug" + # The following modal is displayed but disappears straight away because the + # page is reloaded after the click on the save button. Somehow this happens + # only the first time the page is loaded and after that this logic seems to + # work fine. + sleep 1 + within(".modal") do + expect(page).to have_content "Quantity unchanged from previous amount" + click_on "OK" + end expect(page).to_not have_content "Loading..." within("tr.stock-item", text: order.products.first.name) do From efc338aeeb223b8759ba071265b01ae92634f6c3 Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Fri, 3 Nov 2023 14:15:16 +1100 Subject: [PATCH 2/3] Don't update order when not enough stock There's a user-facing change here. When you tried to update the quantity of a line item beyond available stock, two things used to happen: 1. A warning was displayed. 2. The item's quantity was updated to the highest possible. Unfortunately, the logic to update the line item was also reloading the page and the warning message disappeared before it could be acknowledged. The easiest fix was to skip the update request. And in my opinion, it's even better to let the user decide if they still want to update or cancel the update. Eventually, we want to replace all this custom Javascript logic with StimulusJS anyway. So let's not put too much effort into this. It was important though to resolve the flaky spec which made many builds fail. --- .../spree/orders/variant_autocomplete.js.erb | 4 ++-- spec/system/admin/order_spec.rb | 19 ++++++------------- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/app/assets/javascripts/admin/spree/orders/variant_autocomplete.js.erb b/app/assets/javascripts/admin/spree/orders/variant_autocomplete.js.erb index a8a96ec36b..bf054b0cd1 100644 --- a/app/assets/javascripts/admin/spree/orders/variant_autocomplete.js.erb +++ b/app/assets/javascripts/admin/spree/orders/variant_autocomplete.js.erb @@ -51,10 +51,10 @@ $(document).ready(function() { quantity = maxQuantity; save.parents('tr').find('input.line_item_quantity').val(maxQuantity); ofnAlert(t("js.admin.orders.quantity_adjusted")); + } else { + adjustItems(shipment_number, variant_id, quantity, true); } - toggleItemEdit(); - adjustItems(shipment_number, variant_id, quantity, true); return false; } $('a.save-item').click(handle_save_click); diff --git a/spec/system/admin/order_spec.rb b/spec/system/admin/order_spec.rb index 5ef3ec7fe1..1a94f4ca0f 100644 --- a/spec/system/admin/order_spec.rb +++ b/spec/system/admin/order_spec.rb @@ -339,34 +339,27 @@ describe ' login_as_admin visit spree.edit_admin_order_path(order) - quantity = order.line_items.first.quantity - max_quantity = 0 + item = order.line_items.first + quantity = item.quantity + max_quantity = quantity + item.variant.on_hand total = order.display_total within("tr.stock-item", text: order.products.first.name) do find("a.edit-item").click expect(page).to have_input(:quantity) - max_quantity = find("input[name='quantity']")["max"].to_i fill_in(:quantity, with: max_quantity + 1) find("a.save-item").click end - pending "Reload bug" - # The following modal is displayed but disappears straight away because the - # page is reloaded after the click on the save button. Somehow this happens - # only the first time the page is loaded and after that this logic seems to - # work fine. - sleep 1 within(".modal") do - expect(page).to have_content "Quantity unchanged from previous amount" + expect(page).to have_content "Insufficient stock available" click_on "OK" end - expect(page).to_not have_content "Loading..." within("tr.stock-item", text: order.products.first.name) do - expect(page).to have_text(max_quantity.to_s) + expect(page).to have_field :quantity, with: max_quantity.to_s end - expect(order.reload.line_items.first.quantity).to eq(max_quantity) + expect { item.reload }.to_not change { item.quantity } end it "there are infinite items available (variant is on demand)" do From 71c50e1fcffeb589ad7240e26450ec2620b63ecb Mon Sep 17 00:00:00 2001 From: Maikel Linke Date: Fri, 3 Nov 2023 14:31:29 +1100 Subject: [PATCH 3/3] Update warning message for item quantity --- .../javascripts/admin/spree/orders/variant_autocomplete.js.erb | 2 +- config/locales/en.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/admin/spree/orders/variant_autocomplete.js.erb b/app/assets/javascripts/admin/spree/orders/variant_autocomplete.js.erb index bf054b0cd1..001055417e 100644 --- a/app/assets/javascripts/admin/spree/orders/variant_autocomplete.js.erb +++ b/app/assets/javascripts/admin/spree/orders/variant_autocomplete.js.erb @@ -50,7 +50,7 @@ $(document).ready(function() { if (quantity > maxQuantity) { quantity = maxQuantity; save.parents('tr').find('input.line_item_quantity').val(maxQuantity); - ofnAlert(t("js.admin.orders.quantity_adjusted")); + ofnAlert(t("js.admin.orders.quantity_unavailable")); } else { adjustItems(shipment_number, variant_id, quantity, true); } diff --git a/config/locales/en.yml b/config/locales/en.yml index 583a2962f7..1927079a11 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -3440,7 +3440,7 @@ See the %{link} to find out more about %{sitename}'s features and to start using processing: "processing" void: "void" invalid: "invalid" - quantity_adjusted: "Insufficient stock available. Line item updated to maximum available quantity." + quantity_unavailable: "Insufficient stock available. Line item unsaved!" quantity_unchanged: "Quantity unchanged from previous amount." cancel_the_order_html: "This will cancel the current order.
Are you sure you want to proceed?" cancel_the_order_send_cancelation_email: "Send a cancellation email to the customer"