diff --git a/app/overrides/super_good/solidus_taxjar/spree/order_updater/fire_recalculated_event.rb b/app/overrides/super_good/solidus_taxjar/spree/order_updater/fire_recalculated_event.rb index 1e169bc3..df66124e 100644 --- a/app/overrides/super_good/solidus_taxjar/spree/order_updater/fire_recalculated_event.rb +++ b/app/overrides/super_good/solidus_taxjar/spree/order_updater/fire_recalculated_event.rb @@ -4,6 +4,10 @@ module SuperGood module SolidusTaxjar module Spree module OrderUpdater + # Responsible for introducing the `order_recalculated` event for older + # versions of Solidus (from 2.9 up until but not including 2.11) which + # do not have this event. TaxJar transaction reporting relies on this + # event firing when an order is recalculated. module FireRecalculatedEvent def persist_totals ::Spree::Event.fire 'order_recalculated', order: order diff --git a/lib/super_good/engine.rb b/lib/super_good/engine.rb index 3d94c855..a1fcaa77 100644 --- a/lib/super_good/engine.rb +++ b/lib/super_good/engine.rb @@ -5,6 +5,9 @@ class Engine < Rails::Engine isolate_namespace Spree engine_name 'super_good_solidus_taxjar' + # Solidus versions prior to 2.11.0 do not support auto-loading of subscribers + # so we need to manually register the reporting subscriber with the event + # bus. if Spree.solidus_gem_version < Gem::Version.new('2.11.0') require root.join('app/subscribers/super_good/solidus_taxjar/spree/reporting_subscriber') SuperGood::SolidusTaxjar::Spree::ReportingSubscriber.subscribe! diff --git a/spec/models/spree/order_updater_spec.rb b/spec/models/spree/order_updater_spec.rb index 69ecdc93..2b627c7b 100644 --- a/spec/models/spree/order_updater_spec.rb +++ b/spec/models/spree/order_updater_spec.rb @@ -1,12 +1,45 @@ RSpec.describe Spree::OrderUpdater do describe '#update' do + subject { described_class.new(order).update } + + let(:order) { create(:order) } + + module TestSubscriber + include Spree::Event::Subscriber + include SolidusSupport::LegacyEventCompat::Subscriber + + event_action :order_recalculated + + def order_recalculated(_event) + end + end + + if SolidusSupport::LegacyEventCompat.using_legacy? + let(:event_type_class) { ActiveSupport::Notifications::Event } + + before do + if TestSubscriber.respond_to?(:subscribe!) + TestSubscriber.subscribe! + else + TestSubscriber.activate + end + end + else + let(:event_type_class) { Omnes::UnstructuredEvent } + + before { TestSubscriber.omnes_subscriber.subscribe_to(Spree::Bus) } + end + it 'fires the order_recalculated event exactly once' do - stub_const('Spree::Event', class_spy(Spree::Event)) - order = create(:order) + allow(TestSubscriber).to receive(:order_recalculated) - described_class.new(order).update + subject - expect(Spree::Event).to have_received(:fire).with('order_recalculated', order: order).once + expect(TestSubscriber).to have_received(:order_recalculated) + .with(instance_of(event_type_class)) + .once do |event| + expect(event.payload[:order]).to eq(order) + end end end end