From fa373518fb953ea3b8794d5028dbfb5b92185110 Mon Sep 17 00:00:00 2001 From: basilawwad Date: Sat, 5 Nov 2022 00:59:37 +0300 Subject: [PATCH 1/6] applied modal function Update variant_autocomplete.js.erb Applied modal function refactored function fix indentation and removed old function --- .../spree/orders/variant_autocomplete.js.erb | 33 +++++++++++-------- app/helpers/spree/admin/orders_helper.rb | 3 +- 2 files changed, 21 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 175942b859..fa6480ab99 100644 --- a/app/assets/javascripts/admin/spree/orders/variant_autocomplete.js.erb +++ b/app/assets/javascripts/admin/spree/orders/variant_autocomplete.js.erb @@ -4,7 +4,8 @@ $(document).ready(function() { initAlert() initConfirm() - initCancelOrder() + initButtonCancel() + initLinkCancel() if ($('#variant_autocomplete_template').length > 0) { window.variantTemplate = Handlebars.compile($('#variant_autocomplete_template').text()); @@ -276,17 +277,23 @@ ofnConfirm = function(callback) { $('#custom-confirm').show(); } -initCancelOrder = function() { - $('#cancel_order_form').submit(function(e){ - ofnCancelOrderAlert((confirm, sendEmailCancellation, restock_items) => { - if (confirm) { - var redirectTo = new URL(Spree.routes.cancel_order.toString()); - redirectTo.searchParams.append("send_cancellation_email", sendEmailCancellation); - redirectTo.searchParams.append("restock_items", restock_items); - window.location.href = redirectTo.toString(); - } - }); - e.preventDefault(); - return false; +initCancelAction = function(e){ + ofnCancelOrderAlert((confirm, sendEmailCancellation, restock_items) => { + if (confirm) { + var redirectTo = new URL(Spree.routes.cancel_order.toString()); + redirectTo.searchParams.append("send_cancellation_email", sendEmailCancellation); + redirectTo.searchParams.append("restock_items", restock_items); + window.location.href = redirectTo.toString(); + } }); + e.preventDefault(); + return false; +}; + +initButtonCancel = function() { + $('#cancel_order_form').submit(initCancelAction) +} + +initLinkCancel = function() { + $('#links-dropdown a[href$="cancel"]').click(initCancelAction); } diff --git a/app/helpers/spree/admin/orders_helper.rb b/app/helpers/spree/admin/orders_helper.rb index e38113528e..92e55fa008 100644 --- a/app/helpers/spree/admin/orders_helper.rb +++ b/app/helpers/spree/admin/orders_helper.rb @@ -105,8 +105,7 @@ module Spree def cancel_order_link { name: t(:cancel_order), url: spree.fire_admin_order_path(@order.number, e: 'cancel'), - icon: 'icon-trash', - confirm: t(:are_you_sure) } + icon: 'icon-trash' } end def cancel_event_link From e633670e0198b9fd4c6d3a800d011c1aac7fd0a3 Mon Sep 17 00:00:00 2001 From: basilawwad Date: Sun, 22 Jan 2023 22:23:56 +0300 Subject: [PATCH 2/6] Use a shared_examples for the order cancellation tests --- spec/system/admin/order_spec.rb | 102 +++++++++++++++++--------------- 1 file changed, 53 insertions(+), 49 deletions(-) diff --git a/spec/system/admin/order_spec.rb b/spec/system/admin/order_spec.rb index d9e3dae850..b63f199b90 100644 --- a/spec/system/admin/order_spec.rb +++ b/spec/system/admin/order_spec.rb @@ -104,6 +104,58 @@ describe ' expect(order.line_items.reload.map(&:product)).to include product end + shared_examples_for "Cancelling the order" do + it "shows a modal about order cancellation" do + expect(page).to have_content "This will cancel the current order." + expect(page).to have_checked_field "Send a cancellation email to the customer" + expect(page).to have_checked_field "Restock Items: return all items to stock" + end + + it "that the user can close and then nothing changes" do + within(".modal") do + expect do + click_on("Cancel") + end.not_to change { order.reload.state } + end + end + + context "that the user can confirm" do + let(:shipment) { order.shipments.first } + + it "and by default an Email is sent and the items are restocked" do + expect do + within(".modal") do + click_on("OK") + end + expect(page).to have_content "Cannot add item to canceled order" + expect(order.reload.state).to eq("canceled") + end.to have_enqueued_mail(Spree::OrderMailer, :cancel_email) + end + + it "and then the order is cancelled and email is not sent when unchecked" do + expect do + within(".modal") do + uncheck("send_cancellation_email") + click_on("OK") + end + expect(page).to have_content "Cannot add item to canceled order" + expect(order.reload.state).to eq("canceled") + end.to_not have_enqueued_mail(Spree::OrderMailer, :cancel_email) + end + + it "and the items are not restocked when the user uncheck the checkbox to restock items" do + expect do + within(".modal") do + uncheck("restock_items") + click_on("OK") + end + expect(page).to have_content "Cannot add item to canceled order" + expect(order.reload.state).to eq("canceled") + end.to have_enqueued_mail(Spree::OrderMailer, :cancel_email) + end + end + end + it "displays error when incorrect distribution for products is chosen" do d = create(:distributor_enterprise) oc = create(:simple_order_cycle, distributors: [d]) @@ -178,55 +230,7 @@ describe ' find("a.delete-item").click end - context "it shows a modal about last item deletion and therefore about order cancellation" do - it "that the user can close and then nothing change" do - expect(page).to have_content "This will cancel the current order." - expect(page).to have_checked_field "Send a cancellation email to the customer" - within(".modal") do - click_on("Cancel") - end - - expect(order.reload.line_items.length).to eq(1) - expect(page).to have_selector('tr.stock-item', count: 1) - end - - context "that the user can confirm" do - it "and then the order is cancelled and no email is sent by default" do - expect do - within(".modal") do - uncheck("send_cancellation_email") - click_on("OK") - end - expect(page).to have_content "Cannot add item to canceled order" - expect(order.reload.line_items.length).to eq(0) - expect(order.reload.state).to eq("canceled") - end.to_not have_enqueued_mail(Spree::OrderMailer, :cancel_email) - end - - it "and check the checkbox to send an email to the customer "\ - "about its order cancellation" do - expect do - within(".modal") do - click_on("OK") - end - expect(page).to have_content "Cannot add item to canceled order" - expect(order.reload.line_items.length).to eq(0) - expect(order.reload.state).to eq("canceled") - end.to have_enqueued_mail(Spree::OrderMailer, :cancel_email) - end - end - - context "that the user can choose to restock item" do - let(:shipment) { order.shipments.first } - it "uncheck the checkbox to not restock item" do - within(".modal") do - check("restock_items") - click_on("OK") - end - expect(shipment.stock_location).not_to receive(:restock) - end - end - end + it_should_behave_like "Cancelling the order" end end From 81606e841ac5c78dfa1349f81e31449503a0497b Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Thu, 20 Apr 2023 13:53:37 +0200 Subject: [PATCH 3/6] Canceling an order: clicking on cancel btn or cancel action in dropdown --- spec/system/admin/order_spec.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/spec/system/admin/order_spec.rb b/spec/system/admin/order_spec.rb index b63f199b90..7c87ea5efd 100644 --- a/spec/system/admin/order_spec.rb +++ b/spec/system/admin/order_spec.rb @@ -156,6 +156,33 @@ describe ' end end + context "cancelling an order" do + let(:line_item) { create(:line_item) } + + before do + order.line_items << line_item + login_as_admin + visit spree.edit_admin_order_path(order) + end + + context "when using the cancel button" do + before do + find("#cancel_order_form").click + end + + it_should_behave_like "Cancelling the order" + end + + context "when using the cancel option in the dropdown" do + before do + find("#links-dropdown .ofn-drop-down").click + find('a[href$="cancel"]').click + end + + it_should_behave_like "Cancelling the order" + end + end + it "displays error when incorrect distribution for products is chosen" do d = create(:distributor_enterprise) oc = create(:simple_order_cycle, distributors: [d]) From 2cc509de15fd310e7c334490ff35b10d8fa4a631 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Thu, 20 Apr 2023 14:53:07 +0200 Subject: [PATCH 4/6] Do not check Metrics/BlockLength for shared_examples_for block They can contains a lot of `it` tests --- .rubocop_styleguide.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.rubocop_styleguide.yml b/.rubocop_styleguide.yml index 59258df101..a5878eef94 100644 --- a/.rubocop_styleguide.yml +++ b/.rubocop_styleguide.yml @@ -40,6 +40,7 @@ Metrics/BlockLength: "resources", "scenario", "shared_examples", + "shared_examples_for", "xdescribe", ] From 5b9815f4a323414508210d7780fd8ace098372fc Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Mon, 24 Apr 2023 15:03:14 +0200 Subject: [PATCH 5/6] Test that restock has been called or not, depending on checkbox checked --- spec/system/admin/order_spec.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/spec/system/admin/order_spec.rb b/spec/system/admin/order_spec.rb index 7c87ea5efd..2fae405c2a 100644 --- a/spec/system/admin/order_spec.rb +++ b/spec/system/admin/order_spec.rb @@ -123,6 +123,7 @@ describe ' let(:shipment) { order.shipments.first } it "and by default an Email is sent and the items are restocked" do + expect_any_instance_of(Spree::StockLocation).to receive(:restock).at_least(1).times expect do within(".modal") do click_on("OK") @@ -133,6 +134,7 @@ describe ' end it "and then the order is cancelled and email is not sent when unchecked" do + expect_any_instance_of(Spree::StockLocation).to receive(:restock).at_least(1).times expect do within(".modal") do uncheck("send_cancellation_email") @@ -144,6 +146,7 @@ describe ' end it "and the items are not restocked when the user uncheck the checkbox to restock items" do + expect_any_instance_of(Spree::StockLocation).not_to receive(:restock) expect do within(".modal") do uncheck("restock_items") From 1730c9eb60419994564c3838682ac27902070dfc Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Mon, 24 Apr 2023 15:18:35 +0200 Subject: [PATCH 6/6] Fix one warning: Modal window with text has been opened, ... ``` Modal window with text `An invoice for this order will be sent to the customer. Are you sure you want to continue?` has been opened, but you didn't wrap your code into (`accept_prompt` | `dismiss_prompt` | `accept_confirm` | `dismiss_confirm` | `accept_alert`), accepting by default ``` --- spec/system/admin/order_spec.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/spec/system/admin/order_spec.rb b/spec/system/admin/order_spec.rb index 2fae405c2a..78bbafa549 100644 --- a/spec/system/admin/order_spec.rb +++ b/spec/system/admin/order_spec.rb @@ -543,7 +543,10 @@ describe ' end it 'can send invoices' do - click_link "Send Invoice" + accept_alert "An invoice for this order will be sent to the customer. "\ + "Are you sure you want to continue?" do + click_link "Send Invoice" + end expect(page).to have_content "Invoice email has been sent" end end