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