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_a).to eq Flyboy::Task.all.to_a 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 context "when sorting" do before do Flyboy::Goal.destroy_all Flyboy::Task.destroy_all @goal1 = FactoryGirl.create(:flyboy_goal, title: "Abc") @goal2 = FactoryGirl.create(:flyboy_goal, title: "dEF") @goal3 = FactoryGirl.create(:flyboy_goal, title: "xyz") @task1 = FactoryGirl.create(:flyboy_task, goal: @goal1, title: "Abc", progress: 100, term: "21/12/2012", reminder: "21/12/2012") @task2 = FactoryGirl.create(:flyboy_task, goal: @goal2, title: "dEF", progress: 0, term: "23/12/2012", reminder: "23/12/2012") @task3 = FactoryGirl.create(:flyboy_task, goal: @goal3, title: "xyz", progress: 35, term: "22/12/2012", reminder: "22/12/2012") end it "sorting by goal asc" do get :index, sort: "goal" expect(assigns(:tasks).to_a).to eq [@task1, @task2, @task3] end it "sorting by goal desc" do get :index, sort: "-goal" expect(assigns(:tasks).to_a).to eq [@task3, @task2, @task1] end it "sorting by title asc" do get :index, sort: "title" expect(assigns(:tasks).to_a).to eq [@task1, @task2, @task3] end it "sorting by title desc" do get :index, sort: "-title" expect(assigns(:tasks).to_a).to eq [@task3, @task2, @task1] end it "sorting by progress asc" do get :index, sort: "progress" expect(assigns(:tasks).to_a).to eq [@task2, @task3, @task1] end it "sorting by progress desc" do get :index, sort: "-progress" expect(assigns(:tasks).to_a).to eq [@task1, @task3, @task2] end it "sorting by term asc" do get :index, sort: "term" expect(assigns(:tasks).to_a).to eq [@task1, @task3, @task2] end it "sorting by term desc" do get :index, sort: "-term" expect(assigns(:tasks).to_a).to eq [@task2, @task3, @task1] 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