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", ] 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 diff --git a/spec/system/admin/order_spec.rb b/spec/system/admin/order_spec.rb index 96211327b5..d61513f12c 100644 --- a/spec/system/admin/order_spec.rb +++ b/spec/system/admin/order_spec.rb @@ -160,6 +160,88 @@ 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_any_instance_of(Spree::StockLocation).to receive(:restock).at_least(1).times + 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_any_instance_of(Spree::StockLocation).to receive(:restock).at_least(1).times + 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_any_instance_of(Spree::StockLocation).not_to receive(:restock) + 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 + + 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]) @@ -234,55 +316,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 @@ -565,7 +599,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