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