1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
describe Wisper::Publisher do
let(:listener) { double('listener') }
let(:publisher) { publisher_class.new }
describe '.subscribe' do
it 'subscribes given listener to all published events' do
expect(listener).to receive(:this_happened)
expect(listener).to receive(:so_did_this)
publisher.subscribe(listener)
publisher.send(:broadcast, 'this_happened')
publisher.send(:broadcast, 'so_did_this')
end
describe ':on argument' do
before do
allow(listener).to receive(:something_a_happened)
allow(listener).to receive(:and_this)
allow(listener).to receive(:so_did_this)
end
describe 'given a string' do
it 'subscribes listener to an event' do
expect(listener).to receive(:this_happened)
expect(listener).not_to receive(:so_did_this)
publisher.subscribe(listener, on: 'this_happened')
publisher.send(:broadcast, 'this_happened')
publisher.send(:broadcast, 'so_did_this')
end
end
describe 'given a symbol' do
it 'subscribes listener to an event' do
expect(listener).to receive(:this_happened)
expect(listener).not_to receive(:so_did_this)
publisher.subscribe(listener, on: :this_happened)
publisher.send(:broadcast, 'this_happened')
publisher.send(:broadcast, 'so_did_this')
end
end
describe 'given an array' do
it 'subscribes listener to events' do
expect(listener).to receive(:this_happened)
expect(listener).to receive(:and_this)
expect(listener).not_to receive(:so_did_this)
publisher.subscribe(listener, on: ['this_happened', 'and_this'])
publisher.send(:broadcast, 'this_happened')
publisher.send(:broadcast, 'so_did_this')
publisher.send(:broadcast, 'and_this')
end
end
describe 'given a regex' do
it 'subscribes listener to matching events' do
expect(listener).to receive(:something_a_happened)
expect(listener).not_to receive(:so_did_this)
publisher.subscribe(listener, on: /something_._happened/)
publisher.send(:broadcast, 'something_a_happened')
publisher.send(:broadcast, 'so_did_this')
end
end
describe 'given an unsupported argument' do
it 'raises an error' do
publisher.subscribe(listener, on: Object.new)
expect { publisher.send(:broadcast, 'something_a_happened') }.to raise_error(ArgumentError)
end
end
end
describe ':with argument' do
it 'sets method to call listener with on event' do
expect(listener).to receive(:different_method).twice
publisher.subscribe(listener, :with => :different_method)
publisher.send(:broadcast, 'this_happened')
publisher.send(:broadcast, 'so_did_this')
end
end
describe ':prefix argument' do
it 'prefixes broadcast events with given symbol' do
expect(listener).to receive(:after_it_happened)
expect(listener).not_to receive(:it_happened)
publisher.subscribe(listener, :prefix => :after)
publisher.send(:broadcast, 'it_happened')
end
it 'prefixes broadcast events with "on" when given true' do
expect(listener).to receive(:on_it_happened)
expect(listener).not_to receive(:it_happened)
publisher.subscribe(listener, :prefix => true)
publisher.send(:broadcast, 'it_happened')
end
end
describe ':scope argument' do
let(:listener_1) { double('Listener') }
let(:listener_2) { double('Listener') }
it 'scopes listener to given class' do
expect(listener_1).to receive(:it_happened)
expect(listener_2).not_to receive(:it_happened)
publisher.subscribe(listener_1, :scope => publisher.class)
publisher.subscribe(listener_2, :scope => Class.new)
publisher.send(:broadcast, 'it_happened')
end
it 'scopes listener to given class string' do
expect(listener_1).to receive(:it_happened)
expect(listener_2).not_to receive(:it_happened)
publisher.subscribe(listener_1, :scope => publisher.class.to_s)
publisher.subscribe(listener_2, :scope => Class.new.to_s)
publisher.send(:broadcast, 'it_happened')
end
it 'includes all subclasses of given class' do
publisher_super_klass = publisher_class
publisher_sub_klass = Class.new(publisher_super_klass)
listener = double('Listener')
expect(listener).to receive(:it_happened).once
publisher = publisher_sub_klass.new
publisher.subscribe(listener, :scope => publisher_super_klass)
publisher.send(:broadcast, 'it_happened')
end
end
describe ':broadcaster argument'do
let(:broadcaster) { double('broadcaster') }
let(:listener) { double('listener') }
let(:event_name) { 'it_happened' }
before do
Wisper.configuration.broadcasters.clear
allow(listener).to receive(event_name)
allow(broadcaster).to receive(:broadcast)
end
after { Wisper.setup } # restore default configuration
it 'given an object which responds_to broadcast it uses object' do
publisher.subscribe(listener, broadcaster: broadcaster)
expect(broadcaster).to receive('broadcast')
publisher.send(:broadcast, event_name)
end
it 'given a key it uses a configured broadcaster' do
Wisper.configure { |c| c.broadcaster(:foobar, broadcaster) }
publisher.subscribe(listener, broadcaster: :foobar)
expect(broadcaster).to receive('broadcast')
publisher.send(:broadcast, event_name)
end
it 'given an unknown key it raises error' do
expect { publisher.subscribe(listener, broadcaster: :foobar) }.to raise_error(KeyError, /broadcaster not found/)
end
it 'given nothing it uses the default broadcaster' do
Wisper.configure { |c| c.broadcaster(:default, broadcaster) }
publisher.subscribe(listener)
expect(broadcaster).to receive('broadcast')
publisher.send(:broadcast, event_name)
end
describe 'async alias' do
it 'given an object which responds_to broadcast it uses object' do
publisher.subscribe(listener, async: broadcaster)
expect(broadcaster).to receive('broadcast')
publisher.send(:broadcast, event_name)
end
it 'given true it uses configured async broadcaster' do
Wisper.configure { |c| c.broadcaster(:async, broadcaster) }
publisher.subscribe(listener, async: true)
expect(broadcaster).to receive('broadcast')
publisher.send(:broadcast, event_name)
end
it 'given false it uses configured default broadcaster' do
Wisper.configure { |c| c.broadcaster(:default, broadcaster) }
publisher.subscribe(listener, async: false)
expect(broadcaster).to receive('broadcast')
publisher.send(:broadcast, event_name)
end
end
end
it 'returns publisher so methods can be chained' do
expect(publisher.subscribe(listener, :on => 'so_did_this')).to \
eq publisher
end
it 'is aliased to .subscribe' do
expect(publisher).to respond_to(:subscribe)
end
it 'raises a helpful error if trying to pass a block' do
invalid = ->{
publisher.subscribe(:success) do
puts
end
}
expect{ invalid.call }.to raise_error(ArgumentError)
end
end
describe '.on' do
it 'returns publisher so methods can be chained' do
expect(publisher.on('this_thing_happened') {}).to eq publisher
end
it 'raise an error if no events given' do
expect { publisher.on() {} }.to raise_error(ArgumentError)
end
it 'raises an error of no block given' do
expect { publisher.on(:something) }.to raise_error(ArgumentError)
end
it 'returns publisher so methods can be chained' do
expect(publisher.on(:foo) {}).to eq publisher
end
end
describe '.broadcast' do
it 'does not publish events which cannot be responded to' do
expect(listener).not_to receive(:so_did_this)
allow(listener).to receive(:respond_to?).and_return(false)
publisher.subscribe(listener, :on => 'so_did_this')
publisher.send(:broadcast, 'so_did_this')
end
describe ':event argument' do
it 'is indifferent to string and symbol' do
expect(listener).to receive(:this_happened).twice
publisher.subscribe(listener)
publisher.send(:broadcast, 'this_happened')
publisher.send(:broadcast, :this_happened)
end
it 'is indifferent to dasherized and underscored strings' do
expect(listener).to receive(:this_happened).twice
publisher.subscribe(listener)
publisher.send(:broadcast, 'this_happened')
publisher.send(:broadcast, 'this-happened')
end
end
it 'returns publisher' do
expect(publisher.send(:broadcast, :foo)).to eq publisher
end
it 'is not public' do
expect(publisher).not_to respond_to(:broadcast)
end
it 'is alised as .publish' do
expect(publisher.method(:broadcast)).to eq publisher.method(:publish)
end
end
describe '.listeners' do
it 'returns an immutable collection' do
expect(publisher.listeners).to be_frozen
expect { publisher.listeners << listener }.to raise_error(RuntimeError)
end
it 'returns local listeners' do
publisher.subscribe(listener)
expect(publisher.listeners).to eq [listener]
expect(publisher.listeners.size).to eq 1
end
end
describe '#subscribe' do
let(:publisher_klass_1) { publisher_class }
let(:publisher_klass_2) { publisher_class }
it 'subscribes listener to all instances of publisher' do
publisher_klass_1.subscribe(listener)
expect(listener).to receive(:it_happened).once
publisher_klass_1.new.send(:broadcast, 'it_happened')
publisher_klass_2.new.send(:broadcast, 'it_happened')
end
end
end
|