require 'spec_helper'

describe "Surveys" do
  include Rapidfire::QuestionSpecHelper
  include Rapidfire::AnswerSpecHelper

  let(:survey)  { FactoryBot.create(:survey, name: "Question Set") }
  let(:question1)  { FactoryBot.create(:q_long,  survey: survey, question_text: "Long Question")  }
  let(:question2)  { FactoryBot.create(:q_short, survey: survey, question_text: "Short Question") }
  before do
    [question1, question2]
  end

  describe "INDEX surveys" do
    before do
      visit rapidfire.root_path
    end

    it "lists all surveys" do
      expect(page).to have_content survey.name
    end
  end

  describe "DELETE surveys" do
    context "when user can administer" do
      before do
        allow_any_instance_of(ApplicationController).to receive(:can_administer?).and_return(true)
        visit rapidfire.root_path
        click_button "Delete"
      end

      it "deletes the survey" do
        expect(page).not_to have_content survey.name
      end
    end

    context "when user cannot administer" do
      before do
        allow_any_instance_of(ApplicationController).to receive(:can_administer?).and_return(false)
        visit rapidfire.root_path
      end

      it "doesn't show option to delete question group" do
        expect(page).not_to have_link "Delete"
      end
    end
  end

  describe "CREATING Survey" do
    context "when user can create groups" do
      before do
        allow_any_instance_of(ApplicationController).to receive(:can_administer?).and_return(true)

        visit rapidfire.root_path
        click_link "New Survey"
      end

      context "when name is present" do
        before do
          page.within("#new_survey") do
            fill_in "survey_name", with: "New Survey"
            click_button "Create Survey"
          end
        end

        it "creates question group" do
          expect(page).to have_content "New Survey"
        end
      end

      context "when name is not present" do
        before do
          page.within("#new_survey") do
            click_button "Create Survey"
          end
        end

        it "fails to create question group" do
          page.within("#new_survey") do
            expect(page).to have_content "can't be blank"
          end
        end
      end
    end

    context "when user cannot create groups" do
      before do
        allow_any_instance_of(ApplicationController).to receive(:can_administer?).and_return(false)
        visit rapidfire.root_path
      end

      it "page shouldnot have link to create groups" do
        expect(page).not_to have_link "New Survey"
      end
    end

    context "when creating a survey fails" do
      before do
        allow_any_instance_of(ApplicationController).to receive(:can_administer?).and_return(true)
      end

      it "throws an error message" do
        visit rapidfire.root_path
        click_link "New Survey"

        click_button "Create Survey"

        expect(page).to have_content "Name can't be blank"
      end
    end
  end

  describe "EDITING Surveys" do
    context "when user can manage questions" do
      before do
        allow_any_instance_of(ApplicationController).to receive(:can_administer?).and_return(true)

        visit rapidfire.root_path
        click_link survey.name
      end

      it "shows set of questions" do
        expect(page).to have_content question1.question_text
        expect(page).to have_content question2.question_text
      end
    end

    context "when user cannot manage questions" do
      before do
        allow_any_instance_of(ApplicationController).to receive(:can_administer?).and_return(false)
      end

      it "fails to access the page" do
        expect(page).not_to have_link survey.name
      end
    end
  end

  describe "GET survey results" do
    before do
      create_questions(survey)
      create_answers

      visit rapidfire.root_path
      page.within("#survey_#{survey.id}") do
        click_link "Results"
      end
    end

    it "shows results for all attempts" do
      survey.attempts.each do |attempt|
        expect(page).to have_content "User: #{attempt.user_id}"
        expect(page).to have_link "View Answers", href: result_survey_path(survey, attempt.id)
      end
    end

    context "when viewing an individual attempt" do
      let(:attempt) { survey.attempts.first || survey.attempts.create(user_id: 1) }

      before do
        visit rapidfire.survey_result_path(survey, attempt.id)
      end

      it "displays the questions and answers for the attempt" do
        attempt.answers.each do |answer|
          expect(page).to have_content answer.question.question_text
          expect(page).to have_content answer.answer_text
        end
      end
    end
  end
end
