You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
5.5 KiB

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