online_notifications_count_spec.rb 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Subscriptions::OnlineNotificationsCount, authenticated_as: :agent, type: :graphql do
  4. let(:agent) { create(:agent) }
  5. let(:notification) { create(:online_notification, user_id: agent.id) }
  6. let(:mock_channel) { build_mock_channel }
  7. let(:subscription) do
  8. <<~QUERY
  9. subscription onlineNotificationsCount {
  10. onlineNotificationsCount {
  11. unseenCount
  12. }
  13. }
  14. QUERY
  15. end
  16. before do
  17. notification
  18. gql.execute(subscription, context: { channel: mock_channel })
  19. end
  20. context 'with an agent' do
  21. context 'with matching user' do
  22. let(:user) { agent }
  23. before { travel 10.minutes }
  24. it 'subscribes' do
  25. expect(gql.result.data).to eq({ 'unseenCount' => 1 })
  26. end
  27. it 'receives update when new notification created' do
  28. create(:online_notification, user_id: agent.id)
  29. expect(mock_channel.mock_broadcasted_first.data).to include('unseenCount' => 2)
  30. end
  31. it 'receives update when existing notification marked as seen' do
  32. notification.update! seen: true
  33. expect(mock_channel.mock_broadcasted_first.data).to include('unseenCount' => 0)
  34. end
  35. end
  36. end
  37. end