| 1 | /**
|
|---|
| 2 | * @fileoverview Disallow tabindex on static and noninteractive elements
|
|---|
| 3 | * @author jessebeach
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | // -----------------------------------------------------------------------------
|
|---|
| 7 | // Requirements
|
|---|
| 8 | // -----------------------------------------------------------------------------
|
|---|
| 9 |
|
|---|
| 10 | import { RuleTester } from 'eslint';
|
|---|
| 11 | import { configs } from '../../../src/index';
|
|---|
| 12 | import parserOptionsMapper from '../../__util__/parserOptionsMapper';
|
|---|
| 13 | import parsers from '../../__util__/helpers/parsers';
|
|---|
| 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 componentsSettings = {
|
|---|
| 31 | 'jsx-a11y': {
|
|---|
| 32 | components: {
|
|---|
| 33 | Article: 'article',
|
|---|
| 34 | MyButton: 'button',
|
|---|
| 35 | },
|
|---|
| 36 | },
|
|---|
| 37 | };
|
|---|
| 38 |
|
|---|
| 39 | const alwaysValid = [
|
|---|
| 40 | { code: '<MyButton tabIndex={0} />' },
|
|---|
| 41 | { code: '<button />' },
|
|---|
| 42 | { code: '<button tabIndex="0" />' },
|
|---|
| 43 | { code: '<button tabIndex={0} />' },
|
|---|
| 44 | { code: '<div />' },
|
|---|
| 45 | { code: '<div tabIndex="-1" />' },
|
|---|
| 46 | { code: '<div role="button" tabIndex="0" />' },
|
|---|
| 47 | { code: '<div role="article" tabIndex="-1" />' },
|
|---|
| 48 | { code: '<article tabIndex="-1" />' },
|
|---|
| 49 | { code: '<Article tabIndex="-1" />', settings: componentsSettings },
|
|---|
| 50 | { code: '<MyButton tabIndex={0} />', settings: componentsSettings },
|
|---|
| 51 | ];
|
|---|
| 52 |
|
|---|
| 53 | const neverValid = [
|
|---|
| 54 | { code: '<div tabIndex="0" />', errors: [expectedError] },
|
|---|
| 55 | { code: '<div role="article" tabIndex="0" />', errors: [expectedError] },
|
|---|
| 56 | { code: '<article tabIndex="0" />', errors: [expectedError] },
|
|---|
| 57 | { code: '<article tabIndex={0} />', errors: [expectedError] },
|
|---|
| 58 | { code: '<Article tabIndex={0} />', errors: [expectedError], settings: componentsSettings },
|
|---|
| 59 | ];
|
|---|
| 60 |
|
|---|
| 61 | const recommendedOptions = (
|
|---|
| 62 | configs.recommended.rules[`jsx-a11y/${ruleName}`][1] || {}
|
|---|
| 63 | );
|
|---|
| 64 |
|
|---|
| 65 | ruleTester.run(`${ruleName}:recommended`, rule, {
|
|---|
| 66 | valid: parsers.all([].concat(
|
|---|
| 67 | ...alwaysValid,
|
|---|
| 68 | { code: '<div role="tabpanel" tabIndex="0" />' },
|
|---|
| 69 | // Expressions should pass in recommended mode
|
|---|
| 70 | { code: '<div role={ROLE_BUTTON} onClick={() => {}} tabIndex="0" />;' },
|
|---|
| 71 | // Cases for allowExpressionValues set to true
|
|---|
| 72 | {
|
|---|
| 73 | code: '<div role={BUTTON} onClick={() => {}} tabIndex="0" />;',
|
|---|
| 74 | options: [{ allowExpressionValues: true }],
|
|---|
| 75 | },
|
|---|
| 76 | // Specific case for ternary operator with literals on both side
|
|---|
| 77 | {
|
|---|
| 78 | code: '<div role={isButton ? "button" : "link"} onClick={() => {}} tabIndex="0" />;',
|
|---|
| 79 | options: [{ allowExpressionValues: true }],
|
|---|
| 80 | },
|
|---|
| 81 | {
|
|---|
| 82 | code: '<div role={isButton ? "button" : LINK} onClick={() => {}} tabIndex="0" />;',
|
|---|
| 83 | options: [{ allowExpressionValues: true }],
|
|---|
| 84 | errors: [expectedError],
|
|---|
| 85 | },
|
|---|
| 86 | {
|
|---|
| 87 | code: '<div role={isButton ? BUTTON : LINK} onClick={() => {}} tabIndex="0"/>;',
|
|---|
| 88 | options: [{ allowExpressionValues: true }],
|
|---|
| 89 | errors: [expectedError],
|
|---|
| 90 | },
|
|---|
| 91 | ))
|
|---|
| 92 | .map(ruleOptionsMapperFactory(recommendedOptions))
|
|---|
| 93 | .map(parserOptionsMapper),
|
|---|
| 94 | invalid: parsers.all([].concat(
|
|---|
| 95 | ...neverValid,
|
|---|
| 96 | ))
|
|---|
| 97 | .map(ruleOptionsMapperFactory(recommendedOptions))
|
|---|
| 98 | .map(parserOptionsMapper),
|
|---|
| 99 | });
|
|---|
| 100 |
|
|---|
| 101 | ruleTester.run(`${ruleName}:strict`, rule, {
|
|---|
| 102 | valid: parsers.all([].concat(
|
|---|
| 103 | ...alwaysValid,
|
|---|
| 104 | )).map(parserOptionsMapper),
|
|---|
| 105 | invalid: parsers.all([].concat(
|
|---|
| 106 | ...neverValid,
|
|---|
| 107 | { code: '<div role="tabpanel" tabIndex="0" />', errors: [expectedError] },
|
|---|
| 108 | // Expressions should fail in strict mode
|
|---|
| 109 | { code: '<div role={ROLE_BUTTON} onClick={() => {}} tabIndex="0" />;', errors: [expectedError] },
|
|---|
| 110 | // Cases for allowExpressionValues set to false
|
|---|
| 111 | {
|
|---|
| 112 | code: '<div role={BUTTON} onClick={() => {}} tabIndex="0" />;',
|
|---|
| 113 | options: [{ allowExpressionValues: false }],
|
|---|
| 114 | errors: [expectedError],
|
|---|
| 115 | },
|
|---|
| 116 | // Specific case for ternary operator with literals on both side
|
|---|
| 117 | {
|
|---|
| 118 | code: '<div role={isButton ? "button" : "link"} onClick={() => {}} tabIndex="0" />;',
|
|---|
| 119 | options: [{ allowExpressionValues: false }],
|
|---|
| 120 | errors: [expectedError],
|
|---|
| 121 | },
|
|---|
| 122 | )).map(parserOptionsMapper),
|
|---|
| 123 | });
|
|---|