|
| 1 | +const rule = require('../../../lib/rules/template-no-unsupported-role-attributes'); |
| 2 | +const RuleTester = require('eslint').RuleTester; |
| 3 | + |
| 4 | +const validHbs = [ |
| 5 | + '<div role="button" aria-disabled="true"></div>', |
| 6 | + '<div role="heading" aria-level="1" />', |
| 7 | + '<span role="checkbox" aria-checked={{this.checked}}></span>', |
| 8 | + '<CustomComponent role="banner" />', |
| 9 | + '<div role="textbox" aria-required={{this.required}} aria-errormessage={{this.error}}></div>', |
| 10 | + '<div role="heading" foo="true" />', |
| 11 | + '<dialog />', |
| 12 | + '<a href="#" aria-describedby=""></a>', |
| 13 | + '<menu type="toolbar" aria-hidden="true" />', |
| 14 | + '<a role="menuitem" aria-labelledby={{this.label}} />', |
| 15 | + '<input type="image" aria-atomic />', |
| 16 | + '<input type="submit" aria-disabled="true" />', |
| 17 | + '<select aria-expanded="false" aria-controls="ctrlID" />', |
| 18 | + '<div type="button" foo="true" />', |
| 19 | + '{{some-component role="heading" aria-level="2"}}', |
| 20 | + '{{other-component role=this.role aria-bogus="true"}}', |
| 21 | + '<ItemCheckbox @model={{@model}} @checkable={{@checkable}} />', |
| 22 | + '<some-custom-element />', |
| 23 | + '<input type="password">', |
| 24 | +]; |
| 25 | + |
| 26 | +const invalidHbs = [ |
| 27 | + { |
| 28 | + code: '<div role="link" href="#" aria-checked />', |
| 29 | + output: '<div role="link" href="#" />', |
| 30 | + errors: [{ message: 'The attribute aria-checked is not supported by the role link' }], |
| 31 | + }, |
| 32 | + { |
| 33 | + code: '<CustomComponent role="listbox" aria-level="2" />', |
| 34 | + output: '<CustomComponent role="listbox" />', |
| 35 | + errors: [{ message: 'The attribute aria-level is not supported by the role listbox' }], |
| 36 | + }, |
| 37 | + { |
| 38 | + code: '<div role="option" aria-notreal="bogus" aria-selected="false" />', |
| 39 | + output: '<div role="option" aria-selected="false" />', |
| 40 | + errors: [{ message: 'The attribute aria-notreal is not supported by the role option' }], |
| 41 | + }, |
| 42 | + { |
| 43 | + code: '<div role="combobox" aria-multiline="true" aria-expanded="false" aria-controls="someId" />', |
| 44 | + output: '<div role="combobox" aria-expanded="false" aria-controls="someId" />', |
| 45 | + errors: [{ message: 'The attribute aria-multiline is not supported by the role combobox' }], |
| 46 | + }, |
| 47 | + { |
| 48 | + code: '<button type="submit" aria-valuetext="woosh"></button>', |
| 49 | + output: '<button type="submit"></button>', |
| 50 | + errors: [ |
| 51 | + { |
| 52 | + message: |
| 53 | + 'The attribute aria-valuetext is not supported by the element button with the implicit role of button', |
| 54 | + }, |
| 55 | + ], |
| 56 | + }, |
| 57 | + { |
| 58 | + code: '<menu type="toolbar" aria-expanded="true" />', |
| 59 | + output: '<menu type="toolbar" />', |
| 60 | + errors: [ |
| 61 | + { |
| 62 | + message: |
| 63 | + 'The attribute aria-expanded is not supported by the element menu with the implicit role of list', |
| 64 | + }, |
| 65 | + ], |
| 66 | + }, |
| 67 | + { |
| 68 | + code: '<a role="menuitem" aria-checked={{this.checked}} />', |
| 69 | + output: '<a role="menuitem" />', |
| 70 | + errors: [{ message: 'The attribute aria-checked is not supported by the role menuitem' }], |
| 71 | + }, |
| 72 | + { |
| 73 | + code: '<input type="button" aria-invalid="grammar" />', |
| 74 | + output: '<input type="button" />', |
| 75 | + errors: [ |
| 76 | + { |
| 77 | + message: |
| 78 | + 'The attribute aria-invalid is not supported by the element input with the implicit role of button', |
| 79 | + }, |
| 80 | + ], |
| 81 | + }, |
| 82 | + { |
| 83 | + code: '<input type="email" aria-level={{this.level}} />', |
| 84 | + output: '<input type="email" />', |
| 85 | + errors: [ |
| 86 | + { |
| 87 | + message: |
| 88 | + 'The attribute aria-level is not supported by the element input with the implicit role of combobox', |
| 89 | + }, |
| 90 | + ], |
| 91 | + }, |
| 92 | + { |
| 93 | + code: '{{foo-component role="button" aria-valuetext="blahblahblah"}}', |
| 94 | + output: '{{foo-component role="button"}}', |
| 95 | + errors: [{ message: 'The attribute aria-valuetext is not supported by the role button' }], |
| 96 | + }, |
| 97 | +]; |
| 98 | + |
| 99 | +function wrapTemplate(entry) { |
| 100 | + if (typeof entry === 'string') { |
| 101 | + return `<template>${entry}</template>`; |
| 102 | + } |
| 103 | + |
| 104 | + return { |
| 105 | + ...entry, |
| 106 | + code: `<template>${entry.code}</template>`, |
| 107 | + output: entry.output ? `<template>${entry.output}</template>` : entry.output, |
| 108 | + }; |
| 109 | +} |
| 110 | + |
| 111 | +const gjsRuleTester = new RuleTester({ |
| 112 | + parser: require.resolve('ember-eslint-parser'), |
| 113 | + parserOptions: { ecmaVersion: 2022, sourceType: 'module' }, |
| 114 | +}); |
| 115 | + |
| 116 | +gjsRuleTester.run('template-no-unsupported-role-attributes', rule, { |
| 117 | + valid: validHbs.map(wrapTemplate), |
| 118 | + invalid: invalidHbs.map(wrapTemplate), |
| 119 | +}); |
| 120 | + |
| 121 | +const hbsRuleTester = new RuleTester({ |
| 122 | + parser: require.resolve('ember-eslint-parser/hbs'), |
| 123 | + parserOptions: { |
| 124 | + ecmaVersion: 2022, |
| 125 | + sourceType: 'module', |
| 126 | + }, |
| 127 | +}); |
| 128 | + |
| 129 | +hbsRuleTester.run('template-no-unsupported-role-attributes', rule, { |
| 130 | + valid: validHbs, |
| 131 | + invalid: invalidHbs, |
| 132 | +}); |
0 commit comments