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.

Make tests pass using a dummy serializer.

Sebastian Palma9c8ecfda97cb
diff --git a/api/app/models/concerns/spree/webhooks/has_webhooks.rb b/api/app/models/concerns/spree/webhooks/has_webhooks.rb
index f19fa3e9aa..544c7aeba1 100644
--- a/api/app/models/concerns/spree/webhooks/has_webhooks.rb
+++ b/api/app/models/concerns/spree/webhooks/has_webhooks.rb
@@ -31,8 +31,6 @@ module Spree
def body
resource_serializer.new(self).serializable_hash.to_json
- rescue NameError
- ''
end
def resource_serializer
diff --git a/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb b/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb
index d3326cf21c..a01d8a0f5e 100644
--- a/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb
+++ b/api/spec/models/concerns/spree/webhooks/has_webhooks_spec.rb
@@ -10,7 +10,13 @@ module Spree
end
end
-Spree::TestProduct = Spree::Webhooks::TestProduct
+module Spree
+ class TestProduct < ActiveRecord::Base
+ self.table_name = 'test_products'
+
+ include Spree::Webhooks::HasWebhooks
+ end
+end
describe Spree::Webhooks::HasWebhooks do
before(:all) do
@@ -28,6 +34,8 @@ describe Spree::Webhooks::HasWebhooks do
allow(queue_requests).to receive(:call).with(any_args)
end
+ after { ENV['DISABLE_SPREE_WEBHOOKS'] = 'true' }
+
let(:images) { create_list(:image, 2) }
let(:product) do
build(
@@ -63,15 +71,15 @@ describe Spree::Webhooks::HasWebhooks do
end
end
- context 'without DISABLE_SPREE_WEBHOOKS', :spree_webhooks do
- before do
- ENV['DISABLE_SPREE_WEBHOOKS'] = nil
- product.save
- end
-
+ context 'without DISABLE_SPREE_WEBHOOKS' do
context 'with a Spree::Webhooks descendant class' do
let(:product) { Spree::Webhooks::TestProduct.new(name: 'test') }
+ before do
+ ENV['DISABLE_SPREE_WEBHOOKS'] = nil
+ product.save
+ end
+
context 'after_create_commit' do
it 'does not queue a request' do
expect(queue_requests).not_to have_received(:call).with(hash_including(event: 'test_product.create'))
@@ -97,6 +105,11 @@ describe Spree::Webhooks::HasWebhooks do
context 'with a resource serializer' do
let(:body) { Spree::Api::V2::Platform::ProductSerializer.new(product).serializable_hash.to_json }
+ before do
+ ENV['DISABLE_SPREE_WEBHOOKS'] = nil
+ product.save
+ end
+
context 'after_create_commit' do
it 'queues a request through QueueRequests for product.create' do
expect(queue_requests).to have_received(:call).with(event: 'product.create', body: body).once
@@ -130,26 +143,30 @@ describe Spree::Webhooks::HasWebhooks do
end
end
- context 'without a resource serializer - blank body' do
+ context 'without a resource serializer' do
let(:product) { Spree::TestProduct.new(name: 'test') }
+ let(:exception_msg) { /uninitialized constant Spree::Api::V2::Platform::TestProductSerializer/ }
+
context 'after_create_commit' do
- it 'does not queue a request' do
- expect(queue_requests).not_to have_received(:call).with(hash_including(event: 'test_product.create'))
- end
+ before { ENV['DISABLE_SPREE_WEBHOOKS'] = nil }
+
+ it { expect { product.save }.to raise_error(NameError, exception_msg) }
end
- context 'after_destroy_commit' do
- it 'does not queue a request' do
- product.destroy
- expect(queue_requests).not_to have_received(:call).with(hash_including(event: 'test_product.destroy'))
+ context 'update/destroy' do
+ before do
+ ENV['DISABLE_SPREE_WEBHOOKS'] = 'true'
+ product.save
+ ENV['DISABLE_SPREE_WEBHOOKS'] = nil
end
- end
- context 'after_update_commit' do
- it 'does not queue a request' do
- product.update(name: 'new name')
- expect(queue_requests).not_to have_received(:call).with(hash_including(event: 'test_product.update'))
+ context 'after_destroy_commit' do
+ it { expect { product.destroy }.to raise_error(NameError, exception_msg) }
+ end
+
+ context 'after_update_commit' do
+ it { expect { product.update(name: 'new name') }.to raise_error(NameError, exception_msg) }
end
end
end
diff --git a/api/spec/spec_helper.rb b/api/spec/spec_helper.rb
index 6e9cc381c3..048eec52b7 100644
--- a/api/spec/spec_helper.rb
+++ b/api/spec/spec_helper.rb
@@ -66,6 +66,18 @@ RSpec.configure do |config|
config.include Spree::TestingSupport::Preferences
config.include Spree::TestingSupport::ImageHelpers
+ DummySerializer = Struct.new(:_) { def serializable_hash; {}; end }
+ Spree::Api::V2::Platform::ShippingCategorySerializer = DummySerializer
+ Spree::Api::V2::Platform::PriceSerializer = DummySerializer
+ Spree::Api::V2::Platform::StoreProductSerializer = DummySerializer
+ Spree::Api::V2::Platform::BogusSerializer = DummySerializer
+ Spree::Api::V2::Platform::FlatRateSerializer = DummySerializer
+ Spree::Api::V2::Platform::LegacyUserSerializer = DummySerializer
+ Spree::Api::V2::Platform::OptionValueVariantSerializer = DummySerializer
+ Spree::Api::V2::Platform::ShippingMethodCategorySerializer = DummySerializer
+ Spree::Api::V2::Platform::ShippingMethodZoneSerializer = DummySerializer
+ Spree::Api::V2::Platform::StockMovementSerializer = DummySerializer
+
config.before do
ENV['DISABLE_SPREE_WEBHOOKS'] = 'true'