Files
grecia/spec/system/admin/site_customization/images_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

125 lines
3.5 KiB
Ruby

require "rails_helper"
describe "Admin custom images", :admin do
scenario "Upload valid png image" do
visit admin_root_path
within("#side_menu") do
click_button "Site content"
click_link "Custom images"
end
within("tr#image_logo_header") do
attach_file "logo_header", file_fixture("logo_header.png")
click_button "Update"
end
expect(page).to have_css("tr#image_logo_header img[src*='logo_header.png']")
expect(page).to have_css("img[src*='logo_header.png']", count: 1)
end
scenario "Upload valid jpg image" do
visit admin_site_customization_images_path
within("tr#image_map") do
attach_file "map", file_fixture("custom_map.jpg")
click_button "Update"
end
expect(page).to have_css("tr#image_map img[src*='custom_map.jpg']")
expect(page).to have_css("img[src*='custom_map.jpg']", count: 1)
end
scenario "Image is replaced on front views" do
create(:geozone)
budget = create(:budget)
group = create(:budget_group, budget: budget)
visit admin_site_customization_images_path
within("tr#image_map") do
attach_file "map", file_fixture("custom_map.jpg")
click_button "Update"
end
expect(page).to have_content "Image updated successfully"
visit proposals_path
within("#map") do
expect(page).to have_css("img[src*='custom_map.jpg'][alt='Districts list']")
end
visit map_proposals_path
within(".show-for-medium") do
expect(page).to have_css("img[src*='custom_map.jpg'][alt='Districts list']")
end
visit budget_group_path(budget, group)
within(".show-for-medium") do
expect(page).to have_css("img[src*='custom_map.jpg'][alt='Districts list']")
end
end
scenario "Custom apple touch icon is replaced on front views" do
create(:site_customization_image,
name: "apple-touch-icon-200",
image: fixture_file_upload("apple-touch-icon-custom-200.png"))
visit root_path
expect(page).not_to have_css("link[href*='apple-touch-icon-200']", visible: :all)
expect(page).to have_css("link[href*='apple-touch-icon-custom-200']", visible: :hidden)
end
scenario "Image is replaced on admin newsletters" do
newsletter = create(:newsletter, segment_recipient: "all_users")
visit admin_site_customization_images_path
within("tr#image_logo_email") do
attach_file "logo_email", file_fixture("logo_email_custom.png")
click_button "Update"
end
expect(page).to have_content "Image updated successfully"
visit admin_newsletter_path(newsletter)
within(".newsletter-body-content") do
expect(page).to have_css("img[src*='logo_email_custom.png']")
end
end
scenario "Upload invalid image" do
visit admin_site_customization_images_path
within("tr#image_social_media_icon") do
attach_file "social_media_icon", file_fixture("logo_header.png")
click_button "Update"
end
expect(page).to have_content("Width must be 470px")
expect(page).to have_content("Height must be 246px")
end
scenario "Delete image" do
visit admin_site_customization_images_path
within("tr#image_social_media_icon") do
attach_file "social_media_icon", file_fixture("social_media_icon.png")
click_button "Update"
end
expect(page).to have_css("img[src*='social_media_icon.png']")
within("tr#image_social_media_icon") do
click_button "Delete"
end
expect(page).not_to have_css("img[src*='social_media_icon.png']")
end
end