online_notifications_count_spec.rb 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # Copyright (C) 2012-2024 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(:variables) { { userId: gql.id(user) } }
  7. let(:mock_channel) { build_mock_channel }
  8. let(:subscription) do
  9. <<~QUERY
  10. subscription onlineNotificationsCount($userId: ID!) {
  11. onlineNotificationsCount(userId: $userId) {
  12. unseenCount
  13. }
  14. }
  15. QUERY
  16. end
  17. before do
  18. notification
  19. gql.execute(subscription, variables: variables, context: { channel: mock_channel })
  20. end
  21. context 'with an agent' do
  22. context 'with matching user' do
  23. let(:user) { agent }
  24. before { travel 10.minutes }
  25. it 'subscribes' do
  26. expect(gql.result.data).to eq({ 'unseenCount' => 1 })
  27. end
  28. it 'receives update when new notification created' do
  29. create(:online_notification, user_id: agent.id)
  30. expect(mock_channel.mock_broadcasted_first.data).to include('unseenCount' => 2)
  31. end
  32. it 'receives update when existing notification marked as seen' do
  33. notification.update! seen: true
  34. expect(mock_channel.mock_broadcasted_first.data).to include('unseenCount' => 0)
  35. end
  36. end
  37. context 'with not matching user' do
  38. let(:user) { create(:agent) }
  39. it 'raises authorization error' do
  40. expect(gql.result.error_type).to eq(Exceptions::Forbidden)
  41. end
  42. end
  43. end
  44. end