source: frontend/node_modules/eslint-plugin-jsx-a11y/__tests__/src/rules/no-noninteractive-tabindex-test.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 2 weeks ago

Fix frontend appearance

  • Property mode set to 100644
File size: 4.3 KB
Line 
1/**
2 * @fileoverview Disallow tabindex on static and noninteractive elements
3 * @author jessebeach
4 */
5
6// -----------------------------------------------------------------------------
7// Requirements
8// -----------------------------------------------------------------------------
9
10import { RuleTester } from 'eslint';
11import { configs } from '../../../src/index';
12import parserOptionsMapper from '../../__util__/parserOptionsMapper';
13import parsers from '../../__util__/helpers/parsers';
14import rule from '../../../src/rules/no-noninteractive-tabindex';
15import ruleOptionsMapperFactory from '../../__util__/ruleOptionsMapperFactory';
16
17// -----------------------------------------------------------------------------
18// Tests
19// -----------------------------------------------------------------------------
20
21const ruleTester = new RuleTester();
22
23const ruleName = 'no-noninteractive-tabindex';
24
25const expectedError = {
26 message: '`tabIndex` should only be declared on interactive elements.',
27 type: 'JSXAttribute',
28};
29
30const componentsSettings = {
31 'jsx-a11y': {
32 components: {
33 Article: 'article',
34 MyButton: 'button',
35 },
36 },
37};
38
39const 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
53const 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
61const recommendedOptions = (
62 configs.recommended.rules[`jsx-a11y/${ruleName}`][1] || {}
63);
64
65ruleTester.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
101ruleTester.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});
Note: See TracBrowser for help on using the repository browser.