setting_spec.rb 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. require 'rails_helper'
  2. RSpec.describe Setting, type: :model do
  3. subject(:setting) { create(:setting) }
  4. describe '.get' do
  5. context 'when given a valid Setting#name' do
  6. it 'returns #state_current[:value]' do
  7. expect { setting.update(state_current: { value: 'foo' }) }
  8. .to change { described_class.get(setting.name) }.to('foo')
  9. end
  10. end
  11. end
  12. describe '.set' do
  13. context 'when given a valid Setting#name' do
  14. it 'sets #state_current = { value: <arg> }' do
  15. expect { described_class.set(setting.name, 'foo') }
  16. .to change { setting.reload.state_current }.to({ 'value' => 'foo' })
  17. end
  18. end
  19. context 'when #preferences hash includes a :cache key' do
  20. subject(:setting) { create(:setting, preferences: { cache: ['foo'] }) }
  21. before { Cache.write('foo', 'bar') }
  22. it 'resets the cache key' do
  23. expect { described_class.set(setting.name, 'baz') }
  24. .to change { Cache.get('foo') }.to(nil)
  25. end
  26. end
  27. end
  28. describe '.reset' do
  29. context 'when given a valid Setting#name' do
  30. it 'sets #state_current = { value: <orig> } (via #state_initial[:value])' do
  31. setting.update(state_initial: { value: 'foo' })
  32. described_class.set(setting.name, 'bar')
  33. expect { described_class.reset(setting.name) }
  34. .to change { setting.reload.state_current }.to({ value: 'foo' })
  35. end
  36. end
  37. end
  38. describe 'attributes' do
  39. describe '#state_initial' do
  40. subject(:setting) { build(:setting, state: 'foo') }
  41. it 'is set on creation, based on #state' do
  42. expect { setting.save }
  43. .to change(setting, :state_initial).from({}).to({ value: 'foo' })
  44. end
  45. end
  46. end
  47. end