require 'rails_helper'

# This spec was generated by rspec-rails when you ran the scaffold generator.
# It demonstrates how one might use RSpec to specify the controller code that
# was generated by the Rails when you ran the scaffold generator.

RSpec.describe ProjectsController do

  def mock_project(stubs={})
    @mock_project ||= Project.new(id: 99, name: 'test123')
  end

  describe "GET index" do
    it "assigns all projects as @projects" do
      expect(Project).to receive(:all) { [mock_project] }
      get :index
      expect(assigns(:projects)).to eq([mock_project])
    end
  end

  describe "GET show" do
    it "assigns the requested project as @project" do
      project = Project.create(name: 'TEST')
      get :show, params: {id: project.id }
      expect(assigns(:project)).to eq(project)
    end
  end

  describe "GET new" do
    it "assigns a new project as @project" do
      saved_mock = mock_project
      expect(Project).to receive(:new) { saved_mock }
      get :new
      expect(assigns(:project)).to eq(mock_project)
    end
  end

  describe "GET edit" do
    it "assigns the requested project as @project" do
      expect(Project).to receive(:find).with("37") { mock_project }
      get :edit, params: {id: "37"}
      expect(assigns(:project)).to eq(mock_project)
    end
  end

  describe "POST create" do
    describe "with valid params" do
      before do
        saved_mock = mock_project
        expect(Project).to receive(:new).with(ActionController::Parameters.new({'name' => 'Test123'}).permit!) { mock_project }
        post :create, params: {project: {'name' => 'Test123'}}
      end
      it "assigns a newly created project as @project" do
        expect(assigns(:project)).to eq(mock_project)
      end

      it "redirects to the created project" do
        expect(response).to redirect_to(project_url(assigns(:project)))
      end
    end

    describe "with invalid params" do
      before do
        post :create, params: {project: {'name' => ''}}
      end
      it "assigns a newly created but unsaved project as @project" do
        expect(assigns(:project).name).to be_blank
      end

      it "re-renders the 'new' template" do
        expect(response).to render_template("new")
      end
    end
  end

  describe "PUT update" do
    describe "with valid params" do
      before do

      end
      it "updates the requested project" do
        expect(Project).to receive(:find).with("37") { mock_project }
        expect(mock_project).to receive(:update_attributes).with(ActionController::Parameters.new({'name' => 'new-name'}).permit!)
        put :update, params: {id: "37", project: {name: 'new-name'}}
      end

      it "assigns the requested project as @project" do
        expect(Project).to receive(:find) { mock_project }
        put :update, params: {id: "37", project: {name: 'new-name'}}
        expect(assigns(:project)).to eq(mock_project)
      end

      it "redirects to the project" do
        expect(Project).to receive(:find) { mock_project }
        put :update, params: {id: "37", project: {name: 'new-name'}}
        expect(response).to redirect_to(project_url(mock_project))
      end
    end

    describe "with invalid params" do
      it "assigns the project as @project" do
        expect(Project).to receive(:find) { mock_project }
        put :update, params: { id: "1", project: {name: ''} }
        expect(assigns(:project)).to eq(mock_project)
      end

      it "re-renders the 'edit' template" do
        expect(Project).to receive(:find) { mock_project }
        put :update, params: { id: "1", project: {name: ''} }
        expect(response).to render_template("edit")
      end
    end
  end

  describe "DELETE destroy" do
    it "destroys the requested project" do
      expect(Project).to receive(:find).with("37") { mock_project }
      expect(mock_project).to receive(:destroy)
      delete :destroy, params: {id: "37" }
    end

    it "redirects to the projects list" do
      expect(Project).to receive(:find) { mock_project }
      delete :destroy, params: {id: "1"}
      expect(response).to redirect_to(projects_url)
    end
  end

end
