require 'spec_helper'

describe ApiReviewsController, type: :controller do
  render_views
  before(:all) do
    @logged_in_user = FactoryGirl.create(:user)
  end

  before(:each) do
    Review.destroy_all
    ReviewSummary.destroy_all
    @user= FactoryGirl.create(:user)
    @target= FactoryGirl.create(:jam_track)
    controller.current_user = @logged_in_user
  end

  after(:all) do
    Review.destroy_all
    ReviewSummary.destroy_all
    User.destroy_all
    JamTrack.destroy_all
  end

  describe "create" do
    it "successful" do
      post :create, rating:3, description:"it was ok", target_id: @target.id, target_type:"JamRuby::JamTrack", format: 'json'
      response.should be_success
      Review.index.should have(1).items
    end
  end

  describe "update" do
    before :each do
      @review=Review.create!(target:@target, rating:5, description: "blah", user_id: @logged_in_user.id)
    end

    it "basic" do
      post :update, id:@review.id, mods: {rating:4, description: "not blah"}, :format=>'json'
      response.should be_success
      @review.reload
      @review.rating.should eq(4)
      @review.description.should eq("not blah")
    end

    it "bad identifier" do
      post :update, id:2112, mods: {rating:4, description: "not blah"}, :format=>'json'
      response.status.should eql(404)
    end
  end

  describe "delete" do
    before :each do
      @review=Review.create!(target:@target, rating:5, description: "blah", user_id: @logged_in_user.id)
    end

    it "marks review as deleted" do
      delete :delete, id:@review.id
      response.should be_success
      Review.index.should have(0).items
      Review.index(include_deleted:true).should have(1).items
    end
  end

  describe "indexes" do
    before :each do
      @target2=FactoryGirl.create(:jam_track)

      7.times { Review.create!(target:@target, rating:4, description: "blah", user_id: FactoryGirl.create(:user).id) }
      5.times { Review.create!(target:@target2, rating:4, description: "blah", user_id: FactoryGirl.create(:user).id) }
    end

    it "review summaries" do
      get :index, format: 'json'
      response.should be_success
      json = JSON.parse(response.body)

      ReviewSummary.index.should have(2).items
      get :index, format: 'json'
      json = JSON.parse(response.body)
      json.should have(2).item
    end

    it "details" do
      ReviewSummary.index.should have(2).items

      summaries = ReviewSummary.index
      get :details, :review_summary_id=>summaries[0].id, format: 'json'
      response.should be_success
      json = JSON.parse(response.body)
      json.should have(7).items

      get :details, :review_summary_id=>summaries[1].id, format: 'json'
      response.should be_success
      json = JSON.parse(response.body)
      json.should have(5).items
    end

    it "paginates details" do
      summaries = ReviewSummary.index
      summaries.should have(2).items

      get :details, review_summary_id:summaries[0].id, page: 1, per_page: 3, format: 'json'
      response.should be_success
      json = JSON.parse(response.body)
      json.should have(3).items

      get :details, review_summary_id:summaries[0].id, page: 3, per_page: 3, format: 'json'
      response.should be_success
      json = JSON.parse(response.body)
      json.should have(1).items
    end
  end
end
