|
| 1 | +/** |
| 2 | + * @fileoverview Enforce tabIndex value is not greater than zero. |
| 3 | + * @author Ethan Cohen |
| 4 | + */ |
| 5 | + |
| 6 | +'use strict'; |
| 7 | + |
| 8 | +// ----------------------------------------------------------------------------- |
| 9 | +// Requirements |
| 10 | +// ----------------------------------------------------------------------------- |
| 11 | + |
| 12 | +import rule from '../../../src/rules/avoid-positive-tabindex'; |
| 13 | +import { RuleTester } from 'eslint'; |
| 14 | + |
| 15 | +const parserOptions = { |
| 16 | + ecmaVersion: 6, |
| 17 | + ecmaFeatures: { |
| 18 | + jsx: true |
| 19 | + } |
| 20 | +}; |
| 21 | + |
| 22 | +// ----------------------------------------------------------------------------- |
| 23 | +// Tests |
| 24 | +// ----------------------------------------------------------------------------- |
| 25 | + |
| 26 | +const ruleTester = new RuleTester(); |
| 27 | + |
| 28 | +const expectedError = { |
| 29 | + message: 'Avoid positive integer values for tabIndex.', |
| 30 | + type: 'JSXAttribute' |
| 31 | +}; |
| 32 | + |
| 33 | +ruleTester.run('avoid-positive-tabindex', rule, { |
| 34 | + valid: [ |
| 35 | + { code: '<div />;', parserOptions }, |
| 36 | + { code: '<div {...props} />', parserOptions }, |
| 37 | + { code: '<div tabIndex={undefined} />', parserOptions }, |
| 38 | + { code: '<div tabIndex={`${undefined}`} />', parserOptions }, |
| 39 | + { code: '<div tabIndex={`${undefined}${undefined}`} />', parserOptions }, |
| 40 | + { code: '<div tabIndex={0} />', parserOptions }, |
| 41 | + { code: '<div tabIndex={-1} />', parserOptions }, |
| 42 | + { code: '<div tabIndex={null} />', parserOptions }, |
| 43 | + { code: '<div tabIndex={bar()} />', parserOptions }, |
| 44 | + { code: '<div tabIndex={bar} />', parserOptions }, |
| 45 | + { code: '<div tabIndex={"foobar"} />', parserOptions }, |
| 46 | + { code: '<div tabIndex="0" />', parserOptions }, |
| 47 | + { code: '<div tabIndex="-1" />', parserOptions }, |
| 48 | + { code: '<div tabIndex="-5" />', parserOptions }, |
| 49 | + { code: '<div tabIndex="-5.5" />', parserOptions }, |
| 50 | + { code: '<div tabIndex={-5.5} />', parserOptions }, |
| 51 | + { code: '<div tabIndex={-5} />', parserOptions } |
| 52 | + ], |
| 53 | + |
| 54 | + invalid: [ |
| 55 | + { code: '<div tabIndex="1" />', errors: [ expectedError ], parserOptions }, |
| 56 | + { code: '<div tabIndex={1} />', errors: [ expectedError ], parserOptions }, |
| 57 | + { code: '<div tabIndex={"1"} />', errors: [ expectedError ], parserOptions }, |
| 58 | + { code: '<div tabIndex={`1`} />', errors: [ expectedError ], parserOptions }, |
| 59 | + { code: '<div tabIndex={1.589} />', errors: [ expectedError ], parserOptions } |
| 60 | + ] |
| 61 | +}); |
0 commit comments