Skip to content

Commit

Permalink
Merge pull request #5827 from nvandoorn/call-empty-only-on-incomplete…
Browse files Browse the repository at this point in the history
…-orders

Call empty only on incomplete orders
  • Loading branch information
MadelineCollier authored Sep 19, 2024
2 parents 0f77e8b + c4fd1df commit 72920b8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 34 deletions.
9 changes: 7 additions & 2 deletions api/app/controllers/spree/api/orders_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,13 @@ def create

def empty
authorize! :update, @order, order_token
@order.empty!
respond_with(@order, default_template: :show)

if @order.complete?
invalid_resource!(@order)
else
@order.empty!
respond_with(@order, default_template: :show)
end
end

def index
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ Spree.Views.Cart.EmptyCartButton = Backbone.View.extend({

render: function() {
var isNew = function (item) { return item.isNew() };
this.$el.prop("disabled", !this.collection.length || this.collection.some(isNew));
this.$el.prop("disabled", !this.collection.length || this.collection.some(isNew) || this.model.get("completed_at"));
}
});
57 changes: 26 additions & 31 deletions backend/spec/features/admin/orders/order_details_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,39 +137,11 @@
expect(page).to have_field('quantity')
end

it "can remove all items with empty cart" do
it "cannot remove all items with a completed order" do
# Wait for the cart contents to be loaded.
expect(page).to have_content("spree t-shirt")

within("#item_total") do
expect(page).to have_content("$40.00")
end

within("#order_total") do
expect(page).to have_content("$40.00")
end

within("#order-total", text: 'Order Total') do
expect(page).to have_content("$40.00")
end

accept_confirm "Are you sure you want to delete this record?" do
click_on 'Empty Cart'
end

expect(page).not_to have_content("spree t-shirt")

# Should have a new item row
expect(page).to have_field('quantity')

within("#item_total") do
expect(page).to have_content("$0.00")
end

within("#order_total") do
expect(page).to have_content("$0.00")
end

expect(page).to have_css('#order-total', visible: false)
expect(page).to have_button("Empty Cart", disabled: true)
end

# Regression test for https://github.com/spree/spree/issues/3862
Expand Down Expand Up @@ -384,6 +356,29 @@
expect(page).not_to have_selector('.fa-arrows-h')
expect(page).not_to have_selector('.fa-trash')
end

context 'order has shipped' do
it 'disables empty cart' do
order = create(:order_ready_to_ship)

visit spree.cart_admin_order_path(order)
order.fulfill!

## simulate shipping order in another tab
shipment = order.shipments.first
order.shipping.ship(
inventory_units: shipment.inventory_units,
stock_location: shipment.stock_location,
address: order.ship_address,
shipping_method: shipment.shipping_method
)

# Make an assertion here about page content to give the JS time
# to load before the next expectation.
expect(page).to have_content("Cart")
expect(page).to have_button("Empty Cart", disabled: true)
end
end
end
end

Expand Down

0 comments on commit 72920b8

Please sign in to comment.