| 1 | import test from 'tape';
|
|---|
| 2 | import { elementType } from 'jsx-ast-utils';
|
|---|
| 3 |
|
|---|
| 4 | import isFocusable from '../../../src/util/isFocusable';
|
|---|
| 5 | import {
|
|---|
| 6 | genElementSymbol,
|
|---|
| 7 | genInteractiveElements,
|
|---|
| 8 | genNonInteractiveElements,
|
|---|
| 9 | } from '../../../__mocks__/genInteractives';
|
|---|
| 10 | import JSXAttributeMock from '../../../__mocks__/JSXAttributeMock';
|
|---|
| 11 |
|
|---|
| 12 | function mergeTabIndex(index, attributes) {
|
|---|
| 13 | return [].concat(attributes, JSXAttributeMock('tabIndex', index));
|
|---|
| 14 | }
|
|---|
| 15 |
|
|---|
| 16 | test('isFocusable', (t) => {
|
|---|
| 17 | t.test('interactive elements', (st) => {
|
|---|
| 18 | genInteractiveElements().forEach(({ openingElement }) => {
|
|---|
| 19 | st.equal(
|
|---|
| 20 | isFocusable(
|
|---|
| 21 | elementType(openingElement),
|
|---|
| 22 | openingElement.attributes,
|
|---|
| 23 | ),
|
|---|
| 24 | true,
|
|---|
| 25 | `identifies \`${genElementSymbol(openingElement)}\` as a focusable element`,
|
|---|
| 26 | );
|
|---|
| 27 |
|
|---|
| 28 | st.equal(
|
|---|
| 29 | isFocusable(
|
|---|
| 30 | elementType(openingElement),
|
|---|
| 31 | mergeTabIndex(-1, openingElement.attributes),
|
|---|
| 32 | ),
|
|---|
| 33 | false,
|
|---|
| 34 | `does NOT identify \`${genElementSymbol(openingElement)}\` with tabIndex of -1 as a focusable element`,
|
|---|
| 35 | );
|
|---|
| 36 |
|
|---|
| 37 | st.equal(
|
|---|
| 38 | isFocusable(
|
|---|
| 39 | elementType(openingElement),
|
|---|
| 40 | mergeTabIndex(0, openingElement.attributes),
|
|---|
| 41 | ),
|
|---|
| 42 | true,
|
|---|
| 43 | `identifies \`${genElementSymbol(openingElement)}\` with tabIndex of 0 as a focusable element`,
|
|---|
| 44 | );
|
|---|
| 45 |
|
|---|
| 46 | st.equal(
|
|---|
| 47 | isFocusable(
|
|---|
| 48 | elementType(openingElement),
|
|---|
| 49 | mergeTabIndex(1, openingElement.attributes),
|
|---|
| 50 | ),
|
|---|
| 51 | true,
|
|---|
| 52 | `identifies \`${genElementSymbol(openingElement)}\` with tabIndex of 1 as a focusable element`,
|
|---|
| 53 | );
|
|---|
| 54 | });
|
|---|
| 55 |
|
|---|
| 56 | st.end();
|
|---|
| 57 | });
|
|---|
| 58 |
|
|---|
| 59 | t.test('non-interactive elements', (st) => {
|
|---|
| 60 | genNonInteractiveElements().forEach(({ openingElement }) => {
|
|---|
| 61 | st.equal(
|
|---|
| 62 | isFocusable(
|
|---|
| 63 | elementType(openingElement),
|
|---|
| 64 | openingElement.attributes,
|
|---|
| 65 | ),
|
|---|
| 66 | false,
|
|---|
| 67 | `does NOT identify \`${genElementSymbol(openingElement)}\` as a focusable element`,
|
|---|
| 68 | );
|
|---|
| 69 |
|
|---|
| 70 | st.equal(
|
|---|
| 71 | isFocusable(
|
|---|
| 72 | elementType(openingElement),
|
|---|
| 73 | mergeTabIndex(-1, openingElement.attributes),
|
|---|
| 74 | ),
|
|---|
| 75 | false,
|
|---|
| 76 | `does NOT identify \`${genElementSymbol(openingElement)}\` with tabIndex of -1 as a focusable element`,
|
|---|
| 77 | );
|
|---|
| 78 |
|
|---|
| 79 | st.equal(
|
|---|
| 80 | isFocusable(
|
|---|
| 81 | elementType(openingElement),
|
|---|
| 82 | mergeTabIndex(0, openingElement.attributes),
|
|---|
| 83 | ),
|
|---|
| 84 | true,
|
|---|
| 85 | `identifies \`${genElementSymbol(openingElement)}\` with tabIndex of 0 as a focusable element`,
|
|---|
| 86 | );
|
|---|
| 87 |
|
|---|
| 88 | st.equal(
|
|---|
| 89 | isFocusable(
|
|---|
| 90 | elementType(openingElement),
|
|---|
| 91 | mergeTabIndex(1, openingElement.attributes),
|
|---|
| 92 | ),
|
|---|
| 93 | true,
|
|---|
| 94 | `identifies \`${genElementSymbol(openingElement)}\` with tabIndex of 1 as a focusable element`,
|
|---|
| 95 | );
|
|---|
| 96 |
|
|---|
| 97 | st.equal(
|
|---|
| 98 | isFocusable(
|
|---|
| 99 | elementType(openingElement),
|
|---|
| 100 | mergeTabIndex('bogus', openingElement.attributes),
|
|---|
| 101 | ),
|
|---|
| 102 | false,
|
|---|
| 103 | `does NOT identify \`${genElementSymbol(openingElement)}\` with tabIndex of 'bogus' as a focusable element`,
|
|---|
| 104 | );
|
|---|
| 105 | });
|
|---|
| 106 |
|
|---|
| 107 | st.end();
|
|---|
| 108 | });
|
|---|
| 109 |
|
|---|
| 110 | t.end();
|
|---|
| 111 | });
|
|---|