|
| 1 | +import { describe, expect, test } from 'vitest' |
| 2 | +import { octiconAriaLabels } from '../../lib/linting-rules/octicon-aria-labels.js' |
| 3 | + |
| 4 | +describe('octicon-aria-labels', () => { |
| 5 | + const rule = octiconAriaLabels |
| 6 | + |
| 7 | + test('detects octicon without aria-label', () => { |
| 8 | + const errors = [] |
| 9 | + const onError = (errorInfo) => { |
| 10 | + errors.push(errorInfo) |
| 11 | + } |
| 12 | + |
| 13 | + const content = ['This is a test with an octicon:', '{% octicon "alert" %}', 'Some more text.'] |
| 14 | + |
| 15 | + rule.function({ lines: content }, onError) |
| 16 | + |
| 17 | + expect(errors.length).toBe(1) |
| 18 | + expect(errors[0].lineNumber).toBe(2) |
| 19 | + expect(errors[0].detail).toContain('aria-label=alert') |
| 20 | + expect(errors[0].fixInfo.insertText).toContain('aria-label="alert"') |
| 21 | + }) |
| 22 | + |
| 23 | + test('ignores octicons with aria-label', () => { |
| 24 | + const errors = [] |
| 25 | + const onError = (errorInfo) => { |
| 26 | + errors.push(errorInfo) |
| 27 | + } |
| 28 | + |
| 29 | + const content = [ |
| 30 | + 'This is a test with a proper octicon:', |
| 31 | + '{% octicon "alert" aria-label="alert" %}', |
| 32 | + 'Some more text.', |
| 33 | + ] |
| 34 | + |
| 35 | + rule.function({ lines: content }, onError) |
| 36 | + |
| 37 | + expect(errors.length).toBe(0) |
| 38 | + }) |
| 39 | + |
| 40 | + test('detects multiple octicons without aria-label', () => { |
| 41 | + const errors = [] |
| 42 | + const onError = (errorInfo) => { |
| 43 | + errors.push(errorInfo) |
| 44 | + } |
| 45 | + |
| 46 | + const content = [ |
| 47 | + 'This is a test with multiple octicons:', |
| 48 | + '{% octicon "alert" %}', |
| 49 | + 'Some text in between.', |
| 50 | + '{% octicon "check" %}', |
| 51 | + 'More text.', |
| 52 | + ] |
| 53 | + |
| 54 | + rule.function({ lines: content }, onError) |
| 55 | + |
| 56 | + expect(errors.length).toBe(2) |
| 57 | + expect(errors[0].lineNumber).toBe(2) |
| 58 | + expect(errors[0].detail).toContain('aria-label=alert') |
| 59 | + expect(errors[1].lineNumber).toBe(4) |
| 60 | + expect(errors[1].detail).toContain('aria-label=check') |
| 61 | + }) |
| 62 | + |
| 63 | + test('ignores non-octicon liquid tags', () => { |
| 64 | + const errors = [] |
| 65 | + const onError = (errorInfo) => { |
| 66 | + errors.push(errorInfo) |
| 67 | + } |
| 68 | + |
| 69 | + const content = [ |
| 70 | + 'This is a test with non-octicon tags:', |
| 71 | + '{% data foo.bar %}', |
| 72 | + '{% ifversion fpt %}', |
| 73 | + 'Some text.', |
| 74 | + '{% endif %}', |
| 75 | + ] |
| 76 | + |
| 77 | + rule.function({ lines: content }, onError) |
| 78 | + |
| 79 | + expect(errors.length).toBe(0) |
| 80 | + }) |
| 81 | + |
| 82 | + test('suggests correct fix for octicon with other attributes', () => { |
| 83 | + const errors = [] |
| 84 | + const onError = (errorInfo) => { |
| 85 | + errors.push(errorInfo) |
| 86 | + } |
| 87 | + |
| 88 | + const content = [ |
| 89 | + 'This is a test with an octicon with other attributes:', |
| 90 | + '{% octicon "plus" aria-hidden="true" class="foo" %}', |
| 91 | + 'Some more text.', |
| 92 | + ] |
| 93 | + |
| 94 | + rule.function({ lines: content }, onError) |
| 95 | + |
| 96 | + expect(errors.length).toBe(1) |
| 97 | + expect(errors[0].lineNumber).toBe(2) |
| 98 | + expect(errors[0].fixInfo.insertText).toContain('aria-label="plus"') |
| 99 | + expect(errors[0].fixInfo.insertText).toContain('aria-hidden="true"') |
| 100 | + expect(errors[0].fixInfo.insertText).toContain('class="foo"') |
| 101 | + }) |
| 102 | + |
| 103 | + test('handles octicons with unusual spacing', () => { |
| 104 | + const errors = [] |
| 105 | + const onError = (errorInfo) => { |
| 106 | + errors.push(errorInfo) |
| 107 | + } |
| 108 | + |
| 109 | + const content = [ |
| 110 | + 'This is a test with unusual spacing:', |
| 111 | + '{% octicon "x" %}', |
| 112 | + 'Some more text.', |
| 113 | + ] |
| 114 | + |
| 115 | + rule.function({ lines: content }, onError) |
| 116 | + |
| 117 | + expect(errors.length).toBe(1) |
| 118 | + expect(errors[0].lineNumber).toBe(2) |
| 119 | + expect(errors[0].detail).toContain('aria-label=x') |
| 120 | + }) |
| 121 | + |
| 122 | + test('handles octicons split across multiple lines', () => { |
| 123 | + const errors = [] |
| 124 | + const onError = (errorInfo) => { |
| 125 | + errors.push(errorInfo) |
| 126 | + } |
| 127 | + |
| 128 | + const content = [ |
| 129 | + 'This is a test with a multi-line octicon:', |
| 130 | + '{% octicon "chevron-down"', |
| 131 | + ' class="dropdown-menu-icon"', |
| 132 | + '%}', |
| 133 | + 'Some more text.', |
| 134 | + ] |
| 135 | + |
| 136 | + rule.function({ lines: content }, onError) |
| 137 | + |
| 138 | + expect(errors.length).toBe(1) |
| 139 | + expect(errors[0].detail).toContain('aria-label=chevron-down') |
| 140 | + }) |
| 141 | + |
| 142 | + test('falls back to "icon" when octicon name cannot be determined', () => { |
| 143 | + const errors = [] |
| 144 | + const onError = (errorInfo) => { |
| 145 | + errors.push(errorInfo) |
| 146 | + } |
| 147 | + |
| 148 | + const content = [ |
| 149 | + 'This is a test with a malformed octicon:', |
| 150 | + '{% octicon variable %}', |
| 151 | + 'Some more text.', |
| 152 | + ] |
| 153 | + |
| 154 | + rule.function({ lines: content }, onError) |
| 155 | + |
| 156 | + expect(errors.length).toBe(1) |
| 157 | + expect(errors[0].detail).toContain('aria-label=icon') |
| 158 | + expect(errors[0].fixInfo.insertText).toContain('aria-label="icon"') |
| 159 | + }) |
| 160 | +}) |
0 commit comments