From 75ef3e0a511110338a230cd1fde6bf26402621fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 3 Apr 2024 23:32:16 +0200 Subject: [PATCH] Avoid empty links in banners without content This happened when previewing banners in the "new banner form", which might cause accessibility issues when people access the list of links on the page. We were getting the following accessibility error: ``` link-name: Links must have discernible text (serious) https://dequeuniversity.com/rules/axe/4.9/link-name?application=axeAPI The following node violate this rule: Selector: a[href$="new"] HTML:

Fix all of the following: - Element is in tab order and does not have accessible text Fix any of the following: - Element does not have text that is visible to screen readers - aria-label attribute does not exist or is empty - aria-labelledby attribute does not exist, references elements that do not exist or references elements that - Element has no title attribute ``` --- app/components/shared/banner_component.html.erb | 8 +++----- app/components/shared/banner_component.rb | 4 ++++ spec/components/shared/banner_component_spec.rb | 8 ++++++++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/app/components/shared/banner_component.html.erb b/app/components/shared/banner_component.html.erb index fe020302a..cbea18d0e 100644 --- a/app/components/shared/banner_component.html.erb +++ b/app/components/shared/banner_component.html.erb @@ -1,5 +1,3 @@ -<% if banner %> - -<% end %> + diff --git a/app/components/shared/banner_component.rb b/app/components/shared/banner_component.rb index de61e1602..21aba7c65 100644 --- a/app/components/shared/banner_component.rb +++ b/app/components/shared/banner_component.rb @@ -13,6 +13,10 @@ class Shared::BannerComponent < ApplicationComponent end end + def render? + banner && (banner.title.present? || banner.description.present?) + end + private def link diff --git a/spec/components/shared/banner_component_spec.rb b/spec/components/shared/banner_component_spec.rb index 6d2512373..403a45e9d 100644 --- a/spec/components/shared/banner_component_spec.rb +++ b/spec/components/shared/banner_component_spec.rb @@ -31,6 +31,14 @@ describe Shared::BannerComponent do expect(page).not_to have_content "Proposal banner" end + it "does not render an empty banner" do + banner = build(:banner, title: "", description: "") + + render_inline Shared::BannerComponent.new(banner) + + expect(page).not_to be_rendered + end + context "several banners available in the same section" do before do create(:banner,