setting_spec.rb 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Setting do
  4. describe '#to_param' do
  5. let(:setting) { Fabricate(:setting, var: var) }
  6. let(:var) { 'var' }
  7. it 'returns setting.var' do
  8. expect(setting.to_param).to eq var
  9. end
  10. end
  11. describe '.[]' do
  12. before do
  13. allow(described_class).to receive(:rails_initialized?).and_return(rails_initialized)
  14. end
  15. let(:key) { 'key' }
  16. context 'when rails_initialized? is falsey' do
  17. let(:rails_initialized) { false }
  18. it 'calls RailsSettings::Base#[]' do
  19. allow(RailsSettings::Base).to receive(:[]).with(key)
  20. described_class[key]
  21. expect(RailsSettings::Base).to have_received(:[]).with(key)
  22. end
  23. end
  24. context 'when rails_initialized? is truthy' do
  25. before do
  26. allow(RailsSettings::Base).to receive(:cache_key).with(key, nil).and_return(cache_key)
  27. end
  28. let(:rails_initialized) { true }
  29. let(:cache_key) { 'cache-key' }
  30. let(:cache_value) { 'cache-value' }
  31. it 'calls not RailsSettings::Base#[]' do
  32. allow(RailsSettings::Base).to receive(:[]).with(key)
  33. described_class[key]
  34. expect(RailsSettings::Base).to_not have_received(:[]).with(key)
  35. end
  36. context 'when Rails.cache does not exists' do
  37. before do
  38. allow(RailsSettings::Settings).to receive(:object).with(key).and_return(object)
  39. allow(described_class).to receive(:default_settings).and_return(default_settings)
  40. settings_double = instance_double(Settings::ScopedSettings, thing_scoped: records)
  41. allow(Settings::ScopedSettings).to receive(:new).and_return(settings_double)
  42. Rails.cache.delete(cache_key)
  43. end
  44. let(:object) { nil }
  45. let(:default_value) { 'default_value' }
  46. let(:default_settings) { { key => default_value } }
  47. let(:records) { [Fabricate(:setting, var: key, value: nil)] }
  48. it 'calls RailsSettings::Settings.object' do
  49. allow(RailsSettings::Settings).to receive(:object).with(key)
  50. described_class[key]
  51. expect(RailsSettings::Settings).to have_received(:object).with(key)
  52. end
  53. context 'when RailsSettings::Settings.object returns truthy' do
  54. let(:object) { db_val }
  55. let(:db_val) { instance_double(described_class, value: 'db_val') }
  56. context 'when default_value is a Hash' do
  57. let(:default_value) { { default_value: 'default_value' } }
  58. it 'calls default_value.with_indifferent_access.merge!' do
  59. indifferent_hash = instance_double(Hash, merge!: nil)
  60. allow(default_value).to receive(:with_indifferent_access).and_return(indifferent_hash)
  61. described_class[key]
  62. expect(default_value).to have_received(:with_indifferent_access)
  63. expect(indifferent_hash).to have_received(:merge!).with(db_val.value)
  64. end
  65. end
  66. context 'when default_value is not a Hash' do
  67. let(:default_value) { 'default_value' }
  68. it 'returns db_val.value' do
  69. expect(described_class[key]).to be db_val.value
  70. end
  71. end
  72. end
  73. context 'when RailsSettings::Settings.object returns falsey' do
  74. let(:object) { nil }
  75. it 'returns default_settings[key]' do
  76. expect(described_class[key]).to be default_settings[key]
  77. end
  78. end
  79. end
  80. context 'when Rails.cache exists' do
  81. before do
  82. Rails.cache.write(cache_key, cache_value)
  83. end
  84. it 'does not query the database' do
  85. callback = double
  86. allow(callback).to receive(:call)
  87. ActiveSupport::Notifications.subscribed callback, 'sql.active_record' do
  88. described_class[key]
  89. end
  90. expect(callback).to_not have_received(:call)
  91. end
  92. it 'returns the cached value' do
  93. expect(described_class[key]).to eq cache_value
  94. end
  95. end
  96. end
  97. end
  98. describe '.all_as_records' do
  99. before do
  100. settings_double = instance_double(Settings::ScopedSettings, thing_scoped: records)
  101. allow(Settings::ScopedSettings).to receive(:new).and_return(settings_double)
  102. allow(described_class).to receive(:default_settings).and_return(default_settings)
  103. end
  104. let(:key) { 'key' }
  105. let(:default_value) { 'default_value' }
  106. let(:default_settings) { { key => default_value } }
  107. let(:original_setting) { Fabricate(:setting, var: key, value: nil) }
  108. let(:records) { [original_setting] }
  109. it 'returns a Hash' do
  110. expect(described_class.all_as_records).to be_a Hash
  111. end
  112. context 'when records includes Setting with var as the key' do
  113. let(:records) { [original_setting] }
  114. it 'includes the original Setting' do
  115. setting = described_class.all_as_records[key]
  116. expect(setting).to eq original_setting
  117. end
  118. end
  119. context 'when records includes nothing' do
  120. let(:records) { [] }
  121. context 'when default_value is not a Hash' do
  122. it 'includes Setting with value of default_value' do
  123. setting = described_class.all_as_records[key]
  124. expect(setting).to be_a described_class
  125. expect(setting).to have_attributes(var: key)
  126. expect(setting).to have_attributes(value: 'default_value')
  127. end
  128. end
  129. context 'when default_value is a Hash' do
  130. let(:default_value) { { 'foo' => 'fuga' } }
  131. it 'returns {}' do
  132. expect(described_class.all_as_records).to eq({})
  133. end
  134. end
  135. end
  136. end
  137. describe '.default_settings' do
  138. subject { described_class.default_settings }
  139. before do
  140. allow(RailsSettings::Default).to receive(:enabled?).and_return(enabled)
  141. end
  142. context 'when RailsSettings::Default.enabled? is false' do
  143. let(:enabled) { false }
  144. it 'returns {}' do
  145. expect(subject).to eq({})
  146. end
  147. end
  148. context 'when RailsSettings::Settings.enabled? is true' do
  149. let(:enabled) { true }
  150. it 'returns instance of RailsSettings::Default' do
  151. expect(subject).to be_a RailsSettings::Default
  152. end
  153. end
  154. end
  155. end