Fix edge case when trying to deactivate the only availabe voucher

Plus spec
This commit is contained in:
Gaetan Craig-Riou
2023-09-11 12:00:48 +02:00
parent 48956b9bd1
commit e44a0092ea
2 changed files with 14 additions and 1 deletions

View File

@@ -270,7 +270,7 @@ module Admin
end
def update_vouchers
return if params[:enterprise][:voucher_ids].blank?
params[:enterprise][:voucher_ids] = [] if params[:enterprise][:voucher_ids].blank?
params_voucher_ids = params[:enterprise][:voucher_ids].map(&:to_i)
voucher_ids = @enterprise.vouchers.map(&:id)

View File

@@ -319,6 +319,19 @@ describe Admin::EnterprisesController, type: :controller do
expect(voucher_a.reload.deleted?).to be(true)
end
context "when desactivating the only existing voucher" do
it "deactivates the voucher" do
voucher_b.really_destroy!
# Rails won't populate vouchers_id params if no voucher selected
spree_put :update,
id: enterprise,
enterprise: {}
expect(voucher_a.reload.deleted?).to be(true)
end
end
context "when activating and deactivating voucher at the same time" do
it "deactivates and activates accordingly" do
voucher_c = create(:voucher, enterprise: enterprise, code: "voucher 3")