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.

188 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. callback = double
  80. allow(callback).to receive(:call)
  81. ActiveSupport::Notifications.subscribed callback, 'sql.active_record' do
  82. described_class[key]
  83. end
  84. expect(callback).not_to have_received(:call)
  85. end
  86. it 'returns the cached value' do
  87. expect(described_class[key]).to eq cache_value
  88. end
  89. end
  90. end
  91. end
  92. describe '.all_as_records' do
  93. before do
  94. allow_any_instance_of(Settings::ScopedSettings).to receive(:thing_scoped).and_return(records)
  95. allow(described_class).to receive(:default_settings).and_return(default_settings)
  96. end
  97. let(:key) { 'key' }
  98. let(:default_value) { 'default_value' }
  99. let(:default_settings) { { key => default_value } }
  100. let(:original_setting) { Fabricate(:setting, var: key, value: nil) }
  101. let(:records) { [original_setting] }
  102. it 'returns a Hash' do
  103. expect(described_class.all_as_records).to be_kind_of Hash
  104. end
  105. context 'records includes Setting with var as the key' do
  106. let(:records) { [original_setting] }
  107. it 'includes the original Setting' do
  108. setting = described_class.all_as_records[key]
  109. expect(setting).to eq original_setting
  110. end
  111. end
  112. context 'records includes nothing' do
  113. let(:records) { [] }
  114. context 'default_value is not a Hash' do
  115. it 'includes Setting with value of default_value' do
  116. setting = described_class.all_as_records[key]
  117. expect(setting).to be_kind_of Setting
  118. expect(setting).to have_attributes(var: key)
  119. expect(setting).to have_attributes(value: 'default_value')
  120. end
  121. end
  122. context 'default_value is a Hash' do
  123. let(:default_value) { { 'foo' => 'fuga' } }
  124. it 'returns {}' do
  125. expect(described_class.all_as_records).to eq({})
  126. end
  127. end
  128. end
  129. end
  130. describe '.default_settings' do
  131. before do
  132. allow(RailsSettings::Default).to receive(:enabled?).and_return(enabled)
  133. end
  134. subject { described_class.default_settings }
  135. context 'RailsSettings::Default.enabled? is false' do
  136. let(:enabled) { false }
  137. it 'returns {}' do
  138. is_expected.to eq({})
  139. end
  140. end
  141. context 'RailsSettings::Settings.enabled? is true' do
  142. let(:enabled) { true }
  143. it 'returns instance of RailsSettings::Default' do
  144. is_expected.to be_kind_of RailsSettings::Default
  145. end
  146. end
  147. end
  148. end