state_spec.rb 6.6 KB

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