Files
grecia/spec/system/admin/homepage/homepage_spec.rb
Javi Martín 2af1fc72f3 Prevent Unable to autoload constant error in tests
When running these tests, under certain conditions, we get a warning
followed by an error:

```
activesupport-6.1.7.7/lib/active_support/dependencies.rb:502:
warning: already initialized constant ActiveStorage::Representations

activesupport-6.1.7.7/lib/active_support/dependencies.rb:502:
warning: previous definition of Representations was here

Failure/Error: raise LoadError, "Unable to autoload constant
'#{qualified_name}', expected #{file_path} to define it"

LoadError: Unable to autoload constant
ActiveStorage::Representations::RedirectController, expected
activestorage-6.1.7.7/app/controllers/active_storage/representations/redirect_controller.rb
to define it
```

The error seems to take place when we request a page in a test that
loads two (or more) ActiveStorage images if ActiveStorage hasn't loaded
yet, although it's a flaky error and so the test doesn't always behave
like this.

We've tested that switching to zeitwerk solves the issue but, since we
aren't switching to zeitwerk in version 2.1.1 and we'd like this version
to run all tests correctly, for now we're changing the tests so they
don't load two records with images.

On of these tests ("Polls Index Polls can be listed") fails on my
machine when run individually. I haven't been able to consistently
reproduce the other ones.
2024-03-02 22:06:25 +01:00

230 lines
6.3 KiB
Ruby

require "rails_helper"
describe "Homepage", :admin do
before do
Setting["homepage.widgets.feeds.proposals"] = false
Setting["homepage.widgets.feeds.debates"] = false
Setting["homepage.widgets.feeds.processes"] = false
Setting["feature.user.recommendations"] = false
end
let!(:proposals_feed) { create(:widget_feed, kind: "proposals") }
let!(:debates_feed) { create(:widget_feed, kind: "debates") }
let!(:processes_feed) { create(:widget_feed, kind: "processes") }
let(:user_recommendations) { Setting.find_by(key: "feature.user.recommendations") }
let(:user) { create(:user) }
context "Header" do
scenario "Admin menu links to homepage path" do
visit new_admin_widget_card_path(header_card: true)
click_link "#{Setting["org_name"]} Administration"
expect(page).to have_current_path(admin_root_path)
end
end
context "Feeds" do
scenario "Proposals" do
5.times { create(:proposal) }
visit admin_homepage_path
within("#widget_feed_#{proposals_feed.id}") do
select "1", from: "widget_feed_limit"
click_button "No"
expect(page).to have_button "Yes"
end
visit root_path
within("#feed_proposals") do
expect(page).to have_content "Most active proposals"
expect(page).to have_css(".proposal", count: 1)
end
expect(page).not_to have_css("#feed_proposals.medium-8")
end
scenario "Debates" do
5.times { create(:debate) }
visit admin_homepage_path
within("#widget_feed_#{debates_feed.id}") do
select "2", from: "widget_feed_limit"
click_button "No"
expect(page).to have_button "Yes"
end
visit root_path
within("#feed_debates") do
expect(page).to have_content "Most active debates"
expect(page).to have_css(".debate", count: 2)
end
expect(page).not_to have_css("#feed_debates.medium-4")
end
scenario "Proposals and debates" do
3.times { create(:proposal) }
3.times { create(:debate) }
visit admin_homepage_path
within("#widget_feed_#{proposals_feed.id}") do
select "3", from: "widget_feed_limit"
click_button "No"
expect(page).to have_button "Yes"
end
within("#widget_feed_#{debates_feed.id}") do
select "3", from: "widget_feed_limit"
click_button "No"
expect(page).to have_button "Yes"
end
visit root_path
within("#feed_proposals") do
expect(page).to have_content "Most active proposals"
expect(page).to have_css(".proposal", count: 3)
end
within("#feed_debates") do
expect(page).to have_content "Most active debates"
expect(page).to have_css(".debate", count: 3)
end
end
scenario "Processes" do
5.times { create(:legislation_process) }
visit admin_homepage_path
within("#widget_feed_#{processes_feed.id}") do
select "3", from: "widget_feed_limit"
click_button "No"
expect(page).to have_button "Yes"
end
visit root_path
expect(page).to have_content "Open processes"
expect(page).to have_css(".legislation-process", count: 3)
end
scenario "Deactivate proposals" do
Setting["homepage.widgets.feeds.proposals"] = true
create(:proposal)
visit admin_homepage_path
within("#widget_feed_#{proposals_feed.id}") do
click_button "Yes"
expect(page).to have_button "No"
end
visit root_path
expect(page).not_to have_content "Most active proposals"
end
scenario "Deactivate debates" do
Setting["homepage.widgets.feeds.debates"] = true
create(:debate)
visit admin_homepage_path
within("#widget_feed_#{debates_feed.id}") do
click_button "Yes"
expect(page).to have_button "No"
end
visit root_path
expect(page).not_to have_content "Most active debates"
end
scenario "Deactivate processes" do
Setting["homepage.widgets.feeds.processes"] = true
create(:legislation_process)
visit admin_homepage_path
within("#widget_feed_#{processes_feed.id}") do
click_button "Yes"
expect(page).to have_button "No"
end
visit root_path
expect(page).not_to have_content "Open processes"
end
end
scenario "Cards" do
card1 = create(:widget_card, label: "Card1 label",
title: "Card1 text",
description: "Card1 description",
link_text: "Link1 text",
link_url: "consul1.dev")
# TODO: uncomment again after switching to zeitwerk
# card2 = create(:widget_card, label: "Card2 label",
# title: "Card2 text",
# description: "Card2 description",
# link_text: "Link2 text",
# link_url: "consul2.dev")
visit root_path
expect(page).to have_css(".card", count: 1) # TODO: change to `count: 2 after switching to zeitwerk
within("#widget_card_#{card1.id}") do
expect(page).to have_content("CARD1 LABEL")
expect(page).to have_content("CARD1 TEXT")
expect(page).to have_content("Card1 description")
expect(page).to have_content("Link1 text")
expect(page).to have_link(href: "consul1.dev")
expect(page).to have_css("img[alt='#{card1.image.title}']")
end
# TODO: uncomment again after switching to zeitwerk
# within("#widget_card_#{card2.id}") do
# expect(page).to have_content("CARD2 LABEL")
# expect(page).to have_content("CARD2 TEXT")
# expect(page).to have_content("Card2 description")
# expect(page).to have_content("Link2 text")
# expect(page).to have_link(href: "consul2.dev")
# expect(page).to have_css("img[alt='#{card2.image.title}']")
# end
end
scenario "Recomendations" do
create(:proposal, tag_list: "Sport", followers: [user])
create(:proposal, tag_list: "Sport")
visit admin_homepage_path
within("#edit_setting_#{user_recommendations.id}") do
click_button "No"
expect(page).to have_button "Yes"
end
login_as(user)
visit root_path
expect(page).to have_content("Recommendations that may interest you")
end
end