2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-01 15:19:43 +08:00
|
|
|
module DiscourseEvent::TestHelper
|
2023-01-09 18:26:39 +08:00
|
|
|
def trigger(event_name, *params, **kwargs)
|
|
|
|
super(event_name, *params, **kwargs)
|
2017-06-01 15:19:43 +08:00
|
|
|
|
|
|
|
if @events_trigger
|
2023-01-09 18:26:39 +08:00
|
|
|
params << kwargs if kwargs != {}
|
2017-06-01 15:19:43 +08:00
|
|
|
@events_trigger << { event_name: event_name, params: params }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-22 09:06:02 +08:00
|
|
|
def track_events(event_name = nil, args: nil)
|
2017-06-01 15:19:43 +08:00
|
|
|
@events_trigger = events_trigger = []
|
|
|
|
yield
|
|
|
|
@events_trigger = nil
|
2022-07-22 09:06:02 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2017-06-01 15:19:43 +08:00
|
|
|
events_trigger
|
|
|
|
end
|
2019-01-04 01:03:01 +08:00
|
|
|
|
2022-07-22 09:06:02 +08:00
|
|
|
def track(event_name, args: nil)
|
|
|
|
events = track_events(event_name, args: args) { yield }
|
|
|
|
events.first
|
2019-01-04 01:03:01 +08:00
|
|
|
end
|
2017-06-01 15:19:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
DiscourseEvent.singleton_class.prepend DiscourseEvent::TestHelper
|