| 1 | /**
|
|---|
| 2 | * @fileoverview Enforce heading (h1, h2, etc) elements contain accessible content.
|
|---|
| 3 | * @author Ethan Cohen
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | // -----------------------------------------------------------------------------
|
|---|
| 7 | // Requirements
|
|---|
| 8 | // -----------------------------------------------------------------------------
|
|---|
| 9 |
|
|---|
| 10 | import { RuleTester } from 'eslint';
|
|---|
| 11 | import parserOptionsMapper from '../../__util__/parserOptionsMapper';
|
|---|
| 12 | import parsers from '../../__util__/helpers/parsers';
|
|---|
| 13 | import rule from '../../../src/rules/heading-has-content';
|
|---|
| 14 |
|
|---|
| 15 | // -----------------------------------------------------------------------------
|
|---|
| 16 | // Tests
|
|---|
| 17 | // -----------------------------------------------------------------------------
|
|---|
| 18 |
|
|---|
| 19 | const ruleTester = new RuleTester();
|
|---|
| 20 |
|
|---|
| 21 | const expectedError = {
|
|---|
| 22 | message: 'Headings must have content and the content must be accessible by a screen reader.',
|
|---|
| 23 | type: 'JSXOpeningElement',
|
|---|
| 24 | };
|
|---|
| 25 |
|
|---|
| 26 | const components = [{
|
|---|
| 27 | components: ['Heading', 'Title'],
|
|---|
| 28 | }];
|
|---|
| 29 |
|
|---|
| 30 | const componentsSettings = {
|
|---|
| 31 | 'jsx-a11y': {
|
|---|
| 32 | components: {
|
|---|
| 33 | CustomInput: 'input',
|
|---|
| 34 | Title: 'h1',
|
|---|
| 35 | Heading: 'h2',
|
|---|
| 36 | },
|
|---|
| 37 | },
|
|---|
| 38 | };
|
|---|
| 39 |
|
|---|
| 40 | ruleTester.run('heading-has-content', rule, {
|
|---|
| 41 | valid: parsers.all([].concat(
|
|---|
| 42 | // DEFAULT ELEMENT TESTS
|
|---|
| 43 | { code: '<div />;' },
|
|---|
| 44 | { code: '<h1>Foo</h1>' },
|
|---|
| 45 | { code: '<h2>Foo</h2>' },
|
|---|
| 46 | { code: '<h3>Foo</h3>' },
|
|---|
| 47 | { code: '<h4>Foo</h4>' },
|
|---|
| 48 | { code: '<h5>Foo</h5>' },
|
|---|
| 49 | { code: '<h6>Foo</h6>' },
|
|---|
| 50 | { code: '<h6>123</h6>' },
|
|---|
| 51 | { code: '<h1><Bar /></h1>' },
|
|---|
| 52 | { code: '<h1>{foo}</h1>' },
|
|---|
| 53 | { code: '<h1>{foo.bar}</h1>' },
|
|---|
| 54 | { code: '<h1 dangerouslySetInnerHTML={{ __html: "foo" }} />' },
|
|---|
| 55 | { code: '<h1 children={children} />' },
|
|---|
| 56 | // CUSTOM ELEMENT TESTS FOR COMPONENTS OPTION
|
|---|
| 57 | { code: '<Heading>Foo</Heading>', options: components },
|
|---|
| 58 | { code: '<Title>Foo</Title>', options: components },
|
|---|
| 59 | { code: '<Heading><Bar /></Heading>', options: components },
|
|---|
| 60 | { code: '<Heading>{foo}</Heading>', options: components },
|
|---|
| 61 | { code: '<Heading>{foo.bar}</Heading>', options: components },
|
|---|
| 62 | { code: '<Heading dangerouslySetInnerHTML={{ __html: "foo" }} />', options: components },
|
|---|
| 63 | { code: '<Heading children={children} />', options: components },
|
|---|
| 64 | { code: '<h1 aria-hidden />' },
|
|---|
| 65 | // CUSTOM ELEMENT TESTS FOR COMPONENTS SETTINGS
|
|---|
| 66 | { code: '<Heading>Foo</Heading>', settings: componentsSettings },
|
|---|
| 67 | { code: '<h1><CustomInput type="hidden" /></h1>' },
|
|---|
| 68 | )).map(parserOptionsMapper),
|
|---|
| 69 | invalid: parsers.all([].concat(
|
|---|
| 70 | // DEFAULT ELEMENT TESTS
|
|---|
| 71 | { code: '<h1 />', errors: [expectedError] },
|
|---|
| 72 | { code: '<h1><Bar aria-hidden /></h1>', errors: [expectedError] },
|
|---|
| 73 | { code: '<h1>{undefined}</h1>', errors: [expectedError] },
|
|---|
| 74 | { code: '<h1><input type="hidden" /></h1>', errors: [expectedError] },
|
|---|
| 75 |
|
|---|
| 76 | // CUSTOM ELEMENT TESTS FOR COMPONENTS OPTION
|
|---|
| 77 | { code: '<Heading />', errors: [expectedError], options: components },
|
|---|
| 78 | { code: '<Heading><Bar aria-hidden /></Heading>', errors: [expectedError], options: components },
|
|---|
| 79 | { code: '<Heading>{undefined}</Heading>', errors: [expectedError], options: components },
|
|---|
| 80 |
|
|---|
| 81 | // CUSTOM ELEMENT TESTS FOR COMPONENTS SETTINGS
|
|---|
| 82 | { code: '<Heading />', errors: [expectedError], settings: componentsSettings },
|
|---|
| 83 | { code: '<h1><CustomInput type="hidden" /></h1>', errors: [expectedError], settings: componentsSettings },
|
|---|
| 84 | )).map(parserOptionsMapper),
|
|---|
| 85 | });
|
|---|