|
| 1 | +/** |
| 2 | + * @fileoverview Enforce img alt attribute does not have the word image, picture, or photo. |
| 3 | + * @author Ethan Cohen |
| 4 | + */ |
| 5 | + |
| 6 | +'use strict'; |
| 7 | + |
| 8 | +// ----------------------------------------------------------------------------- |
| 9 | +// Requirements |
| 10 | +// ----------------------------------------------------------------------------- |
| 11 | + |
| 12 | +import rule from '../../../src/rules/redundant-alt'; |
| 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 expectedError = { |
| 29 | + message: 'Redundant alt attribute. Screen-readers already announce `img` tags as an image. ' + |
| 30 | + 'You don\'t need to use the words `image`, `photo,` or `picture` in the alt prop.', |
| 31 | + type: 'JSXOpeningElement' |
| 32 | +}; |
| 33 | + |
| 34 | +ruleTester.run('img-uses-alt', rule, { |
| 35 | + valid: [ |
| 36 | + { code: '<img alt="foo" />;', parserOptions }, |
| 37 | + { code: '<img alt="picture of me taking a photo of an image" aria-hidden />', parserOptions }, |
| 38 | + { code: '<img aria-hidden alt="photo of image" />', parserOptions }, |
| 39 | + { code: '<img ALt="foo" />;', parserOptions }, |
| 40 | + { code: '<img {...this.props} alt="foo" />', parserOptions }, |
| 41 | + { code: '<a />', parserOptions }, |
| 42 | + { code: '<img />', parserOptions }, |
| 43 | + { code: '<img aria-hidden={false} alt="Doing cool things." />', parserOptions } |
| 44 | + ], |
| 45 | + invalid: [ |
| 46 | + { code: '<img alt="Photo of friend." />;', errors: [ expectedError ], parserOptions }, |
| 47 | + { code: '<img alt="Picture of friend." />;', errors: [ expectedError ], parserOptions }, |
| 48 | + { code: '<img alt="Image of friend." />;', errors: [ expectedError ], parserOptions }, |
| 49 | + { code: '<img alt="PhOtO of friend." />;', errors: [ expectedError ], parserOptions }, |
| 50 | + { code: '<img alt="piCTUre of friend." />;', errors: [ expectedError ], parserOptions }, |
| 51 | + { code: '<img alt="imAGE of friend." />;', errors: [ expectedError ], parserOptions }, |
| 52 | + { code: '<img alt="photo of cool person" aria-hidden={false} />', errors: [ expectedError ], parserOptions }, |
| 53 | + { code: '<img alt="picture of cool person" aria-hidden={false} />', errors: [ expectedError ], parserOptions }, |
| 54 | + { code: '<img alt="image of cool person" aria-hidden={false} />', errors: [ expectedError ], parserOptions }, |
| 55 | + { code: '<img alt="photo" {...this.props} />', errors: [ expectedError ], parserOptions }, |
| 56 | + { code: '<img alt="image" {...this.props} />', errors: [ expectedError ], parserOptions }, |
| 57 | + { code: '<img alt="picture" {...this.props} />', errors: [ expectedError ], parserOptions } |
| 58 | + ] |
| 59 | +}); |
0 commit comments