describe Flyboy::GoalsController, type: :controller do routes { Flyboy::Engine.routes } let(:goal){ FactoryGirl.create(:flyboy_goal) } describe '#index' do context 'when applying filter' do before(:each) do Flyboy::Goal.destroy_all @goal1 = FactoryGirl.create(:flyboy_goal, status: "open") @goal2 = FactoryGirl.create(:flyboy_goal, status: "closed") end it 'should display both when not filtered' do get :index assigns(:goals).should eq [@goal2, @goal1] end it 'should filter by status closed' do Flyboy::SmallData::FilterForGoals.new(request.cookies) .store(status: "closed") get :index assigns(:goals).should eq [@goal2] end it 'should filter by status open' do Flyboy::SmallData::FilterForGoals.new(request.cookies) .store(status: "open") get :index assigns(:goals).should eq [@goal1] end end context "when sorting" do before do Flyboy::Goal.destroy_all @goal1 = FactoryGirl.create(:flyboy_goal, title: "Abc", progress: 100, status: "open") @goal2 = FactoryGirl.create(:flyboy_goal, title: "dEF", progress: 0, status: "closed") @goal3 = FactoryGirl.create(:flyboy_goal, title: "xyz", progress: 35, status: "closed") end it "sorting by title asc" do get :index, sort: "title" expect(assigns(:goals).to_a).to eq [@goal1, @goal2, @goal3] end it "sorting by title desc" do get :index, sort: "-title" expect(assigns(:goals).to_a).to eq [@goal3, @goal2, @goal1] end it "sorting by progress asc" do get :index, sort: "progress" expect(assigns(:goals).to_a).to eq [@goal2, @goal3, @goal1] end it "sorting by progress desc" do get :index, sort: "-progress" expect(assigns(:goals).to_a).to eq [@goal1, @goal3, @goal2] end it "sorting by status asc" do get :index, sort: "status" expect(assigns(:goals).to_a).to eq [@goal2, @goal3, @goal1] end it "sorting by status desc" do get :index, sort: "-status" expect(assigns(:goals).to_a).to eq [@goal1, @goal2, @goal3] end end end describe "#close" do before(:each) do @goal = FactoryGirl.create(:flyboy_goal, status: "open") end it "should close goal" do patch :close, id: @goal expect(@goal.reload.status).to eq "closed" end it "should redirect to goals path" do patch :close, id: @goal expect(response).to redirect_to goals_path end end describe "#open" do before(:each) do @goal = FactoryGirl.create(:flyboy_goal, status: "closed") end it "should open goal" do patch :open, id: @goal expect(@goal.reload.status).to eq "open" end it "should redirect to goal path" do patch :open, id: @goal expect(response).to redirect_to goal_path(@goal) end end end