checklist_updates_spec.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Gql::Subscriptions::Ticket::ChecklistUpdates, type: :graphql do
  4. let(:group) { create(:group) }
  5. let(:agent) { create(:agent, groups: [group]) }
  6. let(:ticket) { create(:ticket, group: group) }
  7. let(:variables) { { ticketId: gql.id(ticket) } }
  8. let(:mock_channel) { build_mock_channel }
  9. let(:subscription) do
  10. <<~QUERY
  11. subscription ticketChecklistUpdates($ticketId: ID!) {
  12. ticketChecklistUpdates(ticketId: $ticketId) {
  13. ticketChecklist {
  14. id
  15. name
  16. completed
  17. incomplete
  18. items {
  19. id
  20. text
  21. checked
  22. ticket {
  23. id
  24. internalId
  25. number
  26. title
  27. state {
  28. name
  29. }
  30. stateColorCode
  31. }
  32. ticketAccess
  33. }
  34. }
  35. removedTicketChecklist
  36. }
  37. }
  38. QUERY
  39. end
  40. before do
  41. gql.execute(subscription, variables: variables, context: { channel: mock_channel })
  42. end
  43. context 'with an unauthenticated user' do
  44. it 'does not subscribe to checklist updates and returns an authorization error' do
  45. expect(gql.result.error_type).to eq(Exceptions::NotAuthorized)
  46. end
  47. end
  48. context 'with an authenticated user', authenticated_as: :agent do
  49. it 'subscribes to checklist updates' do
  50. expect(gql.result.data).not_to be_nil
  51. end
  52. it 'triggers after checklist create' do
  53. checklist = create(:checklist, ticket: ticket, item_count: nil)
  54. result = mock_channel.mock_broadcasted_messages.first[:result]['data']['ticketChecklistUpdates']
  55. expect(result).to include('ticketChecklist' => include(
  56. 'id' => gql.id(checklist),
  57. 'completed' => true,
  58. 'incomplete' => 0,
  59. ))
  60. end
  61. context 'with an existing checklist', authenticated_as: :authenticate do
  62. let(:checklist) { create(:checklist, ticket: ticket, item_count: 1) }
  63. def authenticate
  64. checklist
  65. agent
  66. end
  67. it 'triggers after checklist update' do
  68. checklist.update!(name: 'foobar')
  69. result = mock_channel.mock_broadcasted_messages.first[:result]['data']['ticketChecklistUpdates']
  70. expect(result).to include('ticketChecklist' => include(
  71. 'name' => 'foobar',
  72. ))
  73. end
  74. it 'triggers after checklist destroy' do
  75. checklist.destroy!
  76. result = mock_channel.mock_broadcasted_messages.first[:result]['data']['ticketChecklistUpdates']
  77. expect(result).to include('ticketChecklist' => nil, 'removedTicketChecklist' => true)
  78. end
  79. it 'triggers after checklist item create' do
  80. checklist.items.create!(text: 'foobar')
  81. result = mock_channel.mock_broadcasted_messages.first[:result]['data']['ticketChecklistUpdates']
  82. expect(result).to include('ticketChecklist' => include(
  83. 'items' => include(
  84. include(
  85. 'text' => 'foobar',
  86. )
  87. ),
  88. ))
  89. end
  90. it 'triggers after checklist item update' do
  91. checklist.items.last.update!(text: 'foobar')
  92. result = mock_channel.mock_broadcasted_messages.first[:result]['data']['ticketChecklistUpdates']
  93. expect(result).to include('ticketChecklist' => include(
  94. 'items' => include(
  95. include(
  96. 'text' => 'foobar',
  97. )
  98. ),
  99. ))
  100. end
  101. it 'triggers after checklist item destroy' do
  102. checklist.items.last.destroy!
  103. result = mock_channel.mock_broadcasted_messages.first[:result]['data']['ticketChecklistUpdates']
  104. expect(result['ticketChecklist']['items']).to be_empty
  105. end
  106. end
  107. end
  108. end