Files
grecia/spec/system/admin/legislation/questions_spec.rb
Javi Martín 40d89ee47c Add missing expectations before calls to visit
There were many cases where we were clicking on a link or (most of the
time) a button and then calling the `visit` method. In the past, it
worked just fine because clicking on buttons usually results in non-AJAX
requests, meaning that the test waited for the request to finish before
continuing.

That's no longer the case, though. In the last few months/years (not
sure since when) we're getting sporadic failures because the test
doesn't wait for the request to finish before making another request
with the `visit` method. This sometimes results in flaky tests.

Some of these tests have recently failed in our CI. Here are a few
examples (note the numbers don't follow an order because these tests
failed in different jobs):

```
1) Admin edit translatable records Current locale translation does not
   exist For ActivePoll Shows first available fallback
   Failure/Error: expect(page).to have_content "Sondage en Français"

     expected to find text "Sondage en Français" in "Language: \n
       \nEnglish\nDeutsch\nEspañol\nFrançais\nNederlands\nPortuguês
       brasileiro\n中文\n       Go back to CONSUL DEMOCRACY\nCONSUL
       DEMOCRACY\nADMINISTRATION\nMenu\nNotifications\nMy content\nMy
       account\nSign out\nProposals\nDebates\nComments\nPolls\n
       Collaborative Legislation\nParticipatory budgets\nVoting booths
       \nSignature Sheets\nMessages to users\nSite content\nModerated
       content\nProfiles\nStatistics\nSettings\nProposals dashboard\n×
       \nPolls description updated successfully.\nList of polls\nPolls
       description\nCreate poll\nThere are no polls."

2) Public area translatable records Existing records Update a
   translation With valid data Changes the existing translation
   Failure/Error: expect(page).to have_field "Debate title",
                  with: "Title in English"
     expected to find field "Debate title" that is not disabled but
     there were no matches

2) Admin collaborative legislation Update Edit milestones summary
     Failure/Error: expect(page).to have_content "There is still a long
                                                 journey ahead of us"
       expected to find text "There is still a long journey ahead of us"
        in "Language: \n
        \nEnglish\nDeutsch\nEspañol\nFrançais\nNederlands\nPortuguês
        brasileiro\n中文\n       Go back to CONSUL DEMOCRACY\nCONSUL
        DEMOCRACY\nADMINISTRATION\nMenu\nNotifications\nMy content\nMy
        account\nSign out\nProposals\nDebates\nComments\nPolls\n
        Collaborative Legislation\nParticipatory budgets\nVoting booths
        \nSignature Sheets\nMessages to users\nSite content\nModerated
        content\nProfiles\nStatistics\nSettings\nProposals dashboard\n×
        \nProcess updated successfully. Click to visit\nBack\nAn example
        legislation process\nInformation\nHomepage\nDebate\nProposals\n
        Drafting\nFollowing\n1 language in use\nCurrent language\n
        English\nSummary\n    Format\n    ◢\n Milestone\nManage progress
        bars\nDon't have defined milestones\nCreate new milestone".
        (However, it was found 1 time including non-visible text.)

3) Admin collaborative legislation SDG related list create Collaborative
   Legislation with sdg related list
     Failure/Error:
       within("tr", text: "Legislation process with SDG related content") do
         expect(page).to have_css "td", exact_text: "17"
       end

     Capybara::ElementNotFound:
       Unable to find css "tr"

4) Valuation budget investments Valuate Feasibility can be marked as
   pending
   Failure/Error: expect(find("#budget_investment_feasibility_undecided"))
                  .not_to be_checked

    Capybara::ElementNotFound:
      Unable to find css "#budget_investment_feasibility_undecided"

3) Custom information texts Show custom texts instead of default ones
   Failure/Error:
     within("#section_help") do
       expect(page).to have_content "Custom help with debates"
       expect(page).not_to have_content "Help with debates"
     end

4) Admin budgets Update Deselect all selected staff
   Failure/Error: expect(page).to have_link "Select administrators"
     expected to find link "Select administrators" but there were no
     matches

3) Admin polls SDG related list edit poll with sdg related list
   Failure/Error:
     within("tr", text: "Upcoming poll with SDG related content") do
       expect(page).to have_css "td", exact_text: "17"
     end

   Capybara::ElementNotFound:
     Unable to find css "tr"

4) Admin polls SDG related list create poll with sdg related list
   Failure/Error:
     within("tr", text: "Upcoming poll with SDG related content") do
       expect(page).to have_css "td", exact_text: "17"
     end

   Capybara::ElementNotFound:
     Unable to find css "tr"

5) Admin custom images Image is replaced on admin newsletters
     Failure/Error:
       within(".newsletter-body-content") do
         expect(page).to have_css("img[src*='logo_email_custom.png']")
       end

     Capybara::ElementNotFound:
       Unable to find css ".newsletter-body-content"

6) Admin custom images Image is replaced on front views
     Failure/Error:
       within("#map") do
         expect(page).to
           have_css("img[src*='custom_map.jpg'][alt='Districts list']")
       end

     Capybara::ElementNotFound:
       Unable to find css "#map"
```
2025-03-26 16:27:07 +01:00

200 lines
6.0 KiB
Ruby

require "rails_helper"
describe "Admin legislation questions", :admin do
let!(:process) { create(:legislation_process, title: "An example legislation process") }
context "Index" do
scenario "Displaying legislation process questions" do
create(:legislation_question, process: process, title: "Question 1")
create(:legislation_question, process: process, title: "Question 2")
visit admin_legislation_processes_path(filter: "all")
within("tr", text: "An example legislation process") { click_link "Edit" }
click_link "Debate"
expect(page).to have_content("Question 1")
expect(page).to have_content("Question 2")
end
end
context "Create" do
scenario "Valid legislation question" do
visit admin_root_path
within("#side_menu") do
click_link "Collaborative Legislation"
end
within("tr", text: "An example legislation process") { click_link "Edit" }
click_link "Debate"
click_link "Create question"
fill_in "Question", with: "Question 3"
fill_in_ckeditor "Description", with: "A little description about question 3"
click_button "Create question"
expect(page).to have_content "Question 3"
end
end
context "Update" do
scenario "Valid legislation question" do
create(:legislation_question, title: "Question 2", description: "Description 2", process: process)
visit admin_root_path
within("#side_menu") do
click_link "Collaborative Legislation"
end
click_link "All"
expect(page).not_to have_link "All"
within("tr", text: "An example legislation process") { click_link "Edit" }
click_link "Debate"
click_link "Question 2"
fill_in "Question", with: "Question 2b"
fill_in_ckeditor "Description", with: "Description 2b"
click_button "Save changes"
expect(page).to have_content "Question 2b"
expect(page).to have_ckeditor "Description", with: "Description 2b"
end
end
context "Delete" do
scenario "Legislation question" do
create(:legislation_question, title: "Question 1", process: process)
question = create(:legislation_question, title: "Question 2", process: process)
question_option = create(:legislation_question_option, question: question, value: "Yes")
create(:legislation_answer, question: question, question_option: question_option)
visit edit_admin_legislation_process_question_path(process, question)
accept_confirm("Are you sure? This action will delete \"Question 2\" and can't be undone.") do
click_button "Delete"
end
expect(page).to have_content "Questions"
expect(page).to have_content "Question 1"
expect(page).not_to have_content "Question 2"
end
end
context "Legislation options" do
let!(:question) { create(:legislation_question) }
let(:edit_question_url) do
edit_admin_legislation_process_question_path(question.process, question)
end
let(:field_en) { fields_for(:en).first }
let(:field_es) { fields_for(:es).first }
def fields_for(locale)
within("#nested_question_options") do
page.all(
"[data-locale='#{locale}'] [id^='legislation_question_question_option'][id$='value']"
)
end
end
scenario "Edit an existing option" do
create(:legislation_question_option, question: question, value: "Original")
visit edit_question_url
find("#nested_question_options input").set("Changed")
click_button "Save changes"
expect(page).not_to have_css "#error_explanation"
visit edit_question_url
expect(page).to have_field(field_en[:id], with: "Changed")
end
scenario "Remove an option" do
create(:legislation_question_option, question: question, value: "Yes")
create(:legislation_question_option, question: question, value: "No")
visit edit_question_url
expect(page).to have_field fields_for(:en).first[:id], with: "Yes"
expect(page).to have_field fields_for(:en).last[:id], with: "No"
page.first(:link, "Remove option").click
expect(page).not_to have_field fields_for(:en).first[:id], with: "Yes"
expect(page).to have_field fields_for(:en).last[:id], with: "No"
click_button "Save changes"
expect(page).to have_content "Question updated successfully"
visit edit_question_url
expect(page).not_to have_field fields_for(:en).first[:id], with: "Yes"
expect(page).to have_field fields_for(:en).last[:id], with: "No"
end
context "Special translation behaviour" do
before do
question.update!(title_en: "Title in English", title_es: "Título en Español")
end
scenario "Add translation for question option" do
visit edit_question_url
click_link "Add option"
find("#nested_question_options input").set("Option 1")
select "Español", from: "Current language"
find("#nested_question_options input").set("Opción 1")
click_button "Save changes"
expect(page).to have_content "Question updated successfully"
visit edit_question_url
expect(page).to have_field(field_en[:id], with: "Option 1")
select "Español", from: "Current language"
expect(page).to have_field(field_es[:id], with: "Opción 1")
end
scenario "Add new question option after changing active locale" do
visit edit_question_url
select "Español", from: "Current language"
click_link "Add option"
find("#nested_question_options input").set("Opción 1")
select "English", from: "Current language"
find("#nested_question_options input").set("Option 1")
click_button "Save changes"
expect(page).to have_content "Question updated successfully"
visit edit_question_url
expect(page).to have_field(field_en[:id], with: "Option 1")
select "Español", from: "Current language"
expect(page).to have_field(field_es[:id], with: "Opción 1")
end
end
end
end