2016-04-01 05:33:25 +08:00
|
|
|
require 'rails_helper'
|
2014-07-23 07:02:22 +08:00
|
|
|
require_dependency 'discourse_event'
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
describe DiscourseEvent do
|
|
|
|
|
2013-02-13 12:45:10 +08:00
|
|
|
describe "#events" do
|
|
|
|
it "defaults to {}" do
|
|
|
|
DiscourseEvent.instance_variable_set(:@events, nil)
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(DiscourseEvent.events).to eq({})
|
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
|
|
|
|
|
|
|
|
describe ".clear" do
|
|
|
|
it "clears out events" do
|
|
|
|
DiscourseEvent.events["event42"] << "test event"
|
|
|
|
DiscourseEvent.clear
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(DiscourseEvent.events).to be_empty
|
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 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
|
|
|
|
|
|
|
before do
|
|
|
|
DiscourseEvent.on(:acid_face) do |user|
|
|
|
|
user.name = 'Two Face'
|
|
|
|
end
|
|
|
|
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
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
before do
|
|
|
|
DiscourseEvent.on(:acid_face) do |user|
|
|
|
|
user.job = 'Supervillian'
|
2013-02-13 12:45:10 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
DiscourseEvent.trigger(:acid_face, harvey)
|
|
|
|
end
|
|
|
|
|
2013-02-13 12:45:10 +08:00
|
|
|
it 'triggers both events' do
|
2015-01-10 00:34:37 +08:00
|
|
|
expect(harvey.job).to eq('Supervillian')
|
|
|
|
expect(harvey.name).to eq('Two Face')
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-02-13 12:45:10 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|