triggers_subscriptions_spec.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe Checklist::Item::TriggersSubscriptions, current_user_id: 1 do
  4. let(:checklist_item) { create(:checklist_item, ticket: referenced_ticket) }
  5. let(:referenced_ticket) { nil }
  6. before do
  7. # Disable subscription triggering by ticket
  8. # Otherwise it throws off checking if checklist item triggered correct subscriptions
  9. allow_any_instance_of(Ticket).to receive(:trigger_subscriptions)
  10. end
  11. describe '#trigger_item_reference_change_subscription' do
  12. context 'when new item is created' do
  13. before do
  14. allow(Gql::Subscriptions::TicketUpdates).to receive(:trigger)
  15. end
  16. it 'triggers the subscription if a ticket is given' do
  17. item = create(:checklist_item, ticket: create(:ticket))
  18. expect(Gql::Subscriptions::TicketUpdates)
  19. .to have_received(:trigger)
  20. .with(item.ticket, arguments: { ticket_id: item.ticket.to_global_id.to_s })
  21. end
  22. it 'does not trigger the subscription if a ticket is not given' do
  23. create(:checklist_item, ticket: nil)
  24. expect(Gql::Subscriptions::TicketUpdates).not_to have_received(:trigger)
  25. end
  26. end
  27. context 'when an item with the ticket exists' do
  28. let(:referenced_ticket) { create(:ticket) }
  29. before do
  30. checklist_item
  31. allow(Gql::Subscriptions::TicketUpdates).to receive(:trigger)
  32. end
  33. it 'triggers the subscription twice when changing the ticket', aggregate_failures: true do
  34. checklist_item.update! ticket: create(:ticket)
  35. expect(Gql::Subscriptions::TicketUpdates)
  36. .to have_received(:trigger)
  37. .with(referenced_ticket, arguments: { ticket_id: referenced_ticket.to_global_id.to_s })
  38. expect(Gql::Subscriptions::TicketUpdates)
  39. .to have_received(:trigger)
  40. .with(checklist_item.ticket, arguments: { ticket_id: checklist_item.ticket.to_global_id.to_s })
  41. end
  42. it 'does not trigger the subscription when editing another attribute' do
  43. checklist_item.update! text: 'changed!'
  44. expect(Gql::Subscriptions::TicketUpdates).not_to have_received(:trigger)
  45. end
  46. it 'triggers the subscription if the ticket is unset' do
  47. checklist_item.update! ticket: nil
  48. expect(Gql::Subscriptions::TicketUpdates)
  49. .to have_received(:trigger)
  50. .with(referenced_ticket, arguments: { ticket_id: referenced_ticket.to_global_id.to_s })
  51. end
  52. end
  53. context 'when an item without the ticket exists' do
  54. before do
  55. checklist_item
  56. allow(Gql::Subscriptions::TicketUpdates).to receive(:trigger)
  57. end
  58. it 'triggers the subscription if the ticket is set' do
  59. checklist_item.update! ticket: create(:ticket)
  60. expect(Gql::Subscriptions::TicketUpdates)
  61. .to have_received(:trigger)
  62. .with(checklist_item.ticket, arguments: { ticket_id: checklist_item.ticket.to_global_id.to_s })
  63. end
  64. end
  65. end
  66. describe '#trigger_item_reference_destroy_subscription' do
  67. before do
  68. checklist_item
  69. allow(Gql::Subscriptions::TicketUpdates).to receive(:trigger)
  70. end
  71. context 'when a ticket is referenced' do
  72. let(:referenced_ticket) { create(:ticket) }
  73. it 'triggers the subscription' do
  74. checklist_item.destroy!
  75. expect(Gql::Subscriptions::TicketUpdates)
  76. .to have_received(:trigger)
  77. .with(referenced_ticket, arguments: { ticket_id: referenced_ticket.to_global_id.to_s })
  78. end
  79. end
  80. context 'when a ticket is not referenced' do
  81. it 'does not trigger a subscription' do
  82. checklist_item.destroy!
  83. expect(Gql::Subscriptions::TicketUpdates).not_to have_received(:trigger)
  84. end
  85. end
  86. end
  87. end