setting_spec.rb 1.8 KB

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