Module: Upkeep::Rails::Testing
- Defined in:
- lib/upkeep/rails/testing.rb
Overview
Test helpers for asserting the public Upkeep subscription lifecycle from Rails request, integration, and system tests.
Class Method Summary collapse
-
.drain_delivery! ⇒ void
Drains the async delivery dispatcher when a test needs deterministic broadcast assertions.
Instance Method Summary collapse
-
#activate_upkeep_subscription!(subscription = upkeep_subscription) ⇒ Upkeep::Subscriptions::Subscription
Activates the registered subscription so delivery lookup can find it.
-
#assert_upkeep_subscription_registered(message = nil) ⇒ void
Asserts that the last successful HTML response injected an Upkeep subscription marker and registered a subscription in the configured store.
-
#capture_upkeep_broadcasts(subscription = upkeep_subscription, &block) ⇒ Array<String>
Captures ActionCable broadcasts for every stream associated with a subscription while the block runs.
-
#drain_upkeep_delivery! ⇒ void
Drains async Upkeep delivery for deterministic test assertions.
-
#upkeep_stream_names(subscription = upkeep_subscription) ⇒ Array<String>
Returns every ActionCable stream name that can receive broadcasts for a subscription, including shared streams.
-
#upkeep_subscription ⇒ Upkeep::Subscriptions::Subscription?
Returns the most recently registered Upkeep subscription.
Class Method Details
.drain_delivery! ⇒ void
This method returns an undefined value.
Drains the async delivery dispatcher when a test needs deterministic broadcast assertions.
Production code should not call this; normal app delivery runs through the configured adapter.
16 17 18 |
# File 'lib/upkeep/rails/testing.rb', line 16 def drain_delivery! Upkeep::Rails.send(:drain_delivery_dispatcher!) end |
Instance Method Details
#activate_upkeep_subscription!(subscription = upkeep_subscription) ⇒ Upkeep::Subscriptions::Subscription
Activates the registered subscription so delivery lookup can find it.
58 59 60 61 62 63 64 65 |
# File 'lib/upkeep/rails/testing.rb', line 58 def activate_upkeep_subscription!(subscription = upkeep_subscription) raise ArgumentError, "no Upkeep subscription is registered" unless subscription activated = Upkeep::Rails.subscriptions.activate(subscription.id) raise Upkeep::Subscriptions::NotFound, subscription.id unless activated subscription end |
#assert_upkeep_subscription_registered(message = nil) ⇒ void
This method returns an undefined value.
Asserts that the last successful HTML response injected an Upkeep subscription marker and registered a subscription in the configured store.
27 28 29 30 31 |
# File 'lib/upkeep/rails/testing.rb', line 27 def assert_upkeep_subscription_registered( = nil) assert_select "upkeep-subscription-source[data-upkeep-subscription]" assert Upkeep::Rails.subscriptions.subscriptions.any?, || "expected Upkeep to register at least one subscription" end |
#capture_upkeep_broadcasts(subscription = upkeep_subscription, &block) ⇒ Array<String>
Captures ActionCable broadcasts for every stream associated with a subscription while the block runs.
Include ActionCable::TestHelper before calling this helper.
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/upkeep/rails/testing.rb', line 76 def capture_upkeep_broadcasts(subscription = upkeep_subscription, &block) raise ArgumentError, "capture_upkeep_broadcasts requires a block" unless block raise NoMethodError, "include ActionCable::TestHelper before calling capture_upkeep_broadcasts" unless respond_to?(:capture_broadcasts) captures = {} nested = upkeep_stream_names(subscription).reverse_each.reduce(block) do |inner, stream_name| proc { captures[stream_name] = capture_broadcasts(stream_name, &inner) } end nested.call captures.values.flatten end |
#drain_upkeep_delivery! ⇒ void
This method returns an undefined value.
Drains async Upkeep delivery for deterministic test assertions.
92 93 94 |
# File 'lib/upkeep/rails/testing.rb', line 92 def drain_upkeep_delivery! Upkeep::Rails::Testing.drain_delivery! end |
#upkeep_stream_names(subscription = upkeep_subscription) ⇒ Array<String>
Returns every ActionCable stream name that can receive broadcasts for a subscription, including shared streams.
46 47 48 49 50 |
# File 'lib/upkeep/rails/testing.rb', line 46 def upkeep_stream_names(subscription = upkeep_subscription) raise ArgumentError, "no Upkeep subscription is registered" unless subscription ([subscription..fetch(:stream_name)] + subscription..fetch(:shared_stream_names, [])).uniq end |
#upkeep_subscription ⇒ Upkeep::Subscriptions::Subscription?
Returns the most recently registered Upkeep subscription.
36 37 38 |
# File 'lib/upkeep/rails/testing.rb', line 36 def upkeep_subscription Upkeep::Rails.subscriptions.subscriptions.last end |