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
2 people authored and Noah-Silvera committed Aug 26, 2022
1 parent ff8a77e commit 10a5cee
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 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
41 changes: 37 additions & 4 deletions spec/models/spree/order_updater_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 10a5cee

Please sign in to comment.