require 'spec_helper'

describe 'SiteCategory' do
  let(:attrs) { {} }
  subject(:site_category) { build :site_category, attrs }
  it { should have_and_belong_to_many :articles }
  it { should have_and_belong_to_many :events }
  it { should have_and_belong_to_many :page_editions }
  it { should belong_to :site }

  # fields
  it { should have_field(:title).of_type String }
  it { should have_field(:type).of_type String }
  it { should have_field(:slug).of_type String }
  it { should have_field(:description).of_type String }

  # validations
  it { should validate_presence_of :title }
  it { should validate_presence_of :type }
  it { should validate_presence_of :slug }

  describe '.slug validation' do
    context 'when slug contains invalid characters' do
      site = FactoryGirl.build :site, slug: "invalid$character"

      it 'should be invalid' do
        site.valid?
        expect(site.errors.messages.has_key? :slug).to be_truthy
      end
    end
    context 'when slug contains valid characters' do
      site = FactoryGirl.build :site, slug: "all-this_IS_0-k-"
      it 'should be valid' do
        site.valid?
        expect(site.errors.messages.has_key? :slug).to be_falsey
      end
    end
  end

  describe 'set_slug' do
    it 'should paramaterize the title' do
      expect(site_category.send(:set_slug)).to eq(site_category.title.parameterize)
    end
  end
end
