public_link_updates_spec.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Subscriptions::PublicLinkUpdates, type: :graphql do
  4. let(:mock_channel) { build_mock_channel }
  5. let(:variables) { { screen: 'login' } }
  6. let(:subscription) do
  7. <<~QUERY
  8. subscription publicLinkUpdates($screen: EnumPublicLinksScreen!) {
  9. publicLinkUpdates(screen: $screen) {
  10. publicLinks {
  11. id
  12. link
  13. title
  14. description
  15. newTab
  16. }
  17. }
  18. }
  19. QUERY
  20. end
  21. let!(:link_list) do
  22. first_link = create(:public_link, prio: 1)
  23. second_link = create(:public_link, prio: 2)
  24. third_link = create(:public_link, prio: 3)
  25. {
  26. first: first_link,
  27. second: second_link,
  28. third: third_link,
  29. }
  30. end
  31. let(:expected_result) do
  32. [
  33. {
  34. 'id' => gql.id(link_list[:first]),
  35. 'link' => link_list[:first]['link'],
  36. 'title' => link_list[:first]['title'],
  37. 'description' => link_list[:first]['description'],
  38. 'newTab' => link_list[:first]['new_tab'],
  39. },
  40. {
  41. 'id' => gql.id(link_list[:second]),
  42. 'link' => link_list[:second]['link'],
  43. 'title' => link_list[:second]['title'],
  44. 'description' => link_list[:second]['description'],
  45. 'newTab' => link_list[:second]['new_tab'],
  46. },
  47. {
  48. 'id' => gql.id(link_list[:third]),
  49. 'link' => link_list[:third]['link'],
  50. 'title' => link_list[:third]['title'],
  51. 'description' => link_list[:third]['description'],
  52. 'newTab' => link_list[:third]['new_tab'],
  53. },
  54. ]
  55. end
  56. before do
  57. gql.execute(subscription, variables: variables, context: { channel: mock_channel })
  58. end
  59. context 'when subscribed' do
  60. it 'subscribes' do
  61. expect(gql.result.data).to eq({ 'publicLinks' => nil })
  62. end
  63. it 'receives public link updates' do
  64. link_list[:second].update!(title: 'dummy')
  65. expect(mock_channel.mock_broadcasted_messages.first.dig(:result, 'data', 'publicLinkUpdates', 'publicLinks')[1]['title']).to eq('dummy')
  66. end
  67. it 'receives updates whenever a public link was created' do
  68. create(:public_link, prio: 4)
  69. expect(mock_channel.mock_broadcasted_messages.first.dig(:result, 'data', 'publicLinkUpdates', 'publicLinks').size).to eq(4)
  70. end
  71. it 'receives updates whenever a public link was deleted' do
  72. link_list[:second].destroy!
  73. expect(mock_channel.mock_broadcasted_messages.first.dig(:result, 'data', 'publicLinkUpdates', 'publicLinks').size).to eq(2)
  74. end
  75. end
  76. end