Compare commits

...

3 Commits

Author SHA1 Message Date
Jean-Baptiste Bellet
e4ad6d736b Canceling an order: clicking on cancel btn or cancel action in dropdown 2023-04-20 13:53:37 +02:00
basilawwad
9f30471373 Use a shared_examples for the order cancellation tests 2023-04-20 12:28:27 +02:00
basilawwad
fa373518fb applied modal function
Update variant_autocomplete.js.erb

Applied modal function

refactored function

fix indentation and removed old function
2023-04-20 11:46:02 +02:00
3 changed files with 98 additions and 64 deletions

View File

@@ -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);
}

View File

@@ -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

View File

@@ -104,6 +104,82 @@ 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
within(".modal") do
uncheck("restock_items")
click_on("OK")
end
expect(shipment.stock_location).not_to receive(:restock)
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])
@@ -178,55 +254,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