require 'test_helper'

class AdsControllerTest < ActionController::TestCase
  def test_should_list_all_categories
    get :index
    assert_response :success
    assert_template 'index'
    assert_select 'p', :count => 4
  end

  def test_should_confirm_ad_info
    get :show, :id => ads(:one)
    assert_select 'p', { :count => 1, :text => 'Seller: ' + users(:one).email }
    assert_select 'p', { :count => 1, :text => 'Info: ' + ads(:one).description }
    assert_select 'p', { :count => 1, :text => 'Ad#: ' + ads(:one).id.to_s }
  end

  def test_should_require_user_to_post_ad
    description = 'LA beach house'
    category = 'vacation homes'
    post :create, :ad => { 
      :posted => Time.now,
      :description => description,
      :category => category
    }
    assert_response :redirect
    assert_redirected_to new_session_path
  end

  def test_should_post_ad
    description = 'LA beach house'
    category = 'vacation homes'
    post_with_user :create, :ad => { 
      :posted => Time.now,
      :description => description,
      :category => category
    }
    assert_response :redirect
    assert_equal assigns(:ad).description, description
    assert_equal assigns(:ad).category, category
  end

  def test_should_update_category
    put :update, :id => ads(:one), :ad => { 
      :description => ads(:two).description,
    }
    assert_response :redirect
    assert_equal assigns(:ad).description, ads(:two).description
    assert_equal assigns(:ad).category, ads(:one).category
  end

  def test_should_find_vacation_home_on_subpage
    get :sub, :type => 'vacation'
    assert_select 'td', :text => ads(:one).description
  end
end

