The code powering m.abunchtell.com https://m.abunchtell.com
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

185 řádky
5.4 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. it 'calls Rails.cache.fetch' do
  35. expect(Rails).to receive_message_chain(:cache, :fetch).with(cache_key)
  36. described_class[key]
  37. end
  38. context 'Rails.cache does not exists' do
  39. before do
  40. allow(RailsSettings::Settings).to receive(:object).with(key).and_return(object)
  41. allow(described_class).to receive(:default_settings).and_return(default_settings)
  42. allow_any_instance_of(Settings::ScopedSettings).to receive(:thing_scoped).and_return(records)
  43. Rails.cache.clear(cache_key)
  44. end
  45. let(:object) { nil }
  46. let(:default_value) { 'default_value' }
  47. let(:default_settings) { { key => default_value } }
  48. let(:records) { [Fabricate(:setting, var: key, value: nil)] }
  49. it 'calls RailsSettings::Settings.object' do
  50. expect(RailsSettings::Settings).to receive(:object).with(key)
  51. described_class[key]
  52. end
  53. context 'RailsSettings::Settings.object returns truthy' do
  54. let(:object) { db_val }
  55. let(:db_val) { double(value: 'db_val') }
  56. context '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. expect(default_value).to receive_message_chain(:with_indifferent_access, :merge!)
  60. .with(db_val.value)
  61. described_class[key]
  62. end
  63. end
  64. context 'default_value is not a Hash' do
  65. let(:default_value) { 'default_value' }
  66. it 'returns db_val.value' do
  67. expect(described_class[key]).to be db_val.value
  68. end
  69. end
  70. end
  71. context 'RailsSettings::Settings.object returns falsey' do
  72. let(:object) { nil }
  73. it 'returns default_settings[key]' do
  74. expect(described_class[key]).to be default_settings[key]
  75. end
  76. end
  77. end
  78. context 'Rails.cache exists' do
  79. before do
  80. Rails.cache.write(cache_key, cache_value)
  81. end
  82. it 'returns the cached value' do
  83. expect(described_class[key]).to eq cache_value
  84. end
  85. end
  86. end
  87. end
  88. describe '.all_as_records' do
  89. before do
  90. allow_any_instance_of(Settings::ScopedSettings).to receive(:thing_scoped).and_return(records)
  91. allow(described_class).to receive(:default_settings).and_return(default_settings)
  92. end
  93. let(:key) { 'key' }
  94. let(:default_value) { 'default_value' }
  95. let(:default_settings) { { key => default_value } }
  96. let(:original_setting) { Fabricate(:setting, var: key, value: nil) }
  97. let(:records) { [original_setting] }
  98. it 'returns a Hash' do
  99. expect(described_class.all_as_records).to be_kind_of Hash
  100. end
  101. context 'records includes Setting with var as the key' do
  102. let(:records) { [original_setting] }
  103. it 'includes the original Setting' do
  104. setting = described_class.all_as_records[key]
  105. expect(setting).to eq original_setting
  106. end
  107. end
  108. context 'records includes nothing' do
  109. let(:records) { [] }
  110. context 'default_value is not a Hash' do
  111. it 'includes Setting with value of default_value' do
  112. setting = described_class.all_as_records[key]
  113. expect(setting).to be_kind_of Setting
  114. expect(setting).to have_attributes(var: key)
  115. expect(setting).to have_attributes(value: 'default_value')
  116. end
  117. end
  118. context 'default_value is a Hash' do
  119. let(:default_value) { { 'foo' => 'fuga' } }
  120. it 'returns {}' do
  121. expect(described_class.all_as_records).to eq({})
  122. end
  123. end
  124. end
  125. end
  126. describe '.default_settings' do
  127. before do
  128. allow(RailsSettings::Default).to receive(:enabled?).and_return(enabled)
  129. end
  130. subject { described_class.default_settings }
  131. context 'RailsSettings::Default.enabled? is false' do
  132. let(:enabled) { false }
  133. it 'returns {}' do
  134. is_expected.to eq({})
  135. end
  136. end
  137. context 'RailsSettings::Settings.enabled? is true' do
  138. let(:enabled) { true }
  139. it 'returns instance of RailsSettings::Default' do
  140. is_expected.to be_kind_of RailsSettings::Default
  141. end
  142. end
  143. end
  144. end