|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "test_helper" |
| 4 | + |
| 5 | +class SvgHasAccessibleTextCounter < LinterTestCase |
| 6 | + def linter_class |
| 7 | + ERBLint::Linters::GitHub::Accessibility::SvgHasAccessibleTextCounter |
| 8 | + end |
| 9 | + |
| 10 | + def test_warns_if_svg_does_not_have_accesible_text |
| 11 | + @file = "<svg height='100' width='100'><circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red'/></svg>" |
| 12 | + @linter.run(processed_source) |
| 13 | + assert_equal(2, @linter.offenses.count) |
| 14 | + error_messages = @linter.offenses.map(&:message).sort |
| 15 | + assert_match(/If you must, add <%# erblint:counter GitHub::Accessibility::SvgHasAccessibleTextCounter 1 %> to bypass this check./, error_messages.first) |
| 16 | + assert_match(/`<svg>` must have accessible text. Set `aria-label`, or `aria-labelledby`, or nest a `<title>` element. However, if the `<svg>` is purely decorative, hide it with `aria-hidden='true'./, error_messages.last) |
| 17 | + end |
| 18 | + |
| 19 | + def test_does_not_warn_if_svg_has_accesible_text |
| 20 | + @file = "<svg aria-label='A circle' height='100' width='100'><circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red'/></svg>" |
| 21 | + @linter.run(processed_source) |
| 22 | + |
| 23 | + assert_empty @linter.offenses |
| 24 | + |
| 25 | + @file = "<svg aria-labelledby='test_id' height='100' width='100'><circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red'/></svg>" |
| 26 | + @linter.run(processed_source) |
| 27 | + |
| 28 | + assert_empty @linter.offenses |
| 29 | + |
| 30 | + @file = "<svg height='100' width='100'><title>A circle</title><circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red'/></svg>" |
| 31 | + @linter.run(processed_source) |
| 32 | + |
| 33 | + assert_empty @linter.offenses |
| 34 | + end |
| 35 | + |
| 36 | + def test_does_not_warn_if_svg_has_accesible_text_and_has_correct_counter_comment |
| 37 | + @file = <<~ERB |
| 38 | + <%# erblint:counter GitHub::Accessibility::SvgHasAccessibleTextCounter 1 %> |
| 39 | + <svg height='100' width='100'><circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red'/></svg> |
| 40 | + ERB |
| 41 | + @linter.run(processed_source) |
| 42 | + |
| 43 | + assert_equal 0, @linter.offenses.count |
| 44 | + end |
| 45 | + |
| 46 | + def test_does_not_autocorrect_when_ignores_are_correct |
| 47 | + @file = <<~ERB |
| 48 | + <%# erblint:counter GitHub::Accessibility::SvgHasAccessibleTextCounter 1 %> |
| 49 | + <svg height='100' width='100'><circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red'/></svg> |
| 50 | + ERB |
| 51 | + |
| 52 | + assert_equal @file, corrected_content |
| 53 | + end |
| 54 | + |
| 55 | + def test_does_autocorrect_when_ignores_are_not_correct |
| 56 | + @file = <<~ERB |
| 57 | + <%# erblint:counter GitHub::Accessibility::SvgHasAccessibleTextCounter 3 %> |
| 58 | + <svg height='100' width='100'><circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red'/></svg> |
| 59 | + ERB |
| 60 | + refute_equal @file, corrected_content |
| 61 | + |
| 62 | + expected_content = <<~ERB |
| 63 | + <%# erblint:counter GitHub::Accessibility::SvgHasAccessibleTextCounter 1 %> |
| 64 | + <svg height='100' width='100'><circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red'/></svg> |
| 65 | + ERB |
| 66 | + assert_equal expected_content, corrected_content |
| 67 | + end |
| 68 | +end |
0 commit comments