require 'spec_helper'

describe Page do
  describe "mass assignment" do
    [:name, :body, :link_type_ids].each do |attr|
      it { should allow_mass_assignment_of(attr) }

    end

    it { should_not allow_mass_assignment_of(:slug) }
  end

  describe 'validations' do
    it "is valid with valid attributes" do
      build(:page).should be_valid
    end

    [:name, :slug, :body].each do |attr|
      it { should validate_presence_of(attr) }
    end

    it 'creates unique slugs' do
      page1 = create(:page, name: 'about')
      page2 = create(:page, name: 'about')

      page1.slug.should_not == page2.slug
    end

    describe 'name format' do
      it "must start with a letter" do
        build(:page, name: 'a').should be_valid
        build(:page, name: '1').should_not be_valid
      end
    end
  end

  describe "associations" do
    describe "link types" do
      it { should have_many(:link_types) }
    end

    describe "link_placements" do
      it { should have_many(:link_placements) }

      context 'when the page is destroyed' do
        let(:page) { create(:page_with_navbar_link) }

        it 'destroys the associated link placements' do
          page.link_placements.count.should be(1)
          page.link_types.count.should be(1)
          page.destroy
          page.link_placements.should be_empty
          page.link_types.should be_empty
        end
      end
    end
  end

  describe 'template_name_for_show' do
    let(:page) { create(:page, name: 'Some Page 1') }

    it 'returns the slug with dashes replaced with underscores' do
      page.template_name_for_show.should == 'some_page_1'
    end
  end
end
