state_spec.rb 6.7 KB

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