discourse/vendor/gems/discourse_plugin/spec/discourse_event_spec.rb

84 lines
1.6 KiB
Ruby
Raw Normal View History

2013-02-05 14:16:51 -05:00
require 'spec_helper'
require 'discourse_event'
require 'ostruct'
describe DiscourseEvent do
2013-02-12 23:45:10 -05:00
describe "#events" do
it "defaults to {}" do
DiscourseEvent.instance_variable_set(:@events, nil)
DiscourseEvent.events.should == {}
end
describe "key value" do
it "defaults to an empty set" do
DiscourseEvent.events["event42"].should == Set.new
end
end
end
describe ".clear" do
it "clears out events" do
DiscourseEvent.events["event42"] << "test event"
DiscourseEvent.clear
DiscourseEvent.events.should be_empty
end
2013-02-05 14:16:51 -05:00
end
2013-02-12 23:45:10 -05:00
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)
harvey.name.should == 'Two Face'
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|
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
2013-02-05 14:16:51 -05:00
harvey.job.should == 'Supervillian'
harvey.name.should == 'Two Face'
end
2013-02-12 23:45:10 -05:00
2013-02-05 14:16:51 -05:00
end
end
end