state_spec.rb 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. require 'rails_helper'
  2. require 'models/application_model_examples'
  3. require 'models/concerns/can_be_imported_examples'
  4. require 'models/concerns/has_collection_update_examples'
  5. require 'models/concerns/has_xss_sanitized_note_examples'
  6. RSpec.describe Ticket::State, type: :model do
  7. it_behaves_like 'ApplicationModel'
  8. it_behaves_like 'CanBeImported'
  9. it_behaves_like 'HasCollectionUpdate', collection_factory: :ticket_state
  10. it_behaves_like 'HasXssSanitizedNote', model_factory: :ticket_state
  11. describe 'Default state' do
  12. describe 'of whole table:' do
  13. it 'has seven records' do
  14. expect(described_class.pluck(:name))
  15. .to match_array(%w[closed merged new open pending\ close pending\ reminder removed])
  16. end
  17. end
  18. describe 'of "new" state:' do
  19. it 'is the sole #default_create state' do
  20. expect(described_class.where(default_create: true))
  21. .to match_array([described_class.find_by(name: 'new')])
  22. end
  23. end
  24. describe 'of "open" state:' do
  25. it 'is the sole #default_follow_up state' do
  26. expect(described_class.where(default_follow_up: true))
  27. .to match_array([described_class.find_by(name: 'open')])
  28. end
  29. end
  30. end
  31. describe 'Class methods:' do
  32. describe '.by_category' do
  33. it 'looks up states by category' do
  34. expect(described_class.by_category(:open))
  35. .to be_an(ActiveRecord::Relation)
  36. .and include(instance_of(described_class))
  37. end
  38. context 'with invalid category name' do
  39. it 'raises RuntimeError' do
  40. expect { described_class.by_category(:invalidcategoryname) }
  41. .to raise_error(RuntimeError)
  42. end
  43. end
  44. end
  45. end
  46. describe 'Attributes:' do
  47. describe '#default_create' do
  48. let!(:original_default) { described_class.find_by(default_create: true) }
  49. context 'for newly created record' do
  50. subject!(:state) { build(:ticket_state, default_create: default_create) }
  51. context 'when true' do
  52. let(:default_create) { true }
  53. it 'unsets previous default' do
  54. expect { state.save }
  55. .to change { original_default.reload.default_create }.to(false)
  56. .and not_change { described_class.where(default_create: true).count }
  57. end
  58. end
  59. context 'when false' do
  60. let(:default_create) { false }
  61. it 'does not alter existing default' do
  62. expect { state.save }
  63. .to not_change { described_class.find_by(default_create: true) }
  64. .and not_change { described_class.where(default_create: true).count }
  65. end
  66. end
  67. end
  68. context 'for existing record' do
  69. subject!(:state) { create(:ticket_state, default_create: default_create) }
  70. context 'when true' do
  71. let(:default_create) { true }
  72. context 'and updated to false' do
  73. it 'assigns Ticket::State.first as default' do
  74. expect { state.update(default_create: false) }
  75. .to change { described_class.first.default_create }.to(true)
  76. .and not_change { described_class.where(default_create: true).count }
  77. end
  78. end
  79. context 'and destroyed' do
  80. it 'assigns Ticket::State.first as default' do
  81. expect { state.destroy }
  82. .to change { described_class.first.default_create }.to(true)
  83. .and not_change { described_class.where(default_create: true).count }
  84. end
  85. end
  86. end
  87. context 'when false' do
  88. let(:default_create) { false }
  89. context 'and updated to true' do
  90. it 'unsets previous default' do
  91. expect { state.update(default_create: true) }
  92. .to change { original_default.reload.default_create }.to(false)
  93. .and not_change { described_class.where(default_create: true).count }
  94. end
  95. end
  96. context 'and destroyed' do
  97. it 'does not alter existing default' do
  98. expect { state.destroy }
  99. .to not_change { described_class.find_by(default_create: true) }
  100. .and not_change { described_class.where(default_create: true).count }
  101. end
  102. end
  103. end
  104. end
  105. end
  106. describe '#default_follow_up' do
  107. let!(:original_default) { described_class.find_by(default_follow_up: true) }
  108. context 'for newly created record' do
  109. subject!(:state) { build(:ticket_state, default_follow_up: default_follow_up) }
  110. context 'when true' do
  111. let(:default_follow_up) { true }
  112. it 'unsets previous default' do
  113. expect { state.save }
  114. .to change { original_default.reload.default_follow_up }.to(false)
  115. .and not_change { described_class.where(default_follow_up: true).count }
  116. end
  117. end
  118. context 'when false' do
  119. let(:default_follow_up) { false }
  120. it 'does not alter existing default' do
  121. expect { state.save }
  122. .to not_change { described_class.find_by(default_follow_up: true) }
  123. .and not_change { described_class.where(default_follow_up: true).count }
  124. end
  125. end
  126. end
  127. context 'for existing record' do
  128. subject!(:state) { create(:ticket_state, default_follow_up: default_follow_up) }
  129. context 'when true' do
  130. let(:default_follow_up) { true }
  131. context 'and updated to false' do
  132. it 'assigns Ticket::State.first as default' do
  133. expect { state.update(default_follow_up: false) }
  134. .to change { described_class.first.default_follow_up }.to(true)
  135. .and not_change { described_class.where(default_follow_up: true).count }
  136. end
  137. end
  138. context 'and destroyed' do
  139. it 'assigns Ticket::State.first as default' do
  140. expect { state.destroy }
  141. .to change { described_class.first.default_follow_up }.to(true)
  142. .and not_change { described_class.where(default_follow_up: true).count }
  143. end
  144. end
  145. end
  146. context 'when false' do
  147. let(:default_follow_up) { false }
  148. context 'and updated to true' do
  149. it 'unsets previous default' do
  150. expect { state.update(default_follow_up: true) }
  151. .to change { original_default.reload.default_follow_up }.to(false)
  152. .and not_change { described_class.where(default_follow_up: true).count }
  153. end
  154. end
  155. context 'and destroyed' do
  156. it 'does not alter existing default' do
  157. expect { state.destroy }
  158. .to not_change { described_class.find_by(default_follow_up: true) }
  159. .and not_change { described_class.where(default_follow_up: true).count }
  160. end
  161. end
  162. end
  163. end
  164. end
  165. end
  166. end