require 'spec_helper' RSpec.describe NsSettingsUi::Setting, type: :model do describe '.get' do let(:key) { 'matching.cluster.size' } context 'when setting exists' do let!(:setting) { described_class.make!(key: key) } it 'finds setting by country and exact key' do expect(described_class.get(key)).to eq(setting) end it 'finds setting by country and fragmented key' do expect(described_class.get(*key.split('.'))).to eq(setting) end end context 'when setting does not exist' do it 'creates setting with empty value' do expect(described_class.get(key).value).to be_nil end it 'creates setting for the given key' do expect(described_class.get(key).key).to eq(key) end end end describe '#keys' do subject { described_class.make(key: key) } context 'when key is empty' do let(:key) { '' } its(:keys) { is_expected.to be_empty } end context 'when there is a simple key' do let(:key) { 'aa' } its(:keys) { is_expected.to eq([:aa]) } end context 'when key consists of multiple keys' do let(:key) { 'aa.bb' } its(:keys) { is_expected.to eq([:aa, :bb]) } end end describe '#formatted_value' do let(:key) { 'aa.bb' } subject { described_class.make(key: key, value: value, value_type: value_type).formatted_value } context 'when value_type is default' do let(:value_type) { nil } context 'when value is a nil' do let(:value) { nil } it { is_expected.to be_nil } end context 'when value is a string' do let(:value) { 'abcd' } it { is_expected.to eq('abcd') } end context 'when value is an integer with a string' do let(:value) { '4abcd' } it { is_expected.to eq('4abcd') } end context 'when value is an integer' do let(:value) { '10' } it { is_expected.to eq('10') } end end context 'when value_type is :string' do let(:value_type) { NsSettingsUi::Setting::TYPES[:string] } context 'when value is a nil' do let(:value) { nil } it { is_expected.to eq(nil) } end context 'when value is a string' do let(:value) { 'abcd' } it { is_expected.to eq('abcd') } end context 'when value is an integer with a string' do let(:value) { '4abcd' } it { is_expected.to eq('4abcd') } end context 'when value is an integer' do let(:value) { '10' } it { is_expected.to eq('10') } end end context 'when value_type is :integer' do let(:value_type) { NsSettingsUi::Setting::TYPES[:integer] } context 'when value is a nil' do let(:value) { nil } it { is_expected.to eq(nil) } end context 'when value is a string' do let(:value) { 'abcd' } it { is_expected.to eq(0) } end context 'when value is an integer with a string' do let(:value) { '4abcd' } it { is_expected.to eq(4) } end context 'when value is an integer' do let(:value) { '10' } it { is_expected.to eq(10) } end end end end