2021-06-01 12:20:20 +00:00
|
|
|
# Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
2019-01-23 11:53:11 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe Setting, type: :model do
|
|
|
|
subject(:setting) { create(:setting) }
|
|
|
|
|
|
|
|
describe '.get' do
|
|
|
|
context 'when given a valid Setting#name' do
|
|
|
|
it 'returns #state_current[:value]' do
|
|
|
|
expect { setting.update(state_current: { value: 'foo' }) }
|
2019-09-16 15:04:17 +00:00
|
|
|
.to change { described_class.get(setting.name) }.to('foo')
|
2019-01-23 11:53:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.set' do
|
|
|
|
context 'when given a valid Setting#name' do
|
|
|
|
it 'sets #state_current = { value: <arg> }' do
|
2019-09-16 15:04:17 +00:00
|
|
|
expect { described_class.set(setting.name, 'foo') }
|
2019-01-23 11:53:11 +00:00
|
|
|
.to change { setting.reload.state_current }.to({ 'value' => 'foo' })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when #preferences hash includes a :cache key' do
|
|
|
|
subject(:setting) { create(:setting, preferences: { cache: ['foo'] }) }
|
2019-04-15 01:41:17 +00:00
|
|
|
|
2019-01-23 11:53:11 +00:00
|
|
|
before { Cache.write('foo', 'bar') }
|
|
|
|
|
|
|
|
it 'resets the cache key' do
|
2019-09-16 15:04:17 +00:00
|
|
|
expect { described_class.set(setting.name, 'baz') }
|
2021-05-31 13:05:54 +00:00
|
|
|
.to change { Cache.read('foo') }.to(nil)
|
2019-01-23 11:53:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '.reset' do
|
|
|
|
context 'when given a valid Setting#name' do
|
|
|
|
it 'sets #state_current = { value: <orig> } (via #state_initial[:value])' do
|
|
|
|
setting.update(state_initial: { value: 'foo' })
|
2019-09-16 15:04:17 +00:00
|
|
|
described_class.set(setting.name, 'bar')
|
2019-01-23 11:53:11 +00:00
|
|
|
|
2019-09-16 15:04:17 +00:00
|
|
|
expect { described_class.reset(setting.name) }
|
2019-01-23 11:53:11 +00:00
|
|
|
.to change { setting.reload.state_current }.to({ value: 'foo' })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'attributes' do
|
|
|
|
describe '#state_initial' do
|
|
|
|
subject(:setting) { build(:setting, state: 'foo') }
|
|
|
|
|
|
|
|
it 'is set on creation, based on #state' do
|
|
|
|
expect { setting.save }
|
2019-04-15 01:41:17 +00:00
|
|
|
.to change(setting, :state_initial).from({}).to({ value: 'foo' })
|
2019-01-23 11:53:11 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|