chat_agent_state_spec.rb 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. require 'rails_helper'
  2. RSpec.describe Sessions::Event::ChatAgentState do
  3. let(:client_id) { rand(123_456_789) }
  4. let(:chat) { Chat.first }
  5. let(:user) do
  6. create(:agent, preferences: {
  7. chat: {
  8. active: {
  9. chat.id.to_s => 'on'
  10. }
  11. }
  12. })
  13. end
  14. let!(:instance) do
  15. Sessions.create(client_id, { 'id' => user.id }, {})
  16. Sessions.queue(client_id)
  17. described_class.new(
  18. payload: {
  19. 'data' => {
  20. 'active' => active
  21. },
  22. },
  23. user_id: user.id,
  24. client_id: client_id,
  25. clients: {},
  26. session: {
  27. 'id' => user.id
  28. },
  29. )
  30. end
  31. let(:record) { create(:'chat/agent', updated_by: user) }
  32. before do
  33. Setting.set('chat', true)
  34. end
  35. context 'when state changes' do
  36. let(:active) { !record.active }
  37. it 'broadcasts agent state update' do
  38. allow(Chat).to receive(:broadcast_agent_state_update)
  39. instance.run
  40. expect(Chat).to have_received(:broadcast_agent_state_update)
  41. end
  42. end
  43. context "when state doesn't change" do
  44. let(:active) { record.active }
  45. it "doesn't broadcasts agent state update" do
  46. allow(Chat).to receive(:broadcast_agent_state_update)
  47. instance.run
  48. expect(Chat).not_to have_received(:broadcast_agent_state_update)
  49. end
  50. end
  51. end