discourse/spec/support/discourse_event_helper.rb
Jarek Radosz ff508d1ae5
FIX: Correctly support DiscourseEvent kwargs (#19788)
Fixes the support for kwargs in `DiscourseEvent.trigger()` on Ruby 3, e.g.

```rb
DiscourseEvent.trigger(:before_system_message_sent, message_type: type, recipient: @recipient, post_creator_args: post_creator_args, params: method_params)
```

Fixes https://github.com/discourse/discourse-local-site-contacts
2023-01-09 11:26:39 +01:00

36 lines
841 B
Ruby

# frozen_string_literal: true
module DiscourseEvent::TestHelper
def trigger(event_name, *params, **kwargs)
super(event_name, *params, **kwargs)
if @events_trigger
params << kwargs if kwargs != {}
@events_trigger << { event_name: event_name, params: params }
end
end
def track_events(event_name = nil, args: nil)
@events_trigger = events_trigger = []
yield
@events_trigger = nil
if event_name
events_trigger = events_trigger.filter do |event|
next if event[:event_name] != event_name
next if args && event[:params] != args
true
end
end
events_trigger
end
def track(event_name, args: nil)
events = track_events(event_name, args: args) { yield }
events.first
end
end
DiscourseEvent.singleton_class.prepend DiscourseEvent::TestHelper