2019-04-29 19:27:42 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 21:27:38 -05:00
|
|
|
RSpec.describe DiscourseEvent do
|
2013-02-12 22:45:10 -06:00
|
|
|
describe "#events" do
|
|
|
|
it "defaults to {}" do
|
2016-09-05 05:14:28 -05:00
|
|
|
begin
|
|
|
|
original_events = DiscourseEvent.events
|
|
|
|
DiscourseEvent.instance_variable_set(:@events, nil)
|
|
|
|
expect(DiscourseEvent.events).to eq({})
|
|
|
|
ensure
|
|
|
|
DiscourseEvent.instance_variable_set(:@events, original_events)
|
|
|
|
end
|
2013-02-12 22:45:10 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "key value" do
|
|
|
|
it "defaults to an empty set" do
|
2015-01-09 10:34:37 -06:00
|
|
|
expect(DiscourseEvent.events["event42"]).to eq(Set.new)
|
2013-02-12 22:45:10 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
context "when calling events" do
|
|
|
|
let(:harvey) { OpenStruct.new(name: "Harvey Dent", job: "District Attorney") }
|
2020-11-11 13:46:13 -06:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
let(:event_handler) { Proc.new { |user| user.name = "Two Face" } }
|
2020-11-11 13:46:13 -06:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
before { DiscourseEvent.on(:acid_face, &event_handler) }
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
after { DiscourseEvent.off(:acid_face, &event_handler) }
|
|
|
|
|
|
|
|
context "when event does not exist" do
|
2013-02-12 22:45:10 -06:00
|
|
|
it "does not raise an error" do
|
|
|
|
DiscourseEvent.trigger(:missing_event)
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
context "when single event exists" do
|
2013-02-12 22:45:10 -06:00
|
|
|
it "doesn't raise an error" do
|
|
|
|
DiscourseEvent.trigger(:acid_face, harvey)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "changes the name" do
|
|
|
|
DiscourseEvent.trigger(:acid_face, harvey)
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(harvey.name).to eq("Two Face")
|
2013-02-12 22:45:10 -06:00
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
context "when multiple events exist" do
|
|
|
|
let(:event_handler_2) { Proc.new { |user| user.job = "Supervillain" } }
|
2013-02-12 22:45:10 -06:00
|
|
|
|
2020-11-11 13:46:13 -06:00
|
|
|
before do
|
|
|
|
DiscourseEvent.on(:acid_face, &event_handler_2)
|
2013-02-05 13:16:51 -06:00
|
|
|
DiscourseEvent.trigger(:acid_face, harvey)
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
after { DiscourseEvent.off(:acid_face, &event_handler_2) }
|
2020-11-11 13:46:13 -06:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "triggers both events" do
|
|
|
|
expect(harvey.job).to eq("Supervillain")
|
|
|
|
expect(harvey.name).to eq("Two Face")
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
describe "#all_off" do
|
|
|
|
let(:event_handler_2) { Proc.new { |user| user.job = "Supervillain" } }
|
2021-06-22 13:00:04 -05:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
before { DiscourseEvent.on(:acid_face, &event_handler_2) }
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "removes all handlers with a key" do
|
|
|
|
harvey.job = "gardening"
|
2021-06-22 13:00:04 -05:00
|
|
|
DiscourseEvent.all_off(:acid_face)
|
|
|
|
DiscourseEvent.trigger(:acid_face, harvey) # Doesn't change anything
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(harvey.job).to eq("gardening")
|
2021-06-22 13:00:04 -05:00
|
|
|
end
|
2023-01-09 04:26:39 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows using kwargs" do
|
|
|
|
begin
|
2023-01-09 05:18:21 -06:00
|
|
|
handler =
|
|
|
|
Proc.new do |name:, message:|
|
|
|
|
expect(name).to eq("Supervillain")
|
|
|
|
expect(message).to eq("Two Face")
|
|
|
|
end
|
2021-06-22 13:00:04 -05:00
|
|
|
|
2023-01-09 04:26:39 -06:00
|
|
|
DiscourseEvent.on(:acid_face, &handler)
|
|
|
|
DiscourseEvent.trigger(:acid_face, name: "Supervillain", message: "Two Face")
|
|
|
|
ensure
|
|
|
|
DiscourseEvent.off(:acid_face, &handler)
|
2021-06-22 13:00:04 -05:00
|
|
|
end
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|