Skip to content

Commit

Permalink
Update specs for backported event
Browse files Browse the repository at this point in the history
The specs here were specific to the legacy implementation of the event
system, and therefore no longer pass on the `master` branch of Solidus
where we now use a [new implementation of the event bus](solidusio/solidus#4342).
This change attempts to refactor the tests to focus on observing the
side-effects of the object under test which should be independent of the
underlying implementation.

This test is valuable regardless of what version of Solidus we are running
against as our extension relies on the event being published when a specific
action happens and that the payload contains the necessary object.

Co-authored-by: Andrew Stewart <andrew@super.gd>
  • Loading branch information
forkata and stewart committed Jun 25, 2022
1 parent 8cb849a commit 586116f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions lib/super_good/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
28 changes: 26 additions & 2 deletions spec/models/spree/order_updater_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,36 @@
RSpec.describe Spree::OrderUpdater do
describe '#update' do

module TestSubscriber
include Spree::Event::Subscriber
include SolidusSupport::LegacyEventCompat::Subscriber

event_action :order_recalculated

def order_recalculated(_event)
end
end

before do
if Spree::Config.use_legacy_events
TestSubscriber.subscribe!
else
TestSubscriber.omnes_subscriber.subscribe_to(Spree::Bus)
end
end

it 'fires the order_recalculated event exactly once' do
stub_const('Spree::Event', class_spy(Spree::Event))
allow(TestSubscriber).to receive(:order_recalculated)

order = create(:order)

described_class.new(order).update

expect(Spree::Event).to have_received(:fire).with('order_recalculated', order: order).once
expect(TestSubscriber).to have_received(:order_recalculated)
.with(instance_of(Omnes::UnstructuredEvent))
.once do |event|
expect(event[:order]).to eq(order)
end
end
end
end

0 comments on commit 586116f

Please sign in to comment.