|
| 1 | +import expect from 'expect'; |
| 2 | +import { elementType } from 'jsx-ast-utils'; |
| 3 | +import isFocusable from '../../../src/util/isFocusable'; |
| 4 | +import { |
| 5 | + genElementSymbol, |
| 6 | + genInteractiveElements, |
| 7 | + genNonInteractiveElements, |
| 8 | +} from '../../../__mocks__/genInteractives'; |
| 9 | +import JSXAttributeMock from '../../../__mocks__/JSXAttributeMock'; |
| 10 | + |
| 11 | +function mergeTabIndex(index, attributes) { |
| 12 | + return [...attributes, JSXAttributeMock('tabIndex', index)]; |
| 13 | +} |
| 14 | + |
| 15 | +describe('isFocusable', () => { |
| 16 | + describe('interactive elements', () => { |
| 17 | + genInteractiveElements().forEach(({ openingElement }) => { |
| 18 | + it(`should identify \`${genElementSymbol(openingElement)}\` as a focusable element`, () => { |
| 19 | + expect(isFocusable( |
| 20 | + elementType(openingElement), |
| 21 | + openingElement.attributes, |
| 22 | + )).toBe(true); |
| 23 | + }); |
| 24 | + |
| 25 | + it(`should not identify \`${genElementSymbol(openingElement)}\` with tabIndex of -1 as a focusable element`, () => { |
| 26 | + expect(isFocusable( |
| 27 | + elementType(openingElement), |
| 28 | + mergeTabIndex(-1, openingElement.attributes), |
| 29 | + )).toBe(false); |
| 30 | + }); |
| 31 | + |
| 32 | + it(`should identify \`${genElementSymbol(openingElement)}\` with tabIndex of 0 as a focusable element`, () => { |
| 33 | + expect(isFocusable( |
| 34 | + elementType(openingElement), |
| 35 | + mergeTabIndex(0, openingElement.attributes), |
| 36 | + )).toBe(true); |
| 37 | + }); |
| 38 | + |
| 39 | + it(`should identify \`${genElementSymbol(openingElement)}\` with tabIndex of 1 as a focusable element`, () => { |
| 40 | + expect(isFocusable( |
| 41 | + elementType(openingElement), |
| 42 | + mergeTabIndex(1, openingElement.attributes), |
| 43 | + )).toBe(true); |
| 44 | + }); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('non-interactive elements', () => { |
| 49 | + genNonInteractiveElements().forEach(({ openingElement }) => { |
| 50 | + it(`should not identify \`${genElementSymbol(openingElement)}\` as a focusable element`, () => { |
| 51 | + expect(isFocusable( |
| 52 | + elementType(openingElement), |
| 53 | + openingElement.attributes, |
| 54 | + )).toBe(false); |
| 55 | + }); |
| 56 | + |
| 57 | + it(`should not identify \`${genElementSymbol(openingElement)}\` with tabIndex of -1 as a focusable element`, () => { |
| 58 | + expect(isFocusable( |
| 59 | + elementType(openingElement), |
| 60 | + mergeTabIndex(-1, openingElement.attributes), |
| 61 | + )).toBe(false); |
| 62 | + }); |
| 63 | + |
| 64 | + it(`should identify \`${genElementSymbol(openingElement)}\` with tabIndex of 0 as a focusable element`, () => { |
| 65 | + expect(isFocusable( |
| 66 | + elementType(openingElement), |
| 67 | + mergeTabIndex(0, openingElement.attributes), |
| 68 | + )).toBe(true); |
| 69 | + }); |
| 70 | + |
| 71 | + it(`should identify \`${genElementSymbol(openingElement)}\` with tabIndex of 1 as a focusable element`, () => { |
| 72 | + expect(isFocusable( |
| 73 | + elementType(openingElement), |
| 74 | + mergeTabIndex(1, openingElement.attributes), |
| 75 | + )).toBe(true); |
| 76 | + }); |
| 77 | + |
| 78 | + it(`should not identify \`${genElementSymbol(openingElement)}\` with tabIndex of 'bogus' as a focusable element`, () => { |
| 79 | + expect(isFocusable( |
| 80 | + elementType(openingElement), |
| 81 | + mergeTabIndex('bogus', openingElement.attributes), |
| 82 | + )).toBe(false); |
| 83 | + }); |
| 84 | + }); |
| 85 | + }); |
| 86 | +}); |
0 commit comments