Files
grecia/spec/system/translatable_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

259 lines
7.9 KiB
Ruby

require "rails_helper"
describe "Public area translatable records" do
let(:user) { create(:user, :in_census) }
before do
Setting["feature.translation_interface"] = true
login_as(user)
end
context "New records" do
scenario "Add only single translation at once" do
visit new_debate_path
fill_in_new_debate_title with: "Who won the debate?"
fill_in_ckeditor "Initial debate text", with: "And who will win this debate?"
check "debate_terms_of_service"
click_button "Start a debate"
expect(page).to have_content "Debate created successfully"
end
scenario "Add single translation maintains introduced field values" do
visit new_proposal_path
fill_in_new_proposal_title with: "Olympic Games in Melbourne"
fill_in "Proposal summary", with: "Full proposal for our candidature"
fill_in_ckeditor "Proposal text", with: "2032 will make Australia famous again"
check "proposal_terms_of_service"
click_button "Create proposal"
expect(page).to have_content "Proposal created successfully"
expect(page).to have_content "Olympic Games in Melbourne"
expect(page).to have_content "Full proposal for our candidature"
expect(page).to have_content "2032 will make Australia famous again"
end
scenario "Add multiple translations at once" do
budget = create(:budget_heading, name: "Everywhere").group.budget
visit new_budget_investment_path(budget)
fill_in_new_investment_title with: "My awesome project"
fill_in_ckeditor "Description", with: "Everything is awesome!"
select "Français", from: "Add language"
fill_in_new_investment_title with: "Titre en Français"
fill_in_ckeditor "Description", with: "Contenu en Français"
check "budget_investment_terms_of_service"
click_button "Create Investment"
expect(page).to have_content "Budget Investment created successfully"
end
scenario "Add only single translation at once not having the current locale" do
visit new_proposal_path
click_link "Remove language"
select "Français", from: "Add language"
fill_in_new_proposal_title with: "Titre en Français"
fill_in "Proposal summary", with: "Résumé en Français"
check "proposal_terms_of_service"
click_button "Create proposal"
expect(page).to have_content "Proposal created successfully"
end
scenario "Add a translation for a locale with non-underscored name" do
budget = create(:budget_heading, name: "Everywhere").group.budget
visit new_budget_investment_path(budget)
click_link "Remove language"
select "Português brasileiro", from: "Add language"
fill_in_new_investment_title with: "Titre en Français"
fill_in_ckeditor "Description", with: "Contenu en Français"
check "budget_investment_terms_of_service"
click_button "Create Investment"
expect(page).to have_content "Budget Investment created successfully"
end
scenario "Add an invalid translation" do
visit new_debate_path
check "debate_terms_of_service"
click_button "Start a debate"
expect(page).to have_css "#error_explanation"
expect(page).to have_field "Debate title", with: "", class: "is-invalid-input"
end
scenario "Shows errors when submiting without any active translations" do
budget = create(:budget_heading, name: "Everywhere").group.budget
visit new_budget_investment_path(budget)
click_link "Remove language"
check "budget_investment_terms_of_service"
click_button "Create Investment"
expect(page).to have_css "#error_explanation"
expect(page).to have_field "Title", with: ""
end
end
context "Globalize javascript interface" do
scenario "Highlight current locale" do
visit new_debate_path
expect_to_have_language_selected "English"
end
scenario "Highlight new locale added" do
visit new_proposal_path
select "Español", from: "Language:"
expect_to_have_language_selected "Español"
end
scenario "Select a locale and add it to the form" do
visit new_budget_investment_path(create(:budget))
select "Français", from: "Add language"
expect(page).to have_field "Title", with: ""
end
scenario "Remove a translation" do
visit new_budget_investment_path(create(:budget))
expect(find("#select_language").value).to eq "en"
click_link "Remove language"
expect_not_to_have_language("English")
end
context "Languages in use" do
scenario "Show default description" do
visit new_debate_path
expect(page).to have_content "1 language in use"
end
scenario "Increase description count after add new language" do
visit new_proposal_path
select "Español", from: "Add language"
expect(page).to have_content "2 languages in use"
end
scenario "Decrease description count after remove a language" do
visit new_proposal_path
click_link "Remove language"
expect(page).to have_content "0 languages in use"
end
end
context "When translation interface feature setting" do
scenario "Is enabled translation interface should be rendered" do
visit new_budget_investment_path(create(:budget))
expect(page).to have_css ".globalize-languages"
end
scenario "Is disabled translation interface should not be rendered" do
Setting["feature.translation_interface"] = nil
visit new_debate_path
expect(page).not_to have_css ".globalize-languages"
end
end
end
context "Existing records" do
before { translatable.update(attributes.merge(author: user)) }
let(:attributes) do
translatable.translated_attribute_names.product(%i[en es]).to_h do |field, locale|
[:"#{field}_#{locale}", text_for(field, locale)]
end
end
context "Update a translation" do
context "With valid data" do
let(:translatable) { create(:debate) }
let(:path) { edit_debate_path(translatable) }
scenario "Changes the existing translation" do
visit path
select "Español", from: "Current language"
fill_in "Debate title", with: "Título corregido"
fill_in_ckeditor "Initial debate text", with: "Texto corregido"
click_button "Save changes"
expect(page).to have_content "Debate updated successfully"
visit path
expect(page).to have_field "Debate title", with: "Title in English"
select "Español", from: "Language:"
expect(page).to have_field "Título del debate", with: "Título corregido"
expect(page).to have_ckeditor "Texto inicial del debate", with: "Texto corregido"
end
end
context "Update a translation with invalid data" do
let(:translatable) { create(:proposal) }
scenario "Show validation errors" do
visit edit_proposal_path(translatable)
select "Español", from: "Current language"
expect(page).to have_field "Proposal title", with: "Título en español"
fill_in "Proposal title", with: ""
click_button "Save changes"
expect(page).to have_css "#error_explanation"
select "Español", from: "Current language"
expect(page).to have_field "Proposal title", with: "", class: "is-invalid-input"
end
end
end
context "Globalize javascript interface" do
let(:translatable) { create(:debate) }
let(:path) { edit_debate_path(translatable) }
scenario "Is rendered with translation interface feature enabled" do
visit path
expect(page).to have_css ".globalize-languages"
end
scenario "Is not rendered with translation interface feature disabled" do
Setting["feature.translation_interface"] = nil
visit path
expect(page).not_to have_css ".globalize-languages"
end
end
end
end