| 1 | /**
|
|---|
| 2 | * @fileoverview Enforce all aria-* properties are valid.
|
|---|
| 3 | * @author Ethan Cohen
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | // -----------------------------------------------------------------------------
|
|---|
| 7 | // Requirements
|
|---|
| 8 | // -----------------------------------------------------------------------------
|
|---|
| 9 |
|
|---|
| 10 | import { aria } from 'aria-query';
|
|---|
| 11 | import { RuleTester } from 'eslint';
|
|---|
| 12 | import parserOptionsMapper from '../../__util__/parserOptionsMapper';
|
|---|
| 13 | import parsers from '../../__util__/helpers/parsers';
|
|---|
| 14 | import rule from '../../../src/rules/aria-props';
|
|---|
| 15 | import getSuggestion from '../../../src/util/getSuggestion';
|
|---|
| 16 |
|
|---|
| 17 | // -----------------------------------------------------------------------------
|
|---|
| 18 | // Tests
|
|---|
| 19 | // -----------------------------------------------------------------------------
|
|---|
| 20 |
|
|---|
| 21 | const ruleTester = new RuleTester();
|
|---|
| 22 | const ariaAttributes = aria.keys();
|
|---|
| 23 |
|
|---|
| 24 | const errorMessage = (name) => {
|
|---|
| 25 | const suggestions = getSuggestion(name, ariaAttributes);
|
|---|
| 26 | const message = `${name}: This attribute is an invalid ARIA attribute.`;
|
|---|
| 27 |
|
|---|
| 28 | if (suggestions.length > 0) {
|
|---|
| 29 | return {
|
|---|
| 30 | type: 'JSXAttribute',
|
|---|
| 31 | message: `${message} Did you mean to use ${suggestions}?`,
|
|---|
| 32 | };
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | return {
|
|---|
| 36 | type: 'JSXAttribute',
|
|---|
| 37 | message,
|
|---|
| 38 | };
|
|---|
| 39 | };
|
|---|
| 40 |
|
|---|
| 41 | // Create basic test cases using all valid role types.
|
|---|
| 42 | const basicValidityTests = ariaAttributes.map((prop) => ({
|
|---|
| 43 | code: `<div ${prop.toLowerCase()}="foobar" />`,
|
|---|
| 44 | }));
|
|---|
| 45 |
|
|---|
| 46 | ruleTester.run('aria-props', rule, {
|
|---|
| 47 | valid: parsers.all([].concat(
|
|---|
| 48 | // Variables should pass, as we are only testing literals.
|
|---|
| 49 | { code: '<div />' },
|
|---|
| 50 | { code: '<div></div>' },
|
|---|
| 51 | { code: '<div aria="wee"></div>' }, // Needs aria-*
|
|---|
| 52 | { code: '<div abcARIAdef="true"></div>' },
|
|---|
| 53 | { code: '<div fooaria-foobar="true"></div>' },
|
|---|
| 54 | { code: '<div fooaria-hidden="true"></div>' },
|
|---|
| 55 | { code: '<Bar baz />' },
|
|---|
| 56 | { code: '<input type="text" aria-errormessage="foobar" />' },
|
|---|
| 57 | )).concat(basicValidityTests).map(parserOptionsMapper),
|
|---|
| 58 | invalid: parsers.all([].concat(
|
|---|
| 59 | { code: '<div aria-="foobar" />', errors: [errorMessage('aria-')] },
|
|---|
| 60 | {
|
|---|
| 61 | code: '<div aria-labeledby="foobar" />',
|
|---|
| 62 | errors: [errorMessage('aria-labeledby')],
|
|---|
| 63 | },
|
|---|
| 64 | {
|
|---|
| 65 | code: '<div aria-skldjfaria-klajsd="foobar" />',
|
|---|
| 66 | errors: [errorMessage('aria-skldjfaria-klajsd')],
|
|---|
| 67 | },
|
|---|
| 68 | )).map(parserOptionsMapper),
|
|---|
| 69 | });
|
|---|