@@ -49,20 +49,90 @@ def test_does_not_warn_when_banned_text_is_part_of_more_text
4949 assert_empty @linter . offenses
5050 end
5151
52+ def test_ignores_when_aria_label_with_variable_is_set_on_link_tag
53+ @file = <<~ERB
54+ < a aria-label ="<%= tooltip_text %> "> Learn more</ a >
55+ ERB
56+ @linter . run ( processed_source )
57+
58+ assert_empty @linter . offenses
59+ end
60+
61+ def test_flags_when_aria_label_does_not_include_visible_link_text
62+ @file = <<~ERB
63+ < a aria-label ="GitHub Sponsors "> Learn more</ a >
64+ ERB
65+ @linter . run ( processed_source )
66+
67+ refute_empty @linter . offenses
68+ end
69+
70+ def test_does_not_flag_when_aria_label_includes_visible_link_text
71+ @file = <<~ERB
72+ < a aria-label ="Learn more about GitHub Sponsors "> Learn more</ a >
73+ ERB
74+ @linter . run ( processed_source )
75+
76+ assert_empty @linter . offenses
77+ end
78+
79+ def test_ignores_when_aria_labelledby_is_set_on_link_tag_since_cannot_be_evaluated_accurately_by_erb_linter
80+ @file = "<a aria-labelledby='someElement'>Click here</a>"
81+ @linter . run ( processed_source )
82+
83+ assert_empty @linter . offenses
84+ end
85+
5286 def test_warns_when_link_rails_helper_text_is_banned_text
53- @file = "<%= link_to('click here', redirect_url, id: 'redirect' ) %>"
87+ @file = "<%= link_to('click here', redirect_url) %>"
5488 @linter . run ( processed_source )
5589
5690 refute_empty @linter . offenses
5791 end
5892
59- def test_does_not_warn_when_generic_text_is_link_rails_helper_sub_text
60- @file = "<%= link_to('click here to learn about github', redirect_url, id: 'redirect') %>"
93+ def test_warns_when_link_rails_helper_text_is_banned_text_with_aria_description
94+ @file = <<~ERB
95+ <%= link_to ( 'click here' , redirect_url , 'aria-describedby' : 'element123' , id : 'redirect' ) %>
96+ ERB
97+ @linter . run ( processed_source )
98+
99+ refute_empty @linter . offenses
100+ end
101+
102+ def test_ignores_when_link_rails_helper_text_is_banned_text_with_any_aria_label_attributes_since_cannot_be_evaluated_accurately_by_erb_linter
103+ @file = <<~ERB
104+ <%= link_to ( 'learn more' , redirect_url , 'aria-labelledby' : 'element1234' , id : 'redirect' ) %>
105+ <%= link_to ( 'learn more' , redirect_url , 'aria-label' : some_variable , id : 'redirect' ) %>
106+ <%= link_to ( 'learn more' , redirect_url , 'aria-label' : "Learn #{ @variable } ", id: 'redirect') %>
107+ <%= link_to ( 'learn more' , redirect_url , 'aria-label' : 'learn more about GitHub' , id : 'redirect' ) %>
108+ <%= link_to ( 'learn more' , redirect_url , aria : { label : 'learn more about GitHub' } , id : 'redirect' ) %>
109+ ERB
61110 @linter . run ( processed_source )
62111
63112 assert_empty @linter . offenses
64113 end
65114
115+ def test_handles_files_with_various_links
116+ @file = <<~ERB
117+ < p >
118+ < a href ="github.com " aria-label ='Click here to learn more '> Click here</ a >
119+ <%= link_to "learn more" , billing_path , "aria-label" : "something" %>
120+ < a href ="github.com " aria-label ='Some totally different text '> Click here</ a >
121+ < a href ="github.com " aria-labelledby ='someElement '> Click here</ a >
122+ </ p >
123+ < p >
124+ <%= link_to "learn more" , billing_path , aria : { label : "something" } %>
125+ <%= link_to "learn more" , billing_path , aria : { describedby : "element123" } %>
126+ <%= link_to "learn more" , billing_path , "aria-describedby" : "element123" %>
127+ </ p >
128+ ERB
129+ @linter . run ( processed_source )
130+
131+ refute_empty @linter . offenses
132+ # 3 offenses, 1 related to matching counter comment not present despite violations
133+ assert_equal 4 , @linter . offenses . count
134+ end
135+
66136 def test_does_not_warns_if_element_has_correct_counter_comment
67137 @file = <<~ERB
68138 <%# erblint:counter GitHub::Accessibility::AvoidGenericLinkTextCounter 1 %>
@@ -75,14 +145,33 @@ def test_does_not_warns_if_element_has_correct_counter_comment
75145
76146 def test_autocorrects_when_ignores_are_not_correct
77147 @file = <<~ERB
78- <%# erblint:counter GitHub::Accessibility::AvoidGenericLinkTextCounter 2 %>
79- < a > Link</ a >
148+ < p >
149+ < a href ="github.com " aria-label ='Click here to learn more '> Click here</ a >
150+ < a href ="github.com " aria-label ='Some totally different text '> Click here</ a >
151+ < a href ="github.com " aria-labelledby ='someElement '> Click here</ a >
152+ </ p >
153+ < p >
154+ <%= link_to "learn more" , billing_path , "aria-label" : "something" %>
155+ <%= link_to "learn more" , billing_path , aria : { label : "something" } %>
156+ <%= link_to "learn more" , billing_path , aria : { describedby : "element123" } %>
157+ <%= link_to "learn more" , billing_path , "aria-describedby" : "element123" %>
158+ </ p >
80159 ERB
81160 refute_equal @file , corrected_content
82161
83162 expected_content = <<~ERB
84- <%# erblint:counter GitHub::Accessibility::AvoidGenericLinkTextCounter 1 %>
85- < a > Link</ a >
163+ <%# erblint:counter GitHub::Accessibility::AvoidGenericLinkTextCounter 3 %>
164+ < p >
165+ < a href ="github.com " aria-label ='Click here to learn more '> Click here</ a >
166+ < a href ="github.com " aria-label ='Some totally different text '> Click here</ a >
167+ < a href ="github.com " aria-labelledby ='someElement '> Click here</ a >
168+ </ p >
169+ < p >
170+ <%= link_to "learn more" , billing_path , "aria-label" : "something" %>
171+ <%= link_to "learn more" , billing_path , aria : { label : "something" } %>
172+ <%= link_to "learn more" , billing_path , aria : { describedby : "element123" } %>
173+ <%= link_to "learn more" , billing_path , "aria-describedby" : "element123" %>
174+ </ p >
86175 ERB
87176 assert_equal expected_content , corrected_content
88177 end
0 commit comments