|
| 1 | +import {_computeAriaAccessibleName} from './aria-accessible-name'; |
| 2 | + |
| 3 | +describe('_computeAriaAccessibleName', () => { |
| 4 | + let rootElement: HTMLSpanElement; |
| 5 | + |
| 6 | + beforeEach(() => { |
| 7 | + rootElement = document.createElement('span'); |
| 8 | + document.body.appendChild(rootElement); |
| 9 | + }); |
| 10 | + |
| 11 | + afterEach(() => { |
| 12 | + document.body.removeChild(rootElement); |
| 13 | + }); |
| 14 | + |
| 15 | + it('uses aria-labelledby over aria-label', () => { |
| 16 | + rootElement.innerHTML = ` |
| 17 | + <label id='test-label'>Aria Labelledby</label> |
| 18 | + <input id='test-el' aria-labelledby='test-label' aria-label='Aria Label'/> |
| 19 | + `; |
| 20 | + |
| 21 | + const input = rootElement.querySelector('#test-el')!; |
| 22 | + expect(_computeAriaAccessibleName(input as HTMLInputElement)).toBe('Aria Labelledby'); |
| 23 | + }); |
| 24 | + |
| 25 | + it('uses aria-label over for/id', () => { |
| 26 | + rootElement.innerHTML = ` |
| 27 | + <label for='test-el'>For</label> |
| 28 | + <input id='test-el' aria-label='Aria Label'/> |
| 29 | + `; |
| 30 | + |
| 31 | + const input = rootElement.querySelector('#test-el')!; |
| 32 | + expect(_computeAriaAccessibleName(input as HTMLInputElement)).toBe('Aria Label'); |
| 33 | + }); |
| 34 | + |
| 35 | + it('uses a label with for/id over a title attribute', () => { |
| 36 | + rootElement.innerHTML = ` |
| 37 | + <label for='test-el'>For</label> |
| 38 | + <input id='test-el' title='Title'/> |
| 39 | + `; |
| 40 | + |
| 41 | + const input = rootElement.querySelector('#test-el')!; |
| 42 | + expect(_computeAriaAccessibleName(input as HTMLInputElement)).toBe('For'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('returns title when argument has a specifieid title', () => { |
| 46 | + rootElement.innerHTML = `<input id="test-el" title='Title'/>`; |
| 47 | + |
| 48 | + const input = rootElement.querySelector('#test-el')!; |
| 49 | + expect(_computeAriaAccessibleName(input as HTMLInputElement)).toBe('Title'); |
| 50 | + }); |
| 51 | + |
| 52 | + // match browser behavior of giving placeholder attribute preference over title attribute |
| 53 | + it('uses placeholder over title', () => { |
| 54 | + rootElement.innerHTML = `<input id="test-el" title='Title' placeholder='Placeholder'/>`; |
| 55 | + |
| 56 | + const input = rootElement.querySelector('#test-el')!; |
| 57 | + expect(_computeAriaAccessibleName(input as HTMLInputElement)).toBe('Placeholder'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('uses aria-label over title and placeholder', () => { |
| 61 | + rootElement.innerHTML = `<input id="test-el" title='Title' placeholder='Placeholder' |
| 62 | + aria-label="Aria Label"/>`; |
| 63 | + |
| 64 | + const input = rootElement.querySelector('#test-el')!; |
| 65 | + expect(_computeAriaAccessibleName(input as HTMLInputElement)).toBe('Aria Label'); |
| 66 | + }); |
| 67 | + |
| 68 | + it('includes both textnode and element children of label with for/id', () => { |
| 69 | + rootElement.innerHTML = ` |
| 70 | + <label for="test-el"> |
| 71 | + Hello |
| 72 | + <span> |
| 73 | + Wo |
| 74 | + <span><span>r</span></span> |
| 75 | + <span> ld </span> |
| 76 | + </span> |
| 77 | + ! |
| 78 | + </label> |
| 79 | + <input id='test-el'/> |
| 80 | + `; |
| 81 | + |
| 82 | + const input = rootElement.querySelector('#test-el')!; |
| 83 | + expect(_computeAriaAccessibleName(input as HTMLInputElement)).toBe('Hello Wo r ld !'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('return computed name of hidden label which has for/id', () => { |
| 87 | + rootElement.innerHTML = ` |
| 88 | + <label for="test-el" aria-hidden="true" style="display: none;">For</label> |
| 89 | + <input id='test-el'/> |
| 90 | + `; |
| 91 | + |
| 92 | + const input = rootElement.querySelector('#test-el')!; |
| 93 | + expect(_computeAriaAccessibleName(input as HTMLInputElement)).toBe('For'); |
| 94 | + }); |
| 95 | + |
| 96 | + it('returns computed names of existing elements when 2 of 3 targets of aria-labelledby exist', () => { |
| 97 | + rootElement.innerHTML = ` |
| 98 | + <label id="label-1-of-2" aria-hidden="true" style="display: none;">Label1</label> |
| 99 | + <label id="label-2-of-2" aria-hidden="true" style="display: none;">Label2</label> |
| 100 | + <input id="test-el" aria-labelledby="label-1-of-2 label-2-of-2 non-existant-label"/> |
| 101 | + `; |
| 102 | + |
| 103 | + const input = rootElement.querySelector('#test-el')!; |
| 104 | + expect(_computeAriaAccessibleName(input as HTMLInputElement)).toBe('Label1 Label2'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('returns repeated label when there are duplicate ids in aria-labelledby', () => { |
| 108 | + rootElement.innerHTML = ` |
| 109 | + <label id="label-1-of-1" aria-hidden="true" style="display: none;">Label1</label> |
| 110 | + <input id="test-el" aria-labelledby="label-1-of-1 label-1-of-1"/> |
| 111 | + `; |
| 112 | + |
| 113 | + const input = rootElement.querySelector('#test-el')!; |
| 114 | + expect(_computeAriaAccessibleName(input as HTMLInputElement)).toBe('Label1 Label1'); |
| 115 | + }); |
| 116 | + |
| 117 | + it('returns empty string when passed `<input id="test-el"/>`', () => { |
| 118 | + rootElement.innerHTML = `<input id="test-el"/>`; |
| 119 | + |
| 120 | + const input = rootElement.querySelector('#test-el')!; |
| 121 | + expect(_computeAriaAccessibleName(input as HTMLInputElement)).toBe(''); |
| 122 | + }); |
| 123 | + |
| 124 | + it('ignores the aria-labelledby of an aria-labelledby', () => { |
| 125 | + rootElement.innerHTML = ` |
| 126 | + <label id="label" aria-labelledby="transitive-label">Label</label> |
| 127 | + <label id="transitive-label" aria-labelled-by="transitive-label">Transitive Label</div> |
| 128 | + <input id="test-el" aria-labelledby="label"/> |
| 129 | + `; |
| 130 | + |
| 131 | + const input = rootElement.querySelector('#test-el')!; |
| 132 | + const label = rootElement.querySelector('#label')!; |
| 133 | + expect(_computeAriaAccessibleName(label as any)).toBe('Transitive Label'); |
| 134 | + expect(_computeAriaAccessibleName(input as HTMLInputElement)).toBe('Label'); |
| 135 | + }); |
| 136 | + |
| 137 | + it('ignores the aria-labelledby on a label with for/id', () => { |
| 138 | + rootElement.innerHTML = ` |
| 139 | + <label for="transitive2-label" aria-labelledby="transitive2-div"></label> |
| 140 | + <div id="transitive2-div">Div</div> |
| 141 | + <input id="test-el" aria-labelled-by="transitive2-label"/> |
| 142 | + `; |
| 143 | + |
| 144 | + const input = rootElement.querySelector('#test-el')!; |
| 145 | + expect(_computeAriaAccessibleName(input as HTMLInputElement)).toBe(''); |
| 146 | + }); |
| 147 | + |
| 148 | + it('returns empty string when argument input is aria-labelledby itself', () => { |
| 149 | + rootElement.innerHTML = ` |
| 150 | + <input id="test-el" aria-labelled-by="test-el"/> |
| 151 | + `; |
| 152 | + |
| 153 | + const input = rootElement.querySelector('#test-el')!; |
| 154 | + const computedName = _computeAriaAccessibleName(input as HTMLInputElement); |
| 155 | + expect(typeof computedName) |
| 156 | + .withContext('should return value of type string') |
| 157 | + .toBe('string'); |
| 158 | + }); |
| 159 | +}); |
0 commit comments