require "spec_helper" describe Flyboy::TasksController, type: :controller do routes { Flyboy::Engine.routes } let!(:task) { FactoryGirl.create(:flyboy_task, done: false) } let!(:task2) { FactoryGirl.create(:flyboy_task, goal: task.goal, done: true) } let(:valid_attributes) { { title: "New Task" , goal_id: task.goal.id, reminder: Date.today, term: Date.today} } describe "#complete" do before(:each) do request.env["HTTP_REFERER"] = "where_i_came_from" end it "should mark the task as done" do patch :complete, {id: task.id} task.reload.done.should be true end it "should set progress to 100" do patch :complete, {id: task.id} task.reload.progress.should eq(100) end it "should add a task_comment" do count = task.comments.count patch :complete, {id: task.id} task.reload.comments.count.should eq(count+1) task.comments.last.progress.should eq(100) end it "should redirect to the referer page" do patch :complete, {id: task.id} response.should redirect_to "where_i_came_from" end end describe "GET index" do it "assigns all tasks as @tasks" do get :index, {} expect(assigns(:tasks)).to eq Flyboy::Task.all end context "when applying filter" do before do Flyboy::Task.destroy_all @task1 = FactoryGirl.create(:flyboy_task, done: true) @task2 = FactoryGirl.create(:flyboy_task, done: false) end it 'should display both when not filtered' do get :index assigns(:tasks).should eq [@task1, @task2] end it 'should filter by status closed' do Flyboy::SmallData::FilterForTasks.new(request.cookies).store({'status' => "closed"}) get :index assigns(:tasks).should eq [@task1] end it 'should filter by status opened' do Flyboy::SmallData::FilterForTasks.new(request.cookies).store({'status' => "opened"}) get :index assigns(:tasks).should eq [@task2] end end end describe "GET show" do it "assigns the requested task as @task" do get :show, {:id => task.to_param} assigns(:task).should eq(task) end end describe "GET new" do it "assigns a new task as @task" do get :new, {:goal_id => task.goal.id} assigns(:task).should be_a_new(Flyboy::Task) end end describe "GET edit" do it "assigns the requested task as @task" do get :edit, {:id => task.to_param} assigns(:task).should eq(task) end end describe "POST create" do describe "with valid params" do it "creates a new Task" do expect { post :create, {:task => valid_attributes} }.to change(Flyboy::Task, :count).by(1) end it "assigns a newly created task as @task" do post :create, {:task => valid_attributes} assigns(:task).should be_a(Flyboy::Task) assigns(:task).should be_persisted end it "redirects to the created task" do post :create, {:task => valid_attributes} response.should redirect_to Flyboy::Task.order("id ASC").last end end describe "with invalid params" do it "assigns a newly created but unsaved task as @task" do post :create, task: {title: nil, goal_id: task.goal.id} assigns(:task).should be_a_new(Flyboy::Task) end end end describe "PUT update" do describe "with valid params" do it "assigns the requested task as @task" do task = Flyboy::Task.create! valid_attributes patch :update, {:id => task.to_param, :task => valid_attributes} assigns(:task).should eq(task) end it "redirects to the task" do task = Flyboy::Task.create! valid_attributes patch :update, {:id => task.to_param, :task => valid_attributes} response.should redirect_to(task) end end describe "with invalid params" do it "assigns the task as @task" do task = Flyboy::Task.create! valid_attributes patch :update, { :id => task.to_param, :task => {:title => nil} } assigns(:task).should eq(task) end end end describe "DELETE destroy" do it "destroys the requested task" do task = Flyboy::Task.create! valid_attributes expect { delete :destroy, {:id => task.to_param} }.to change(Flyboy::Task, :count).by(-1) end it "redirects to the tasks list" do task = Flyboy::Task.create! valid_attributes delete :destroy, {:id => task.to_param} response.should redirect_to(tasks_url) end end describe "snooze" do it "should redirect to the task list to refresh it" do task = Flyboy::Task.create! valid_attributes patch :snooze, {:id => task.to_param} response.should redirect_to(tasks_url) end end end