Refactoring: Migrate setting_test.rb to RSpec

This commit is contained in:
Ryan Lue 2019-01-23 12:53:11 +01:00 committed by Thorsten Eckel
parent a7e2f9092f
commit 175db6f123
3 changed files with 65 additions and 63 deletions

View file

@ -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

View file

@ -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: <arg> }' 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: <orig> } (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

View file

@ -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