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