ticket_preferences_spec.rb 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. require 'rails_helper'
  2. RSpec.describe Escalation::TicketPreferences do
  3. let(:instance) { described_class.new ticket }
  4. let(:ticket) { create(:ticket) }
  5. describe '#sla_changed?' do
  6. it 'false when using same sla' do
  7. sla = create(:sla)
  8. instance.hash[:sla_id] = sla.id
  9. instance.hash[:sla_updated_at] = sla.updated_at
  10. expect(instance).not_to be_sla_changed(sla)
  11. end
  12. it 'true when using another sla' do
  13. sla = create(:sla)
  14. sla2 = create(:sla)
  15. instance.hash[:sla_id] = sla.id
  16. instance.hash[:sla_updated_at] = sla.updated_at
  17. expect(instance).to be_sla_changed(sla2)
  18. end
  19. it 'true when using updated sla' do
  20. sla = create(:sla)
  21. instance.hash[:sla_id] = sla.id
  22. instance.hash[:sla_updated_at] = sla.updated_at
  23. sla.touch
  24. expect(instance).to be_sla_changed(sla)
  25. end
  26. it 'doe not fail given nil' do
  27. expect { instance.sla_changed?(nil) }.not_to raise_error
  28. end
  29. end
  30. describe '#calendar_changed?' do
  31. it 'false when using same calendar' do
  32. calendar = create(:calendar)
  33. instance.hash[:calendar_id] = calendar.id
  34. instance.hash[:calendar_updated_at] = calendar.updated_at
  35. expect(instance).not_to be_calendar_changed(calendar)
  36. end
  37. it 'true when using another calendar' do
  38. calendar = create(:calendar)
  39. calendar2 = create(:calendar)
  40. instance.hash[:calendar_id] = calendar.id
  41. instance.hash[:calendar_updated_at] = calendar.updated_at
  42. expect(instance).to be_calendar_changed(calendar2)
  43. end
  44. it 'true when using updated calendar' do
  45. calendar = create(:calendar)
  46. instance.hash[:calendar_id] = calendar.id
  47. instance.hash[:calendar_updated_at] = calendar.updated_at
  48. calendar.touch
  49. expect(instance).to be_calendar_changed(calendar)
  50. end
  51. it 'doe not fail given nil' do
  52. expect { instance.calendar_changed?(nil) }.not_to raise_error
  53. end
  54. end
  55. describe '#first_response_at_changed?' do
  56. before { freeze_time }
  57. it 'true when changed' do
  58. instance.hash[:first_response_at] = 1.day.ago
  59. ticket.update! first_response_at: 1.week.ago
  60. expect(instance).to be_first_response_at_changed(ticket)
  61. end
  62. it 'false when matching' do
  63. instance.hash[:first_response_at] = 7.days.ago
  64. ticket.update! first_response_at: 1.week.ago
  65. expect(instance).not_to be_first_response_at_changed(ticket)
  66. end
  67. end
  68. describe '#last_update_at_changed?' do
  69. before { freeze_time }
  70. it 'true when changed' do
  71. instance.hash[:last_update_at] = 1.day.ago
  72. ticket.update! last_contact_customer_at: 1.week.ago
  73. expect(instance).to be_last_update_at_changed(ticket)
  74. end
  75. it 'false when matching' do
  76. instance.hash[:last_update_at] = 7.days.ago
  77. ticket.update! last_contact_customer_at: 1.week.ago
  78. expect(instance).not_to be_last_update_at_changed(ticket)
  79. end
  80. end
  81. describe '#close_at_changed?' do
  82. before { freeze_time }
  83. it 'true when changed' do
  84. instance.hash[:close_at] = 1.day.ago
  85. ticket.update! close_at: 1.week.ago
  86. expect(instance).to be_close_at_changed(ticket)
  87. end
  88. it 'false when matching' do
  89. instance.hash[:close_at] = 7.days.ago
  90. ticket.update! close_at: 1.week.ago
  91. expect(instance).not_to be_close_at_changed(ticket)
  92. end
  93. end
  94. describe '#property_changes?' do
  95. before { freeze_time }
  96. it 'false when no changes' do
  97. ticket.update! last_contact_customer_at: 1.week.ago, first_response_at: 5.days.ago, close_at: 1.day.ago
  98. instance.hash.merge! last_update_at: 1.week.ago, first_response_at: 5.days.ago, close_at: 1.day.ago
  99. expect(instance).not_to be_property_changes(ticket)
  100. end
  101. it 'true when #first_response_at changes' do
  102. ticket.update! last_contact_customer_at: 1.week.ago, first_response_at: 4.days.ago, close_at: 1.day.ago
  103. instance.hash.merge! last_update_at: 1.week.ago, first_response_at: 5.days.ago, close_at: 1.day.ago
  104. expect(instance).to be_property_changes(ticket)
  105. end
  106. it 'true when last_update_at changes' do
  107. ticket.update! last_contact_customer_at: 2.weeks.ago, first_response_at: 5.days.ago, close_at: 1.day.ago
  108. instance.hash.merge! last_update_at: 1.week.ago, first_response_at: 5.days.ago, close_at: 1.day.ago
  109. expect(instance).to be_property_changes(ticket)
  110. end
  111. it 'true when #close_at changes' do
  112. ticket.update! last_contact_customer_at: 1.week.ago, first_response_at: 5.days.ago, close_at: 2.days.ago
  113. instance.hash.merge! last_update_at: 1.week.ago, first_response_at: 5.days.ago, close_at: 1.day.ago
  114. expect(instance).to be_property_changes(ticket)
  115. end
  116. end
  117. describe '#any_changes?' do
  118. let(:sla) { create(:sla) }
  119. before do
  120. freeze_time
  121. sla
  122. instance.hash[:sla_id] = sla.id
  123. instance.hash[:sla_updated_at] = sla.updated_at
  124. instance.hash[:calendar_id] = sla.calendar.id
  125. instance.hash[:calendar_updated_at] = sla.calendar.updated_at
  126. end
  127. it 'false when no changes' do
  128. instance.hash[:escalation_disabled] = false
  129. ticket.update! last_contact_customer_at: 1.week.ago, first_response_at: 5.days.ago, close_at: 1.day.ago
  130. instance.hash.merge! last_update_at: 1.week.ago, first_response_at: 5.days.ago, close_at: 1.day.ago
  131. expect(instance).not_to be_any_changes(ticket, sla, false)
  132. end
  133. it 'true when time property changed' do
  134. instance.hash[:escalation_disabled] = false
  135. ticket.update! last_contact_customer_at: 1.week.ago, first_response_at: 5.days.ago, close_at: 2.days.ago
  136. instance.hash.merge! last_update_at: 1.week.ago, first_response_at: 5.days.ago, close_at: 1.day.ago
  137. expect(instance).to be_any_changes(ticket, sla, false)
  138. end
  139. it 'true when sla changes' do
  140. sla2 = create(:sla)
  141. instance.hash[:escalation_disabled] = false
  142. ticket.update! last_contact_customer_at: 1.week.ago, first_response_at: 5.days.ago, close_at: 1.day.ago
  143. instance.hash.merge! last_update_at: 1.week.ago, first_response_at: 5.days.ago, close_at: 1.day.ago
  144. expect(instance).to be_any_changes(ticket, sla2, false)
  145. end
  146. it 'true when escalability status changes' do
  147. instance.hash[:escalation_disabled] = true
  148. ticket.update! last_contact_customer_at: 1.week.ago, first_response_at: 5.days.ago, close_at: 1.day.ago
  149. instance.hash.merge! last_update_at: 1.week.ago, first_response_at: 5.days.ago, close_at: 1.day.ago
  150. expect(instance).to be_any_changes(ticket, sla, false)
  151. end
  152. end
  153. describe '#update_preferences' do
  154. it 'sets escalation_calculation in ticket preferences' do
  155. response = :spec
  156. allow(instance).to receive(:hash_of).and_return(response)
  157. instance.update_preferences(ticket, nil, false)
  158. expect(ticket.preferences[:escalation_calculation]).to eq response
  159. end
  160. end
  161. describe '#hash_of' do
  162. let(:last_contact) { 6.days.ago }
  163. let(:first_response_at) { 1.week.ago }
  164. let(:close_at) { 1.day.ago }
  165. let(:sla) { create(:sla, updated_at: 1.week.from_now) }
  166. let(:escalation_disabled) { false }
  167. let(:result) { instance.hash_of(ticket, sla, escalation_disabled) }
  168. before do
  169. freeze_time
  170. sla
  171. ticket.update! last_contact_customer_at: last_contact, first_response_at: first_response_at, close_at: close_at
  172. end
  173. it { expect(result).to be_a(Hash) }
  174. it { expect(result[:first_response_at]).to eq ticket.first_response_at }
  175. it { expect(result[:last_update_at]).to eq last_contact }
  176. it { expect(result[:close_at]).to eq ticket.close_at }
  177. it { expect(result[:sla_id]).to eq sla.id }
  178. it { expect(result[:sla_updated_at]).to eq sla.updated_at }
  179. it { expect(result[:calendar_id]).to eq sla.calendar.id }
  180. it { expect(result[:calendar_updated_at]).to eq sla.calendar.updated_at }
  181. it { expect(result[:escalation_disabled]).to eq escalation_disabled }
  182. context 'when sla not given' do
  183. let(:sla) { nil }
  184. it 'sla and calendar meta data are not included' do
  185. expect(result.keys).to eq %i[first_response_at last_update_at close_at escalation_disabled]
  186. end
  187. end
  188. end
  189. end