From 175db6f123ec6403f85589e4d0a89e12bfb5a28f Mon Sep 17 00:00:00 2001 From: Ryan Lue Date: Wed, 23 Jan 2019 12:53:11 +0100 Subject: [PATCH] Refactoring: Migrate setting_test.rb to RSpec --- spec/factories/setting.rb | 9 ++++++ spec/models/setting_spec.rb | 56 +++++++++++++++++++++++++++++++++ test/unit/setting_test.rb | 63 ------------------------------------- 3 files changed, 65 insertions(+), 63 deletions(-) create mode 100644 spec/factories/setting.rb create mode 100644 spec/models/setting_spec.rb delete mode 100644 test/unit/setting_test.rb diff --git a/spec/factories/setting.rb b/spec/factories/setting.rb new file mode 100644 index 000000000..ed389fdd0 --- /dev/null +++ b/spec/factories/setting.rb @@ -0,0 +1,9 @@ +FactoryBot.define do + factory :setting do + title { 'ABC API Token' } + name { 'abc_api_token' } + area { 'Integration::ABC' } + description { 'API Token for ABC to access ABC.' } + frontend { false } + end +end diff --git a/spec/models/setting_spec.rb b/spec/models/setting_spec.rb new file mode 100644 index 000000000..8f4d21b6e --- /dev/null +++ b/spec/models/setting_spec.rb @@ -0,0 +1,56 @@ +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' }) } + .to change { Setting.get(setting.name) }.to('foo') + end + end + end + + describe '.set' do + context 'when given a valid Setting#name' do + it 'sets #state_current = { value: }' do + expect { Setting.set(setting.name, 'foo') } + .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'] }) } + before { Cache.write('foo', 'bar') } + + it 'resets the cache key' do + expect { Setting.set(setting.name, 'baz') } + .to change { Cache.get('foo') }.to(nil) + end + end + end + + describe '.reset' do + context 'when given a valid Setting#name' do + it 'sets #state_current = { value: } (via #state_initial[:value])' do + setting.update(state_initial: { value: 'foo' }) + Setting.set(setting.name, 'bar') + + expect { Setting.reset(setting.name) } + .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 } + .to change { setting.state_initial }.from({}).to({ value: 'foo' }) + end + end + end +end diff --git a/test/unit/setting_test.rb b/test/unit/setting_test.rb deleted file mode 100644 index 7a0e652fe..000000000 --- a/test/unit/setting_test.rb +++ /dev/null @@ -1,63 +0,0 @@ -require 'test_helper' - -class SettingTest < ActiveSupport::TestCase - - test 'basics' do - Setting.create!( - title: 'ABC API Token', - name: 'abc_api_token', - area: 'Integration::ABC', - description: 'API Token for ABC to access ABC.', - options: { - form: [ - { - display: '', - null: false, - name: 'abc_token', - tag: 'input', - }, - ], - }, - state: 'abc', - frontend: false - ) - assert_equal(Setting.get('abc_api_token'), 'abc') - assert(Setting.set('abc_api_token', 'new_abc')) - assert_equal(Setting.get('abc_api_token'), 'new_abc') - assert(Setting.reset('abc_api_token')) - assert_equal(Setting.get('abc_api_token'), 'abc') - end - - test 'cache reset via preferences' do - Setting.create!( - title: 'ABC API Token', - name: 'abc_api_token', - area: 'Integration::ABC', - description: 'API Token for ABC to access ABC.', - options: { - form: [ - { - display: '', - null: false, - name: 'abc_token', - tag: 'input', - }, - ], - }, - state: '', - preferences: { - permission: ['admin.integration'], - cache: ['abcGetVoipUsers'], - }, - frontend: false - ) - - Cache.write('abcGetVoipUsers', { a: 1 }) - assert_equal(Cache.get('abcGetVoipUsers'), { a: 1 }) - - Setting.set('abc_api_token', 'some_new_value') - assert_nil(Cache.get('abcGetVoipUsers')) - - end - -end