|
| 1 | +/* eslint-env jest */ |
| 2 | +/** |
| 3 | + * @fileoverview Disallow tabindex on static and noninteractive elements |
| 4 | + * @author jessebeach |
| 5 | + */ |
| 6 | + |
| 7 | +// ----------------------------------------------------------------------------- |
| 8 | +// Requirements |
| 9 | +// ----------------------------------------------------------------------------- |
| 10 | + |
| 11 | +import { RuleTester } from 'eslint'; |
| 12 | +import { configs } from '../../../src/index'; |
| 13 | +import parserOptionsMapper from '../../__util__/parserOptionsMapper'; |
| 14 | +import rule from '../../../src/rules/no-noninteractive-tabindex'; |
| 15 | +import ruleOptionsMapperFactory from '../../__util__/ruleOptionsMapperFactory'; |
| 16 | + |
| 17 | +// ----------------------------------------------------------------------------- |
| 18 | +// Tests |
| 19 | +// ----------------------------------------------------------------------------- |
| 20 | + |
| 21 | +const ruleTester = new RuleTester(); |
| 22 | + |
| 23 | +const ruleName = 'no-noninteractive-tabindex'; |
| 24 | + |
| 25 | +const expectedError = { |
| 26 | + message: '`tabIndex` should only be declared on interactive elements.', |
| 27 | + type: 'JSXAttribute', |
| 28 | +}; |
| 29 | + |
| 30 | +const alwaysValid = [ |
| 31 | + { code: '<MyButton tabIndex={0} />' }, |
| 32 | + { code: '<button />' }, |
| 33 | + { code: '<button tabIndex="0" />' }, |
| 34 | + { code: '<button tabIndex={0} />' }, |
| 35 | + { code: '<div />' }, |
| 36 | + { code: '<div tabIndex="-1" />' }, |
| 37 | + { code: '<div role="button" tabIndex="0" />' }, |
| 38 | + { code: '<div role="article" tabIndex="-1" />' }, |
| 39 | + { code: '<article tabIndex="-1" />' }, |
| 40 | +]; |
| 41 | + |
| 42 | +const neverValid = [ |
| 43 | + { code: '<div tabIndex="0" />', errors: [expectedError] }, |
| 44 | + { code: '<div role="article" tabIndex="0" />', errors: [expectedError] }, |
| 45 | + { code: '<article tabIndex="0" />', errors: [expectedError] }, |
| 46 | + { code: '<article tabIndex={0} />', errors: [expectedError] }, |
| 47 | +]; |
| 48 | + |
| 49 | +const recommendedOptions = ( |
| 50 | + configs.recommended.rules[`jsx-a11y/${ruleName}`][1] || {} |
| 51 | +); |
| 52 | + |
| 53 | +ruleTester.run(`${ruleName}:recommended`, rule, { |
| 54 | + valid: [ |
| 55 | + ...alwaysValid, |
| 56 | + { code: '<div role="tabpanel" tabIndex="0" />' }, |
| 57 | + ] |
| 58 | + .map(ruleOptionsMapperFactory(recommendedOptions)) |
| 59 | + .map(parserOptionsMapper), |
| 60 | + invalid: [ |
| 61 | + ...neverValid, |
| 62 | + ] |
| 63 | + .map(ruleOptionsMapperFactory(recommendedOptions)) |
| 64 | + .map(parserOptionsMapper), |
| 65 | +}); |
| 66 | + |
| 67 | +ruleTester.run(`${ruleName}:strict`, rule, { |
| 68 | + valid: [ |
| 69 | + ...alwaysValid, |
| 70 | + ].map(parserOptionsMapper), |
| 71 | + invalid: [ |
| 72 | + ...neverValid, |
| 73 | + { code: '<div role="tabpanel" tabIndex="0" />', errors: [expectedError] }, |
| 74 | + ].map(parserOptionsMapper), |
| 75 | +}); |
0 commit comments