Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fix failing specs for PromotionDuplicator

Damian Kaczmarczyk19ebe42e9a84
diff --git a/backend/spec/controllers/spree/admin/promotions_controller_spec.rb b/backend/spec/controllers/spree/admin/promotions_controller_spec.rb
index 7b8e9824d3..b3b3afd171 100644
--- a/backend/spec/controllers/spree/admin/promotions_controller_spec.rb
+++ b/backend/spec/controllers/spree/admin/promotions_controller_spec.rb
@@ -55,8 +55,8 @@ describe Spree::Admin::PromotionsController, type: :controller do
subject
new_promo = Spree::Promotion.last
expect(new_promo.name).to eq 'New name1'
- expect(new_promo.code).to eq 'code1_new'
- expect(new_promo.path).to eq 'path1_new'
+ expect(new_promo.code).to match(/code1_[a-zA-Z]{4}/)
+ expect(new_promo.path).to match(/path1_[a-zA-Z]{4}/)
end
end
@@ -68,7 +68,11 @@ describe Spree::Admin::PromotionsController, type: :controller do
let!(:promotion3) { create(:promotion, name: 'Name3', code: 'code3', path: '') }
before do
- create(:promotion, name: 'Name4', code: 'code4', path: '_new') # promotion 4
+ promotion_duplicator_mock = Spree::PromotionHandler::PromotionDuplicator.new(promotion3, random_string: "ABCD")
+ allow(Spree::PromotionHandler::PromotionDuplicator).to receive(:new).
+ with(promotion3).and_return(promotion_duplicator_mock)
+
+ create(:promotion, name: 'Name4', code: 'code4', path: '_ABCD') # promotion 4
end
it 'doesnt create a copy of promotion' do
diff --git a/core/app/models/spree/promotion_handler/promotion_duplicator.rb b/core/app/models/spree/promotion_handler/promotion_duplicator.rb
index e57140a173..f6d45b8858 100644
--- a/core/app/models/spree/promotion_handler/promotion_duplicator.rb
+++ b/core/app/models/spree/promotion_handler/promotion_duplicator.rb
@@ -1,16 +1,16 @@
module Spree
module PromotionHandler
class PromotionDuplicator
- def initialize(promotion)
+ def initialize(promotion, random_string: nil)
@promotion = promotion
+ @random_string = random_string || generate_random_string(4)
end
def duplicate
@new_promotion = @promotion.dup
- random_string = generate_random_string(4)
- @new_promotion.path = "#{@promotion.path}_#{random_string}"
+ @new_promotion.path = "#{@promotion.path}_#{@random_string}"
@new_promotion.name = "New #{@promotion.name}"
- @new_promotion.code = "#{@promotion.code}_#{random_string}"
+ @new_promotion.code = "#{@promotion.code}_#{@random_string}"
ActiveRecord::Base.transaction do
@new_promotion.save