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 flaky tests: handle prefixed IDs and non-deterministic positions (#13547)

Damian Legawiec83ffd6eef21a
diff --git a/admin/spec/controllers/spree/admin/products_controller_spec.rb b/admin/spec/controllers/spree/admin/products_controller_spec.rb
index 2a87e9cc62..1d625a384c 100644
--- a/admin/spec/controllers/spree/admin/products_controller_spec.rb
+++ b/admin/spec/controllers/spree/admin/products_controller_spec.rb
@@ -1459,8 +1459,11 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
it 'assigns the product positions on the taxon list' do
send_request
- expect(product.reload.classifications.last.position).to eq(1)
- expect(product2.reload.classifications.last.position).to eq(2)
+ positions = [
+ product.reload.classifications.find_by(taxon: category).position,
+ product2.reload.classifications.find_by(taxon: category).position
+ ]
+ expect(positions).to contain_exactly(1, 2)
end
it 'touches the products' do
diff --git a/core/app/subscribers/spree/product_metrics_subscriber.rb b/core/app/subscribers/spree/product_metrics_subscriber.rb
index 2813cfcc90..854da77f29 100644
--- a/core/app/subscribers/spree/product_metrics_subscriber.rb
+++ b/core/app/subscribers/spree/product_metrics_subscriber.rb
@@ -13,16 +13,16 @@ module Spree
def refresh_product_metrics(event)
order_id = event.payload['id']
- store_id = event.payload['store_id']
- return unless order_id && store_id
+ return unless order_id
- order = Spree::Order.find_by(id: order_id)
+ order = Spree::Order.find_by_param(order_id)
return unless order
+ return unless order.store_id
product_ids = order.line_items.includes(:variant).map { |li| li.variant.product_id }.uniq
return if product_ids.empty?
- jobs = product_ids.map { |product_id| Spree::Products::RefreshMetricsJob.new(product_id, store_id) }
+ jobs = product_ids.map { |product_id| Spree::Products::RefreshMetricsJob.new(product_id, order.store_id) }
ActiveJob.perform_all_later(jobs)
end
end
diff --git a/core/spec/subscribers/spree/product_metrics_subscriber_spec.rb b/core/spec/subscribers/spree/product_metrics_subscriber_spec.rb
index b9b13c79eb..a0ce9653e4 100644
--- a/core/spec/subscribers/spree/product_metrics_subscriber_spec.rb
+++ b/core/spec/subscribers/spree/product_metrics_subscriber_spec.rb
@@ -65,11 +65,17 @@ RSpec.describe Spree::ProductMetricsSubscriber do
end
end
- context 'when store_id is missing' do
+ context 'when order has no store' do
+ let(:order_without_store) do
+ create(:completed_order_with_totals, store: store).tap do |o|
+ o.update_column(:store_id, nil)
+ end
+ end
+
let(:event) do
Spree::Event.new(
name: 'order.completed',
- payload: { 'id' => order.id }
+ payload: { 'id' => order_without_store.id }
)
end