<commit_before>require "rails_helper"

describe "Polls" do
  let!(:proposal) { create(:proposal, :draft) }

  before do
    login_as(proposal.author)
    visit proposal_dashboard_path(proposal)
  end

  scenario "Has a link to polls feature" do
    expect(page).to have_link("Polls")
  end

  scenario "Create a poll" do
    click_link "Polls"
    click_link "Create poll"

    start_date = 1.week.from_now
    end_date = 2.weeks.from_now

    fill_in "poll_name", with: "Proposal poll"
    fill_in "poll_starts_at", with: start_date.strftime("%d/%m/%Y")
    fill_in "poll_ends_at", with: end_date.strftime("%d/%m/%Y")
    fill_in "poll_description", with: "Proposal's poll description. This poll..."

    expect(page).not_to have_css("#poll_results_enabled")
    expect(page).not_to have_css("#poll_stats_enabled")

    click_link "Add question"

    fill_in "Question", with: "First question"

    click_link "Add answer"

    fill_in "Answer", with: "First answer"

    click_button "Create poll"
    expect(page).to have_content "Poll created successfully"
    expect(page).to have_content "Proposal poll"
    expect(page).to have_content I18n.l(start_date.to_date)
  end

  describe "Datepicker" do
    scenario "displays the expected format when changing the date field" do
      visit new_proposal_dashboard_poll_path(proposal)

      fill_in "Start Date", with: "20/02/2002"
      find_field("Start Date").click
      within(".ui-datepicker") { click_link "22" }

      expect(page).to have_field "Start Date", with: "22/02/2002"
    end

    scenario "is closed after using the browser back button" do
      visit proposal_dashboard_polls_path(proposal)

      click_link "Create poll"
      find_field("Start Date").click

      expect(page).to have_css "#ui-datepicker-div"

      go_back

      expect(page).to have_link "Create poll"
      expect(page).not_to have_css "#ui-datepicker-div"
    end

    scenario "works after using the browser back button" do
      visit new_proposal_dashboard_poll_path(proposal)
      click_link "Polls"

      expect(page).to have_link "Create poll"

      go_back
      find_field("Start Date").click

      expect(page).to have_css "#ui-datepicker-div"
    end
  end

  scenario "Create a poll redirects back to form when invalid data" do
    click_link "Polls"
    click_link "Create poll"

    click_button "Create poll"

    expect(page).to have_content("New poll")
  end

  scenario "Edit poll is allowed for upcoming polls" do
    poll = create(:poll, related: proposal, starts_at: 1.week.from_now)

    visit proposal_dashboard_polls_path(proposal)

    within "div#poll_#{poll.id}" do
      expect(page).to have_content("Edit survey")

      click_link "Edit survey"
    end

    click_button "Update poll"

    expect(page).to have_content "Poll updated successfully"
  end

  scenario "Edit poll redirects back when invalid data" do
    poll = create(:poll, related: proposal, starts_at: 1.week.from_now)

    visit proposal_dashboard_polls_path(proposal)<commit_msg>Reduce number of requests in dashboard poll tests<commit_after>require "rails_helper"

describe "Polls" do
  let!(:proposal) { create(:proposal, :draft) }

  before { login_as(proposal.author) }

  scenario "Has a link to polls feature" do
    visit proposal_dashboard_path(proposal)

    expect(page).to have_link("Polls")
  end

  scenario "Create a poll" do
    visit proposal_dashboard_path(proposal)
    click_link "Polls"
    click_link "Create poll"

    start_date = 1.week.from_now
    end_date = 2.weeks.from_now

    fill_in "poll_name", with: "Proposal poll"
    fill_in "poll_starts_at", with: start_date.strftime("%d/%m/%Y")
    fill_in "poll_ends_at", with: end_date.strftime("%d/%m/%Y")
    fill_in "poll_description", with: "Proposal's poll description. This poll..."

    expect(page).not_to have_css("#poll_results_enabled")
    expect(page).not_to have_css("#poll_stats_enabled")

    click_link "Add question"

    fill_in "Question", with: "First question"

    click_link "Add answer"

    fill_in "Answer", with: "First answer"

    click_button "Create poll"
    expect(page).to have_content "Poll created successfully"
    expect(page).to have_content "Proposal poll"
    expect(page).to have_content I18n.l(start_date.to_date)
  end

  describe "Datepicker" do
    scenario "displays the expected format when changing the date field" do
      visit new_proposal_dashboard_poll_path(proposal)

      fill_in "Start Date", with: "20/02/2002"
      find_field("Start Date").click
      within(".ui-datepicker") { click_link "22" }

      expect(page).to have_field "Start Date", with: "22/02/2002"
    end

    scenario "is closed after using the browser back button" do
      visit proposal_dashboard_polls_path(proposal)

      click_link "Create poll"
      find_field("Start Date").click

      expect(page).to have_css "#ui-datepicker-div"

      go_back

      expect(page).to have_link "Create poll"
      expect(page).not_to have_css "#ui-datepicker-div"
    end

    scenario "works after using the browser back button" do
      visit new_proposal_dashboard_poll_path(proposal)
      click_link "Polls"

      expect(page).to have_link "Create poll"

      go_back
      find_field("Start Date").click

      expect(page).to have_css "#ui-datepicker-div"
    end
  end

  scenario "Create a poll redirects back to form when invalid data" do
    visit proposal_dashboard_path(proposal)
    click_link "Polls"
    click_link "Create poll"

    click_button "Create poll"

    expect(page).to have_content("New poll")
  end

  scenario "Edit poll is allowed for upcoming polls" do
    poll = create(:poll, related: proposal, starts_at: 1.week.from_now)

    visit proposal_dashboard_polls_path(proposal)

    within "div#poll_#{poll.id}" do
      expect(page).to have_content("Edit survey")

      click_link "Edit survey"
    end

    click_button "Update poll"

    expect(page).to have_content "Poll updated successfully"
  end

  scenario "Edit poll redirects back when invalid data" do
    poll = create(:poll, related: proposal, starts_at: 1.week.from_now)

    visit proposal_dashboard_polls_path(proposal)
{'fraction_non_alphanumeric': 0.09886547811993517, 'fraction_numerical': 0.007779578606158833, 'mean_word_length': 3.6363636363636362, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3293442', 'n_tokens_mistral': 2047, 'n_tokens_neox': 1947, 'n_words': 528}