require "spec_helper"

describe PagesController do
  describe 'routing' do
    let(:page) { create(:page, slug: 'test-page') }

    it 'routes to #index' do
      get('/pages').should route_to('pages#index')
    end

    it 'routes to #new' do
      get('/pages/new').should route_to('pages#new')
    end

    it 'routes to #create' do
      post('/pages').should route_to('pages#create')
    end

    # Route by ids or slugs
    { 'an id' => '1', 'a slug' => 'test-page' }.each do |id_type, param|
      it "routes to #show by #{id_type}" do
        get("/pages/#{param}").should route_to("pages#show", id: param)
      end

      it "routes to #edit by #{id_type}" do
        get("/pages/#{param}/edit").should route_to("pages#edit", id: param)
      end

      it "routes to #update by #{id_type}" do
        put("/pages/#{param}").should route_to("pages#update", id: param)
      end

      it "routes to #destroy by #{id_type}" do
        delete("/pages/#{param}").should route_to("pages#destroy", id: param)
      end
    end

    context 'when /pages/ is not in the url' do
      context 'and the :id starts starts with a letter' do
        it 'routes to #show' do
          get('/test-page').should route_to('pages#show', id: 'test-page')
        end
      end

      context 'and the :id starts with a number' do
        it 'is not routable' do
          get('/1').should_not be_routable
        end
      end
    end
  end
end
