2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2014-07-22 19:02:22 -04:00
|
|
|
require_dependency 'discourse_event'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
describe DiscourseEvent do
|
|
|
|
|
2013-02-12 23:45:10 -05:00
|
|
|
describe "#events" do
|
|
|
|
it "defaults to {}" do
|
2016-09-05 06:14:28 -04: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 23:45:10 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "key value" do
|
|
|
|
it "defaults to an empty set" do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(DiscourseEvent.events["event42"]).to eq(Set.new)
|
2013-02-12 23:45:10 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when calling events' do
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2013-02-12 23:45:10 -05:00
|
|
|
let(:harvey) {
|
|
|
|
OpenStruct.new(
|
|
|
|
name: 'Harvey Dent',
|
|
|
|
job: 'District Attorney'
|
|
|
|
)
|
|
|
|
}
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
before do
|
|
|
|
DiscourseEvent.on(:acid_face) do |user|
|
|
|
|
user.name = 'Two Face'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-12 23:45:10 -05:00
|
|
|
context 'when event does not exist' do
|
|
|
|
|
|
|
|
it "does not raise an error" do
|
|
|
|
DiscourseEvent.trigger(:missing_event)
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-12 23:45:10 -05: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-09 11:34:37 -05:00
|
|
|
expect(harvey.name).to eq('Two Face')
|
2013-02-12 23:45:10 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-12 23:45:10 -05:00
|
|
|
context 'when multiple events exist' do
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
before do
|
|
|
|
DiscourseEvent.on(:acid_face) do |user|
|
2017-07-27 21:20:09 -04:00
|
|
|
user.job = 'Supervillian'
|
2013-02-12 23:45:10 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
DiscourseEvent.trigger(:acid_face, harvey)
|
|
|
|
end
|
|
|
|
|
2013-02-12 23:45:10 -05:00
|
|
|
it 'triggers both events' do
|
2015-01-09 11:34:37 -05:00
|
|
|
expect(harvey.job).to eq('Supervillian')
|
|
|
|
expect(harvey.name).to eq('Two Face')
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-12 23:45:10 -05:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|