|
| 1 | +/** |
| 2 | + * @fileoverview Enforce all aria-* properties are valid. |
| 3 | + * @author Ethan Cohen |
| 4 | + */ |
| 5 | + |
| 6 | +'use strict'; |
| 7 | + |
| 8 | +// ----------------------------------------------------------------------------- |
| 9 | +// Requirements |
| 10 | +// ----------------------------------------------------------------------------- |
| 11 | + |
| 12 | +import rule from '../../../src/rules/no-invalid-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 | +const errorMessage = name => ({ |
| 29 | + message: `${name}: This attribute is an invalid ARIA attribute.`, |
| 30 | + type: 'JSXAttribute' |
| 31 | +}); |
| 32 | + |
| 33 | +import validAriaProperties from '../../../src/util/validAriaProperties'; |
| 34 | + |
| 35 | +// Create basic test cases using all valid role types. |
| 36 | +const basicValidityTests = validAriaProperties.map(prop => ({ |
| 37 | + code: `<div ${prop.toLowerCase()}="foobar" />`, |
| 38 | + parserOptions |
| 39 | +})); |
| 40 | + |
| 41 | +ruleTester.run('no-invalid-aria', rule, { |
| 42 | + valid: [ |
| 43 | + // Variables should pass, as we are only testing literals. |
| 44 | + { code: '<div />', parserOptions }, |
| 45 | + { code: '<div></div>', parserOptions }, |
| 46 | + { code: '<div aria="wee"></div>', parserOptions }, // Needs aria-* |
| 47 | + { code: '<div abcARIAdef="true"></div>', parserOptions }, |
| 48 | + { code: '<Bar baz />', parserOptions } |
| 49 | + ].concat(basicValidityTests), |
| 50 | + invalid: [ |
| 51 | + { code: '<div aria-="foobar" />', errors: [ errorMessage('aria-') ], parserOptions }, |
| 52 | + { code: '<div aria-labeledby="foobar" />', errors: [ errorMessage('aria-labeledby') ], parserOptions }, |
| 53 | + { code: '<div aria-skldjfaria-klajsd="foobar" />', errors: [ errorMessage('aria-skldjfaria-klajsd') ], parserOptions } |
| 54 | + ] |
| 55 | +}); |
0 commit comments