Extract banner test to a component

Checking the `style` attribute fails in JavaScript tests because the
browser converts colors to the `rgb()` format.

So we're testing the generated HTML in a component test while
simplifying the system test.
This commit is contained in:
Javi Martín
2021-03-23 20:14:26 +01:00
parent ba432c9f7f
commit b93f38ec11
2 changed files with 23 additions and 17 deletions

View File

@@ -2,9 +2,24 @@ require "rails_helper"
describe Shared::BannerComponent, type: :component do describe Shared::BannerComponent, type: :component do
it "renders given a banner" do it "renders given a banner" do
render_inline Shared::BannerComponent.new(create(:banner, title: "Vote now!")) banner = create(:banner,
title: "Vote now!",
description: "Banner description",
target_url: "http://www.url.com",
post_started_at: (Time.current - 4.days),
post_ended_at: (Time.current + 10.days),
background_color: "#FF0000",
font_color: "#FFFFFF"
)
render_inline Shared::BannerComponent.new(banner)
expect(page.find(".banner")).to have_content "Vote now!" expect(page.find(".banner")).to have_content "Vote now!"
expect(page.find(".banner")).to have_content "Banner description"
expect(page.find(".banner")[:style]).to eq("background-color:#FF0000;")
expect(page.find("h2")[:style]).to eq("color:#FFFFFF;")
expect(page.find("h3")[:style]).to eq("color:#FFFFFF;")
expect(page).to have_link href: "http://www.url.com"
end end
it "renders given a section" do it "renders given a section" do

View File

@@ -1,25 +1,16 @@
require "rails_helper" require "rails_helper"
describe "Banner" do describe "Banner" do
scenario "The banner is shown correctly" do scenario "Only renders banners in the right section" do
create(:web_section, name: "homepage") create(:banner,
banner = create(:banner, title: "Hello", web_sections: [WebSection.find_by!(name: "homepage")],
description: "Banner description", description: "Banner description",
target_url: "http://www.url.com", post_started_at: (Time.current - 4.days),
post_started_at: (Time.current - 4.days), post_ended_at: (Time.current + 10.days))
post_ended_at: (Time.current + 10.days),
background_color: "#FF0000",
font_color: "#FFFFFF")
section = WebSection.where(name: "homepage").last
create(:banner_section, web_section: section, banner_id: banner.id)
visit root_path visit root_path
within(".banner") do within(".banner") { expect(page).to have_content("Banner description") }
expect(page).to have_content("Banner description")
expect(find("h2")[:style]).to eq("color:#{banner.font_color};")
expect(find("h3")[:style]).to eq("color:#{banner.font_color};")
end
visit debates_path visit debates_path