require 'rails_helper'

describe Concerns::Storable::Config do
  let(:klass) { described_class }
  let(:config) { Concerns::Storable::Config.new }

  describe '#fields' do
    it :default do
      expect(config.fields).to eq({})
    end
  end

  describe '#add' do
    it 'as intended' do
      config.add(:first_name, :string)
      expect(config.fields).to eq({
        first_name: { klass: :string, default: nil }
      })
    end

    it 'as strings' do
      config.add('first_name', 'string', '')
      expect(config.fields).to eq({
        first_name: { klass: :string, default: ''}
      })
    end

    it 'only allow certain klasses' do
      klass::KLASSES.keys.each do |k|
        config.add(:foo, k)
        expect(config.fields[:foo][:klass]).to eq k.to_sym
      end

      %w(foo bar baz).each do |k |
        expect { config.add(:foo, k) }.to raise_error("#{k} is not a valid storable config klass")
      end
    end
  end

  describe '#allowed?' do
    it :true do
      config.add(:foo, :string)
      expect(config).to be_allowed('foo')
    end

    it :false do
      expect(config).not_to be_allowed('foo')
    end
  end

  it '#respond_to?' do
    expect(config).to respond_to(:add, :fields, :allowed?)
  end
end

require 'rails_helper'

describe Concerns::Storable::Config do
  let(:klass) { described_class }
  let(:config) { Concerns::Storable::Config.new }

  it '#fields' do
    expect(config.fields).to eq({})
  end

  describe '#add' do
    it 'without default value' do
      config.add :first_name, String
      expect(config.fields).to eq ({
        first_name: { type: String, default: nil }
      })
    end

    it 'with default value' do
      config.add :first_name, String, 'foo'
      expect(config.fields).to eq ({
        first_name: { type: String, default: 'foo' }
      })
    end
  end

  describe '#allowed?' do
    it :true do
      config.add :foo, String
      expect(config).to be_allowed('foo')
    end

    it :false do
      expect(config).not_to be_allowed('foo')
    end
  end

  it '#respond_to?' do
    expect(config).to respond_to(:add, :fields, :allowed?)
  end
end

Fix the storable config spec.
Fix the storable config spec.
