| 1 | /**
|
|---|
| 2 | * @fileoverview Enforce that elements with ARIA roles must
|
|---|
| 3 | * have all required attributes for that role.
|
|---|
| 4 | * @author Ethan Cohen
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 | // -----------------------------------------------------------------------------
|
|---|
| 8 | // Requirements
|
|---|
| 9 | // -----------------------------------------------------------------------------
|
|---|
| 10 |
|
|---|
| 11 | import { roles } from 'aria-query';
|
|---|
| 12 | import { RuleTester } from 'eslint';
|
|---|
| 13 |
|
|---|
| 14 | import parserOptionsMapper from '../../__util__/parserOptionsMapper';
|
|---|
| 15 | import parsers from '../../__util__/helpers/parsers';
|
|---|
| 16 | import rule from '../../../src/rules/role-has-required-aria-props';
|
|---|
| 17 |
|
|---|
| 18 | // -----------------------------------------------------------------------------
|
|---|
| 19 | // Tests
|
|---|
| 20 | // -----------------------------------------------------------------------------
|
|---|
| 21 |
|
|---|
| 22 | const ruleTester = new RuleTester();
|
|---|
| 23 |
|
|---|
| 24 | const errorMessage = (role) => {
|
|---|
| 25 | const requiredProps = Object.keys(roles.get(role).requiredProps);
|
|---|
| 26 |
|
|---|
| 27 | return {
|
|---|
| 28 | message: `Elements with the ARIA role "${role}" must have the following attributes defined: ${requiredProps}`,
|
|---|
| 29 | type: 'JSXAttribute',
|
|---|
| 30 | };
|
|---|
| 31 | };
|
|---|
| 32 |
|
|---|
| 33 | const componentsSettings = {
|
|---|
| 34 | 'jsx-a11y': {
|
|---|
| 35 | components: {
|
|---|
| 36 | MyComponent: 'div',
|
|---|
| 37 | },
|
|---|
| 38 | },
|
|---|
| 39 | };
|
|---|
| 40 |
|
|---|
| 41 | // Create basic test cases using all valid role types.
|
|---|
| 42 | const basicValidityTests = roles.keys().map((role) => {
|
|---|
| 43 | const {
|
|---|
| 44 | requiredProps: requiredPropKeyValues,
|
|---|
| 45 | } = roles.get(role);
|
|---|
| 46 | const requiredProps = Object.keys(requiredPropKeyValues);
|
|---|
| 47 | const propChain = requiredProps.join(' ');
|
|---|
| 48 |
|
|---|
| 49 | return {
|
|---|
| 50 | code: `<div role="${role.toLowerCase()}" ${propChain} />`,
|
|---|
| 51 | };
|
|---|
| 52 | });
|
|---|
| 53 |
|
|---|
| 54 | ruleTester.run('role-has-required-aria-props', rule, {
|
|---|
| 55 | valid: parsers.all([].concat(
|
|---|
| 56 | { code: '<Bar baz />' },
|
|---|
| 57 | { code: '<MyComponent role="combobox" />' },
|
|---|
| 58 | // Variables should pass, as we are only testing literals.
|
|---|
| 59 | { code: '<div />' },
|
|---|
| 60 | { code: '<div></div>' },
|
|---|
| 61 | { code: '<div role={role} />' },
|
|---|
| 62 | { code: '<div role={role || "button"} />' },
|
|---|
| 63 | { code: '<div role={role || "foobar"} />' },
|
|---|
| 64 | { code: '<div role="row" />' },
|
|---|
| 65 | { code: '<span role="checkbox" aria-checked="false" aria-labelledby="foo" tabindex="0"></span>' },
|
|---|
| 66 | { code: '<input role="checkbox" aria-checked="false" aria-labelledby="foo" tabindex="0" {...props} type="checkbox" />' },
|
|---|
| 67 | { code: '<input type="checkbox" role="switch" />' },
|
|---|
| 68 | { code: '<MyComponent role="checkbox" aria-checked="false" aria-labelledby="foo" tabindex="0" />', settings: componentsSettings },
|
|---|
| 69 | { code: '<div role="heading" aria-level={2} />' },
|
|---|
| 70 | { code: '<div role="heading" aria-level="3" />' },
|
|---|
| 71 | )).concat(basicValidityTests).map(parserOptionsMapper),
|
|---|
| 72 |
|
|---|
| 73 | invalid: parsers.all([].concat(
|
|---|
| 74 | // SLIDER
|
|---|
| 75 | { code: '<div role="slider" />', errors: [errorMessage('slider')] },
|
|---|
| 76 | {
|
|---|
| 77 | code: '<div role="slider" aria-valuemax />',
|
|---|
| 78 | errors: [errorMessage('slider')],
|
|---|
| 79 | },
|
|---|
| 80 | {
|
|---|
| 81 | code: '<div role="slider" aria-valuemax aria-valuemin />',
|
|---|
| 82 | errors: [errorMessage('slider')],
|
|---|
| 83 | },
|
|---|
| 84 |
|
|---|
| 85 | // CHECKBOX
|
|---|
| 86 | { code: '<div role="checkbox" />', errors: [errorMessage('checkbox')] },
|
|---|
| 87 | { code: '<div role="checkbox" checked />', errors: [errorMessage('checkbox')] },
|
|---|
| 88 | {
|
|---|
| 89 | code: '<div role="checkbox" aria-chcked />',
|
|---|
| 90 | errors: [errorMessage('checkbox')],
|
|---|
| 91 | },
|
|---|
| 92 | {
|
|---|
| 93 | code: '<span role="checkbox" aria-labelledby="foo" tabindex="0"></span>',
|
|---|
| 94 | errors: [errorMessage('checkbox')],
|
|---|
| 95 | },
|
|---|
| 96 |
|
|---|
| 97 | // COMBOBOX
|
|---|
| 98 | { code: '<div role="combobox" />', errors: [errorMessage('combobox')] },
|
|---|
| 99 | { code: '<div role="combobox" expanded />', errors: [errorMessage('combobox')] },
|
|---|
| 100 | {
|
|---|
| 101 | code: '<div role="combobox" aria-expandd />',
|
|---|
| 102 | errors: [errorMessage('combobox')],
|
|---|
| 103 | },
|
|---|
| 104 |
|
|---|
| 105 | // SCROLLBAR
|
|---|
| 106 | { code: '<div role="scrollbar" />', errors: [errorMessage('scrollbar')] },
|
|---|
| 107 | {
|
|---|
| 108 | code: '<div role="scrollbar" aria-valuemax />',
|
|---|
| 109 | errors: [errorMessage('scrollbar')],
|
|---|
| 110 | },
|
|---|
| 111 | {
|
|---|
| 112 | code: '<div role="scrollbar" aria-valuemax aria-valuemin />',
|
|---|
| 113 | errors: [errorMessage('scrollbar')],
|
|---|
| 114 | },
|
|---|
| 115 | {
|
|---|
| 116 | code: '<div role="scrollbar" aria-valuemax aria-valuenow />',
|
|---|
| 117 | errors: [errorMessage('scrollbar')],
|
|---|
| 118 | },
|
|---|
| 119 | {
|
|---|
| 120 | code: '<div role="scrollbar" aria-valuemin aria-valuenow />',
|
|---|
| 121 | errors: [errorMessage('scrollbar')],
|
|---|
| 122 | },
|
|---|
| 123 | {
|
|---|
| 124 | code: '<div role="heading" />',
|
|---|
| 125 | errors: [errorMessage('heading')],
|
|---|
| 126 | },
|
|---|
| 127 | {
|
|---|
| 128 | code: '<div role="option" />',
|
|---|
| 129 | errors: [errorMessage('option')],
|
|---|
| 130 | },
|
|---|
| 131 | // Custom element
|
|---|
| 132 | { code: '<MyComponent role="combobox" />', settings: componentsSettings, errors: [errorMessage('combobox')] },
|
|---|
| 133 | )).map(parserOptionsMapper),
|
|---|
| 134 | });
|
|---|