|
| 1 | +/** |
| 2 | + * @fileoverview Enforce that elements with ARIA roles must have all required attributes for that role. |
| 3 | + * @author Ethan Cohen |
| 4 | + */ |
| 5 | + |
| 6 | +'use strict'; |
| 7 | + |
| 8 | +// ----------------------------------------------------------------------------- |
| 9 | +// Requirements |
| 10 | +// ----------------------------------------------------------------------------- |
| 11 | + |
| 12 | +import rule from '../../../src/rules/role-requires-aria'; |
| 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 | +import validRoleTypes from '../../../src/util/attributes/role'; |
| 29 | + |
| 30 | +const errorMessage = role => ({ |
| 31 | + message: `Elements with the ARIA role "${role}" must have the following ` + |
| 32 | + `attributes defined: ${validRoleTypes[role.toUpperCase()].requiredProps.toString().toLowerCase()}`, |
| 33 | + type: 'JSXAttribute' |
| 34 | +}); |
| 35 | + |
| 36 | + |
| 37 | +// Create basic test cases using all valid role types. |
| 38 | +const basicValidityTests = Object.keys(validRoleTypes).map(role => { |
| 39 | + const { requiredProps } = validRoleTypes[role]; |
| 40 | + const propChain = requiredProps.join(' ').toLowerCase(); |
| 41 | + |
| 42 | + return { |
| 43 | + code: `<div role="${role.toLowerCase()}" ${propChain} />`, |
| 44 | + parserOptions |
| 45 | + }; |
| 46 | +}); |
| 47 | + |
| 48 | +ruleTester.run('role-requires-aria', rule, { |
| 49 | + valid: [ |
| 50 | + // Variables should pass, as we are only testing literals. |
| 51 | + { code: '<div />', parserOptions }, |
| 52 | + { code: '<div></div>', parserOptions }, |
| 53 | + { code: '<div role={role} />', parserOptions }, |
| 54 | + { code: '<div role={role || "button"} />', parserOptions }, |
| 55 | + { code: '<div role={role || "foobar"} />', parserOptions }, |
| 56 | + { code: '<div role="tabpanel row" />', parserOptions }, |
| 57 | + { code: '<span role="checkbox" aria-checked="false" aria-labelledby="foo" tabindex="0"></span>', parserOptions }, |
| 58 | + { code: '<Bar baz />', parserOptions } |
| 59 | + ].concat(basicValidityTests), |
| 60 | + invalid: [ |
| 61 | + // SLIDER |
| 62 | + { code: '<div role="slider" />', errors: [ errorMessage('slider') ], parserOptions }, |
| 63 | + { code: '<div role="slider" aria-valuemax />', errors: [ errorMessage('slider') ], parserOptions }, |
| 64 | + { code: '<div role="slider" aria-valuemax aria-valuemin />', errors: [ errorMessage('slider') ], parserOptions }, |
| 65 | + { code: '<div role="slider" aria-valuemax aria-valuenow />', errors: [ errorMessage('slider') ], parserOptions }, |
| 66 | + { code: '<div role="slider" aria-valuemin aria-valuenow />', errors: [ errorMessage('slider') ], parserOptions }, |
| 67 | + |
| 68 | + // SPINBUTTON |
| 69 | + { code: '<div role="spinbutton" />', errors: [ errorMessage('spinbutton') ], parserOptions }, |
| 70 | + { code: '<div role="spinbutton" aria-valuemax />', errors: [ errorMessage('spinbutton') ], parserOptions }, |
| 71 | + { code: '<div role="spinbutton" aria-valuemax aria-valuemin />', errors: [ errorMessage('spinbutton') ], parserOptions }, |
| 72 | + { code: '<div role="spinbutton" aria-valuemax aria-valuenow />', errors: [ errorMessage('spinbutton') ], parserOptions }, |
| 73 | + { code: '<div role="spinbutton" aria-valuemin aria-valuenow />', errors: [ errorMessage('spinbutton') ], parserOptions }, |
| 74 | + |
| 75 | + // CHECKBOX |
| 76 | + { code: '<div role="checkbox" />', errors: [ errorMessage('checkbox') ], parserOptions }, |
| 77 | + { code: '<div role="checkbox" checked />', errors: [ errorMessage('checkbox') ], parserOptions }, |
| 78 | + { code: '<div role="checkbox" aria-chcked />', errors: [ errorMessage('checkbox') ], parserOptions }, |
| 79 | + { |
| 80 | + code: '<span role="checkbox" aria-labelledby="foo" tabindex="0"></span>', |
| 81 | + errors: [ errorMessage('checkbox') ], |
| 82 | + parserOptions |
| 83 | + }, |
| 84 | + |
| 85 | + // COMBOBOX |
| 86 | + { code: '<div role="combobox" />', errors: [ errorMessage('combobox') ], parserOptions }, |
| 87 | + { code: '<div role="combobox" expanded />', errors: [ errorMessage('combobox') ], parserOptions }, |
| 88 | + { code: '<div role="combobox" aria-expandd />', errors: [ errorMessage('combobox') ], parserOptions }, |
| 89 | + |
| 90 | + // SCROLLBAR |
| 91 | + { code: '<div role="scrollbar" />', errors: [ errorMessage('scrollbar') ], parserOptions }, |
| 92 | + { code: '<div role="scrollbar" aria-valuemax />', errors: [ errorMessage('scrollbar') ], parserOptions }, |
| 93 | + { code: '<div role="scrollbar" aria-valuemax aria-valuemin />', errors: [ errorMessage('scrollbar') ], parserOptions }, |
| 94 | + { code: '<div role="scrollbar" aria-valuemax aria-valuenow />', errors: [ errorMessage('scrollbar') ], parserOptions }, |
| 95 | + { code: '<div role="scrollbar" aria-valuemin aria-valuenow />', errors: [ errorMessage('scrollbar') ], parserOptions } |
| 96 | + ] |
| 97 | +}); |
0 commit comments