template_updates_spec.rb 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Subscriptions::TemplateUpdates, :aggregate_failures, authenticated_as: :agent, type: :graphql do
  4. let(:mock_channel) { build_mock_channel }
  5. let(:only_active_mock_channel) { build_mock_channel }
  6. let!(:template) { create(:template) }
  7. let(:agent) { create(:agent) }
  8. let(:subscription) do
  9. <<~QUERY
  10. subscription templateUpdates($onlyActive: Boolean) {
  11. templateUpdates(onlyActive: $onlyActive) {
  12. templates {
  13. name
  14. }
  15. }
  16. }
  17. QUERY
  18. end
  19. before do
  20. gql.execute(subscription, context: { channel: mock_channel })
  21. gql.execute(subscription, variables: { onlyActive: true }, context: { channel: only_active_mock_channel })
  22. end
  23. context 'when subscribed' do
  24. it 'subscribes' do
  25. expect(gql.result.data).to eq({ 'templates' => nil })
  26. end
  27. it 'receives template updates' do
  28. template.active = false
  29. template.save!
  30. expect(mock_channel.mock_broadcasted_messages.first.dig(:result, 'data', 'templateUpdates', 'templates')).to eq(['name' => template.name])
  31. expect(only_active_mock_channel.mock_broadcasted_messages.first.dig(:result, 'data', 'templateUpdates', 'templates')).to eq([])
  32. end
  33. it 'receives updates whenever a template was created' do
  34. create(:template, active: false)
  35. expect(mock_channel.mock_broadcasted_messages.first.dig(:result, 'data', 'templateUpdates', 'templates').size).to be(2)
  36. expect(only_active_mock_channel.mock_broadcasted_messages.first.dig(:result, 'data', 'templateUpdates', 'templates').size).to be(1)
  37. end
  38. it 'receives updates whenever a template was deleted' do
  39. template.destroy!
  40. expect(mock_channel.mock_broadcasted_messages.first.dig(:result, 'data', 'templateUpdates', 'templates')).to eq([])
  41. expect(only_active_mock_channel.mock_broadcasted_messages.first.dig(:result, 'data', 'templateUpdates', 'templates')).to eq([])
  42. end
  43. end
  44. end