require "rails_helper"

RSpec.describe Admin::TaskRequestsController, type: :controller do
  render_views

  let!(:current_user) { create(:user, :admin) }

  before do
    sign_in(current_user)
  end

  describe "#index" do
    it "returns a successful response" do
      get :index

      expect(response).to be_successful
    end
  end

  describe "#edit" do
    let(:task_request) { create(:task_request) }

    it "returns a successful response" do
      get :edit, params: { id: task_request }

      expect(response).to be_successful
    end

    it "assigns task request" do
      get :edit, params: { id: task_request }

      expect(assigns(:task_request)).to eq task_request
    end
  end

  describe "#update" do
    let(:task_request) { create(:task_request) }

    it "redirects to task requests page" do
      patch :update, params: { id: task_request, task_request: update_params }

      expect(response).to be_redirect
      expect(response).to redirect_to admin_task_requests_path
    end

    it "sets a flash notice" do
      patch :update, params: { id: task_request, task_request: update_params }

      expect(flash.notice).to eq "The task request has been updated successfully"
    end

    it "updates task request" do
      patch :update, params: { id: task_request, task_request: update_params }

      expect(task_request.reload.status).to eq("completed")
      expect(task_request.reload.deliverable_url).to eq("example.com/deliverables")
    end
  end

  private

  def update_params
    {
      status: "completed",
      deliverable_url: "example.com/deliverables"
    }
  end
end
