Simplify rendering a banner

Now the banner component accepts either a banner or a section and loads
the banner if it's a section, so we don't have to add the `@banners`
variable in several controllers.
This commit is contained in:
Javi Martín
2021-01-14 16:06:34 +01:00
parent c3147a1bb6
commit a7bbdb1bd0
14 changed files with 82 additions and 26 deletions

View File

@@ -7,6 +7,70 @@ describe Shared::BannerComponent, type: :component do
expect(page.find(".banner")).to have_content "Vote now!"
end
it "renders given a section" do
create(:banner, web_sections: [WebSection.find_by!(name: "debates")], title: "Debate banner")
create(:banner, web_sections: [WebSection.find_by!(name: "proposals")], title: "Proposal banner")
render_inline Shared::BannerComponent.new("debates")
expect(page.find(".banner")).to have_content "Debate banner"
expect(page).not_to have_content "Proposal banner"
end
context "several banners available in the same section" do
before do
create(:banner,
web_sections: [WebSection.find_by!(name: "debates")],
title: "First banner",
description: "First description",
target_url: "/first_target"
)
create(:banner,
web_sections: [WebSection.find_by!(name: "debates")],
title: "Second banner",
description: "Second description",
target_url: "/second_target"
)
end
it "only renders one banner" do
render_inline Shared::BannerComponent.new("debates")
expect(page).to have_css ".banner", count: 1
end
it "consistently renders one banner" do
render_inline Shared::BannerComponent.new("debates")
if page.has_content?("First banner")
expect(page).to have_content "First description"
expect(page).to have_css "[href='/first_target']"
expect(page).not_to have_content "Second banner"
else
expect(page).to have_content "Second description"
expect(page).to have_css "[href='/second_target']"
end
end
it "only renders active banners" do
Banner.first.update!(post_ended_at: 1.day.ago)
render_inline Shared::BannerComponent.new("debates")
expect(page).to have_content "Second banner"
expect(page).not_to have_content "First banner"
end
it "does not render anything with no active banners" do
Banner.all.each { |banner| banner.update!(post_ended_at: 1.day.ago) }
render_inline Shared::BannerComponent.new("debates")
expect(page).not_to have_css ".banner"
end
end
it "does not render anything given nil" do
render_inline Shared::BannerComponent.new(nil)