require 'nuge/pusher' require 'nuge/client' describe Nuge::Pusher do before do @clients = Nuge::Pusher.clients end after do Nuge::Pusher.clients = @clients end describe '.clients' do it 'is an empty set if it has not been defined' do expect(Nuge::Pusher.clients).to be_empty end end describe '.clients=' do it 'persists client instances at the class level' do client = Nuge::Client.new Nuge::Pusher.clients = [client] expect(Nuge::Pusher.clients).to include(client) expect(Nuge::Pusher.new.clients).to include(client) end end describe '#clients' do it 'defaults to an empty set of clients' do expect(Nuge::Pusher.new).to respond_to(:clients) expect(Nuge::Pusher.new.clients).to be_empty end it 'accepts changes to the client set' do nuge = Nuge::Pusher.new client = Nuge::Client.new nuge.clients << client expect(nuge.clients).to include(client) end end describe 'forwarding' do it 'forwards client methods to all instantiated clients' do nuge = Nuge::Pusher.new([double(:client), double(:client)]) nuge.clients.each do |client| expect(client).to receive(:push).with(['1234'], message: 'hello') expect(client).to receive(:register).with('1234', option: true) expect(client).to receive(:unregister).with('1234', option: true) end nuge.push(['1234'], message: 'hello') nuge.register('1234', option: true) nuge.unregister('1234', option: true) end end describe 'expanders' do it 'applies an identity function against id objects by default' do nuge = Nuge::Pusher.new expect(nuge.expanded('abc')).to eq(['abc']) end it 'applies expansion against the all supplied id objects' do nuge = Nuge::Pusher.new nuge.expander = -> (obj) { obj[:token] } reg_a = { token: 'abc' } reg_b = { token: 'def' } expect(nuge.expanded(reg_a)).to eq(%w[abc]) expect(nuge.expanded(reg_a, reg_b)).to eq(%w[abc def]) expect(nuge.expanded([reg_a, reg_b])).to eq(%w[abc def]) end end end