|
| 1 | +import expect from 'expect'; |
| 2 | +import { elementType } from 'jsx-ast-utils'; |
| 3 | +import getAccessibleChildText from '../../../src/util/getAccessibleChildText'; |
| 4 | +import JSXAttributeMock from '../../../__mocks__/JSXAttributeMock'; |
| 5 | +import JSXElementMock from '../../../__mocks__/JSXElementMock'; |
| 6 | + |
| 7 | +describe('getAccessibleChildText', () => { |
| 8 | + it('returns the aria-label when present', () => { |
| 9 | + expect(getAccessibleChildText(JSXElementMock( |
| 10 | + 'a', |
| 11 | + [JSXAttributeMock('aria-label', 'foo')], |
| 12 | + ), elementType)).toBe('foo'); |
| 13 | + }); |
| 14 | + |
| 15 | + it('returns the aria-label instead of children', () => { |
| 16 | + expect(getAccessibleChildText(JSXElementMock( |
| 17 | + 'a', |
| 18 | + [JSXAttributeMock('aria-label', 'foo')], |
| 19 | + [{ type: 'JSXText', value: 'bar' }], |
| 20 | + ), elementType)).toBe('foo'); |
| 21 | + }); |
| 22 | + |
| 23 | + it('skips elements with aria-hidden=true', () => { |
| 24 | + expect(getAccessibleChildText(JSXElementMock( |
| 25 | + 'a', |
| 26 | + [JSXAttributeMock('aria-hidden', 'true')], |
| 27 | + ), elementType)).toBe(''); |
| 28 | + }); |
| 29 | + |
| 30 | + it('returns literal value for JSXText child', () => { |
| 31 | + expect(getAccessibleChildText(JSXElementMock( |
| 32 | + 'a', |
| 33 | + [], |
| 34 | + [{ type: 'JSXText', value: 'bar' }], |
| 35 | + ), elementType)).toBe('bar'); |
| 36 | + }); |
| 37 | + |
| 38 | + it('returns literal value for JSXText child', () => { |
| 39 | + expect(getAccessibleChildText(JSXElementMock( |
| 40 | + 'a', |
| 41 | + [], |
| 42 | + [{ type: 'Literal', value: 'bar' }], |
| 43 | + ), elementType)).toBe('bar'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('returns recursive value for JSXElement child', () => { |
| 47 | + expect(getAccessibleChildText(JSXElementMock( |
| 48 | + 'a', |
| 49 | + [], |
| 50 | + [JSXElementMock( |
| 51 | + 'span', |
| 52 | + [], |
| 53 | + [{ type: 'Literal', value: 'bar' }], |
| 54 | + )], |
| 55 | + ), elementType)).toBe('bar'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('skips children with aria-hidden-true', () => { |
| 59 | + expect(getAccessibleChildText(JSXElementMock( |
| 60 | + 'a', |
| 61 | + [], |
| 62 | + [JSXElementMock( |
| 63 | + 'span', |
| 64 | + [], |
| 65 | + [JSXElementMock( |
| 66 | + 'span', |
| 67 | + [JSXAttributeMock('aria-hidden', 'true')], |
| 68 | + )], |
| 69 | + )], |
| 70 | + ), elementType)).toBe(''); |
| 71 | + }); |
| 72 | + |
| 73 | + it('joins multiple children properly - no spacing', () => { |
| 74 | + expect(getAccessibleChildText(JSXElementMock( |
| 75 | + 'a', |
| 76 | + [], |
| 77 | + [{ type: 'Literal', value: 'foo' }, { type: 'Literal', value: 'bar' }], |
| 78 | + ), elementType)).toBe('foo bar'); |
| 79 | + }); |
| 80 | + |
| 81 | + it('joins multiple children properly - with spacing', () => { |
| 82 | + expect(getAccessibleChildText(JSXElementMock( |
| 83 | + 'a', |
| 84 | + [], |
| 85 | + [{ type: 'Literal', value: ' foo ' }, { type: 'Literal', value: ' bar ' }], |
| 86 | + ), elementType)).toBe('foo bar'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('skips unknown elements', () => { |
| 90 | + expect(getAccessibleChildText(JSXElementMock( |
| 91 | + 'a', |
| 92 | + [], |
| 93 | + [{ type: 'Literal', value: 'foo' }, { type: 'Unknown' }, { type: 'Literal', value: 'bar' }], |
| 94 | + ), elementType)).toBe('foo bar'); |
| 95 | + }); |
| 96 | +}); |
0 commit comments