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.

Remove super class re-definition for testing prependings.

Sebastian Palmaef979a75cc37
diff --git a/api/app/models/spree/api/webhooks/payment.rb b/api/app/models/spree/api/webhooks/payment.rb
index 119d6f7da6..6e3dda54b0 100644
--- a/api/app/models/spree/api/webhooks/payment.rb
+++ b/api/app/models/spree/api/webhooks/payment.rb
@@ -2,9 +2,7 @@ module Spree
module Api
module Webhooks
module Payment
- private
-
- def after_void
+ def void
super
Spree::Webhooks::Endpoints::QueueRequests.call(event: 'payment.void', payload: {})
end
diff --git a/api/app/models/spree/api/webhooks/shipment.rb b/api/app/models/spree/api/webhooks/shipment.rb
index 152da17260..1594bcdca3 100644
--- a/api/app/models/spree/api/webhooks/shipment.rb
+++ b/api/app/models/spree/api/webhooks/shipment.rb
@@ -2,9 +2,7 @@ module Spree
module Api
module Webhooks
module Shipment
- private
-
- def after_ship
+ def ship
super
Spree::Webhooks::Endpoints::QueueRequests.call(event: 'shipment.ship', payload: {})
end
diff --git a/api/spec/models/spree/api/webhooks/order_spec.rb b/api/spec/models/spree/api/webhooks/order_spec.rb
index 5cdabee86b..86c37de87f 100644
--- a/api/spec/models/spree/api/webhooks/order_spec.rb
+++ b/api/spec/models/spree/api/webhooks/order_spec.rb
@@ -1,47 +1,34 @@
require 'spec_helper'
-class TextFormatter
-end
-
-xdescribe Spree::Order do
+describe Spree::Order do
describe 'sending webhooks after transitioning from states' do
- let(:order) { Spree::Order.create }
let(:payload) { {} }
let(:queue_requests) { instance_double(Spree::Webhooks::Endpoints::QueueRequests) }
+ let!(:store) { create(:store, default: true) }
- shared_examples 'queues a webhook request' do |event, method_name|
- before do
- allow(Spree::Webhooks::Endpoints::QueueRequests).to receive(:new).and_return(queue_requests)
- allow(queue_requests).to receive(:call).with(any_args)
- # redefine `method_name` method body to assert super is being used
- # [TODO]: find another way to test super is being used
- # this makes other tests relying on `method_name` fail
- described_class.class_eval do
- define_method(method_name) do
- TextFormatter.new.to_s
- end
- end
- end
-
- it "executes QueueRequests.call with a order.#{event} event and {} payload after calling super" do
- expect(TextFormatter).to(
- receive(:new).and_return(
- double(:scope).tap { |scope| expect(scope).to receive(:to_s) }
- )
- )
- order.send(method_name)
- expect(queue_requests).to have_received(:call).with(event: "order.#{event}", payload: payload).once
- end
+ before do
+ allow(Spree::Webhooks::Endpoints::QueueRequests).to receive(:new).and_return(queue_requests)
+ allow(queue_requests).to receive(:call).with(any_args)
end
context '#cancel' do
- include_examples 'queues a webhook request', :cancel, :cancel
+ let(:order) { create(:completed_order_with_totals, store: store) }
+
+ it 'executes QueueRequests.call with a order.cancel event and {} payload after calling super' do
+ order.cancel
+ expect(queue_requests).to have_received(:call).with(event: 'order.cancel', payload: payload).once
+ end
end
context '#finalize!' do
- before { order.update_column :state, 'complete' }
+ let(:order) { Spree::Order.create(email: '[email protected]', store: store) }
- include_examples 'queues a webhook request', :complete, :finalize!
+ it 'executes QueueRequests.call with a order.complete event and {} payload after calling super' do
+ # check the last statement in the method body is actually
+ # executed, because Order doesn't have a complete method.
+ order.finalize!
+ expect(queue_requests).to have_received(:call).with(event: 'order.complete', payload: payload).once
+ end
end
end
end
diff --git a/api/spec/models/spree/api/webhooks/payment_spec.rb b/api/spec/models/spree/api/webhooks/payment_spec.rb
index b9c2d9f412..92d3a10fb4 100644
--- a/api/spec/models/spree/api/webhooks/payment_spec.rb
+++ b/api/spec/models/spree/api/webhooks/payment_spec.rb
@@ -1,34 +1,19 @@
require 'spec_helper'
-class TextFormatter
-end
-
describe Spree::Payment do
let(:payment) { create(:payment) }
- describe '#after_void' do
+ describe '#void' do
let(:payload) { {} }
let(:queue_requests) { instance_double(Spree::Webhooks::Endpoints::QueueRequests) }
before do
- # redefine the after_void method body to assert super is being used
- described_class.class_eval do
- def after_void
- TextFormatter.new.to_s
- end
- end
-
allow(Spree::Webhooks::Endpoints::QueueRequests).to receive(:new).and_return(queue_requests)
allow(queue_requests).to receive(:call).with(any_args)
end
shared_examples 'queues a webhook request' do
- it 'executes QueueRequests.call with a payment.void event and {} payload after calling super' do
- expect(TextFormatter).to(
- receive(:new).and_return(
- double(:scope).tap { |scope| expect(scope).to receive(:to_s) }
- )
- )
+ it 'executes QueueRequests.call with a payment.void event and {} payload after invoking void' do
payment.void
expect(queue_requests).to have_received(:call).with(event: 'payment.void', payload: payload).once
end
diff --git a/api/spec/models/spree/api/webhooks/shipment_spec.rb b/api/spec/models/spree/api/webhooks/shipment_spec.rb
index 376464ec8b..b53be83fb5 100644
--- a/api/spec/models/spree/api/webhooks/shipment_spec.rb
+++ b/api/spec/models/spree/api/webhooks/shipment_spec.rb
@@ -1,20 +1,10 @@
require 'spec_helper'
-class TextFormatter
-end
-
describe Spree::Shipment do
let(:shipment) { create(:shipment) }
- context '#after_ship' do
+ context '#ship' do
before do
- # redefine the after_ship method body to assert super is being used
- described_class.class_eval do
- def after_ship
- TextFormatter.new.to_s
- end
- end
-
allow(Spree::Webhooks::Endpoints::QueueRequests).to receive(:new).and_return(queue_requests)
allow(queue_requests).to receive(:call).with(any_args)
end
@@ -23,11 +13,7 @@ describe Spree::Shipment do
let(:queue_requests) { instance_double(Spree::Webhooks::Endpoints::QueueRequests) }
it 'executes QueueRequests.call with a shipment.ship event and {} payload after calling super' do
- expect(TextFormatter).to(
- receive(:new).and_return(
- double(:scope).tap { |scope| expect(scope).to receive(:to_s) }
- )
- )
+ expect(shipment).to receive(:after_ship).once
shipment.cancel # previous state that allows the object be shipped
shipment.ship
expect(queue_requests).to have_received(:call).with(event: 'shipment.ship', payload: payload).once
diff --git a/api/spec/serializers/spree/v2/storefront/menu_serializer_spec.rb b/api/spec/serializers/spree/v2/storefront/menu_serializer_spec.rb
index c8bc0c06b5..f22ad9983b 100644
--- a/api/spec/serializers/spree/v2/storefront/menu_serializer_spec.rb
+++ b/api/spec/serializers/spree/v2/storefront/menu_serializer_spec.rb
@@ -3,9 +3,12 @@ require 'spec_helper'
describe Spree::V2::Storefront::MenuSerializer do
subject { described_class.new(menu) }
- let!(:menu) { create(:menu) }
+ let(:menu) { create(:menu) }
- before { Rails.cache.clear }
+ before do
+ allow_any_instance_of(Spree::Base).to receive(:queue_webhooks_requests!)
+ menu
+ end
it { expect(subject.serializable_hash).to be_kind_of(Hash) }
diff --git a/api/spec/spec_helper.rb b/api/spec/spec_helper.rb
index 998de3c110..c003672004 100644
--- a/api/spec/spec_helper.rb
+++ b/api/spec/spec_helper.rb
@@ -65,6 +65,8 @@ RSpec.configure do |config|
config.include Spree::TestingSupport::ImageHelpers
config.before do
+ Rails.cache.clear
+
reset_spree_preferences
Spree::Api::Config[:requires_authentication] = true
diff --git a/core/app/models/spree/payment.rb b/core/app/models/spree/payment.rb
index 85313d24b5..be52a2231a 100644
--- a/core/app/models/spree/payment.rb
+++ b/core/app/models/spree/payment.rb
@@ -97,7 +97,6 @@ module Spree
event :void do
transition from: [:pending, :processing, :completed, :checkout], to: :void
end
- after_transition to: :void, do: :after_void
# when the card brand isnt supported
event :invalidate do
transition from: [:checkout], to: :invalid
@@ -196,10 +195,6 @@ module Spree
private
- def after_void
- # this method is prepended in api/ to queue Webhooks requests
- end
-
def has_invalid_state?
INVALID_STATES.include?(state)
end