|
| 1 | +import { getTextValue } from './get-text-value' |
| 2 | + |
| 3 | +let html = String.raw |
| 4 | + |
| 5 | +it('should be possible to get the text value from an element', () => { |
| 6 | + let element = document.createElement('div') |
| 7 | + element.innerText = 'Hello World' |
| 8 | + expect(getTextValue(element)).toEqual('Hello World') |
| 9 | +}) |
| 10 | + |
| 11 | +it('should strip out emojis when receiving the text from the element', () => { |
| 12 | + let element = document.createElement('div') |
| 13 | + element.innerText = '🇨🇦 Canada' |
| 14 | + expect(getTextValue(element)).toEqual('Canada') |
| 15 | +}) |
| 16 | + |
| 17 | +it('should strip out hidden elements', () => { |
| 18 | + let element = document.createElement('div') |
| 19 | + element.innerHTML = html`<div><span hidden>Hello</span> world</div>` |
| 20 | + expect(getTextValue(element)).toEqual('world') |
| 21 | +}) |
| 22 | + |
| 23 | +it('should strip out aria-hidden elements', () => { |
| 24 | + let element = document.createElement('div') |
| 25 | + element.innerHTML = html`<div><span aria-hidden>Hello</span> world</div>` |
| 26 | + expect(getTextValue(element)).toEqual('world') |
| 27 | +}) |
| 28 | + |
| 29 | +it('should strip out role="img" elements', () => { |
| 30 | + let element = document.createElement('div') |
| 31 | + element.innerHTML = html`<div><span role="img">°</span> world</div>` |
| 32 | + expect(getTextValue(element)).toEqual('world') |
| 33 | +}) |
| 34 | + |
| 35 | +it('should be possible to get the text value from the aria-label', () => { |
| 36 | + let element = document.createElement('div') |
| 37 | + element.setAttribute('aria-label', 'Hello World') |
| 38 | + expect(getTextValue(element)).toEqual('Hello World') |
| 39 | +}) |
| 40 | + |
| 41 | +it('should be possible to get the text value from the aria-label (even if there is content)', () => { |
| 42 | + let element = document.createElement('div') |
| 43 | + element.setAttribute('aria-label', 'Hello World') |
| 44 | + element.innerHTML = 'Hello Universe' |
| 45 | + element.innerText = 'Hello Universe' |
| 46 | + expect(getTextValue(element)).toEqual('Hello World') |
| 47 | +}) |
| 48 | + |
| 49 | +it('should be possible to get the text value from the element referenced by aria-labelledby (using `aria-label`)', () => { |
| 50 | + document.body.innerHTML = html` |
| 51 | + <div> |
| 52 | + <div id="foo" aria-labelledby="bar">Contents of foo</div> |
| 53 | + <div id="bar" aria-label="Actual value of bar">Contents of bar</div> |
| 54 | + </div> |
| 55 | + ` |
| 56 | + |
| 57 | + expect(getTextValue(document.querySelector('#foo')!)).toEqual('Actual value of bar') |
| 58 | +}) |
| 59 | + |
| 60 | +it('should be possible to get the text value from the element referenced by aria-labelledby (using its contents)', () => { |
| 61 | + document.body.innerHTML = html` |
| 62 | + <div> |
| 63 | + <div id="foo" aria-labelledby="bar">Contents of foo</div> |
| 64 | + <div id="bar">Contents of bar</div> |
| 65 | + </div> |
| 66 | + ` |
| 67 | + |
| 68 | + expect(getTextValue(document.querySelector('#foo')!)).toEqual('Contents of bar') |
| 69 | +}) |
| 70 | + |
| 71 | +it('should be possible to get the text value from the element referenced by aria-labelledby (using `aria-label`, multiple)', () => { |
| 72 | + document.body.innerHTML = html` |
| 73 | + <div> |
| 74 | + <div id="foo" aria-labelledby="bar baz">Contents of foo</div> |
| 75 | + <div id="bar" aria-label="Actual value of bar">Contents of bar</div> |
| 76 | + <div id="baz" aria-label="Actual value of baz">Contents of baz</div> |
| 77 | + </div> |
| 78 | + ` |
| 79 | + |
| 80 | + expect(getTextValue(document.querySelector('#foo')!)).toEqual( |
| 81 | + 'Actual value of bar, Actual value of baz' |
| 82 | + ) |
| 83 | +}) |
| 84 | + |
| 85 | +it('should be possible to get the text value from the element referenced by aria-labelledby (using its contents, multiple)', () => { |
| 86 | + document.body.innerHTML = html` |
| 87 | + <div> |
| 88 | + <div id="foo" aria-labelledby="bar baz">Contents of foo</div> |
| 89 | + <div id="bar">Contents of bar</div> |
| 90 | + <div id="baz">Contents of baz</div> |
| 91 | + </div> |
| 92 | + ` |
| 93 | + |
| 94 | + expect(getTextValue(document.querySelector('#foo')!)).toEqual('Contents of bar, Contents of baz') |
| 95 | +}) |
0 commit comments