|
| 1 | +const rule = require('../../../lib/rules/template-no-forbidden-elements'); |
| 2 | +const RuleTester = require('eslint').RuleTester; |
| 3 | + |
| 4 | +const ruleTester = new RuleTester({ |
| 5 | + parser: require.resolve('ember-eslint-parser'), |
| 6 | + parserOptions: { ecmaVersion: 2022, sourceType: 'module' }, |
| 7 | +}); |
| 8 | +ruleTester.run('template-no-forbidden-elements', rule, { |
| 9 | + valid: [ |
| 10 | + { code: '<template><div></div></template>', options: [['script']] }, |
| 11 | + // Object config form |
| 12 | + { code: '<template><div></div></template>', options: [{ forbidden: ['script'] }] }, |
| 13 | + { code: '<template><script></script></template>', options: [{ forbidden: ['html'] }] }, |
| 14 | + '<template><header></header></template>', |
| 15 | + '<template><footer></footer></template>', |
| 16 | + '<template><p></p></template>', |
| 17 | + '<template><head><meta charset="utf-8"></head></template>', |
| 18 | + ], |
| 19 | + invalid: [ |
| 20 | + { |
| 21 | + code: '<template><script></script></template>', |
| 22 | + output: null, |
| 23 | + options: [{ forbidden: ['script'] }], |
| 24 | + errors: [{ messageId: 'forbidden' }], |
| 25 | + }, |
| 26 | + { |
| 27 | + code: '<template><script></script></template>', |
| 28 | + output: null, |
| 29 | + options: [['script']], |
| 30 | + errors: [{ messageId: 'forbidden' }], |
| 31 | + }, |
| 32 | + |
| 33 | + { |
| 34 | + code: '<template><html></html></template>', |
| 35 | + output: null, |
| 36 | + errors: [{ messageId: 'forbidden' }], |
| 37 | + }, |
| 38 | + { |
| 39 | + code: '<template><style></style></template>', |
| 40 | + output: null, |
| 41 | + errors: [{ messageId: 'forbidden' }], |
| 42 | + }, |
| 43 | + { |
| 44 | + code: '<template><meta charset="utf-8"></template>', |
| 45 | + output: null, |
| 46 | + errors: [{ messageId: 'forbidden' }], |
| 47 | + }, |
| 48 | + { |
| 49 | + code: '<template><head><html></html></head></template>', |
| 50 | + output: null, |
| 51 | + errors: [{ messageId: 'forbidden' }], |
| 52 | + }, |
| 53 | + { |
| 54 | + code: '<template><Foo /></template>', |
| 55 | + output: null, |
| 56 | + options: [['Foo']], |
| 57 | + errors: [{ messageId: 'forbidden' }], |
| 58 | + }, |
| 59 | + ], |
| 60 | +}); |
| 61 | + |
| 62 | +const hbsRuleTester = new RuleTester({ |
| 63 | + parser: require.resolve('ember-eslint-parser/hbs'), |
| 64 | + parserOptions: { |
| 65 | + ecmaVersion: 2022, |
| 66 | + sourceType: 'module', |
| 67 | + }, |
| 68 | +}); |
| 69 | + |
| 70 | +hbsRuleTester.run('template-no-forbidden-elements', rule, { |
| 71 | + valid: [ |
| 72 | + '<header></header>', |
| 73 | + '<div></div>', |
| 74 | + '<footer></footer>', |
| 75 | + '<p></p>', |
| 76 | + '<head><meta charset="utf-8"></head>', |
| 77 | + // Custom forbidden list (script not included). |
| 78 | + { |
| 79 | + code: '<script></script>', |
| 80 | + options: [['html', 'meta', 'style']], |
| 81 | + }, |
| 82 | + // Object config form. |
| 83 | + { |
| 84 | + code: '<script></script>', |
| 85 | + options: [{ forbidden: ['html', 'meta', 'style'] }], |
| 86 | + }, |
| 87 | + ], |
| 88 | + invalid: [ |
| 89 | + // Default config. |
| 90 | + { |
| 91 | + code: '<script></script>', |
| 92 | + output: null, |
| 93 | + errors: [{ message: 'Use of forbidden element <script>' }], |
| 94 | + }, |
| 95 | + { |
| 96 | + code: '<html></html>', |
| 97 | + output: null, |
| 98 | + errors: [{ message: 'Use of forbidden element <html>' }], |
| 99 | + }, |
| 100 | + { |
| 101 | + code: '<style></style>', |
| 102 | + output: null, |
| 103 | + errors: [{ message: 'Use of forbidden element <style>' }], |
| 104 | + }, |
| 105 | + { |
| 106 | + code: '<meta charset="utf-8">', |
| 107 | + output: null, |
| 108 | + errors: [{ message: 'Use of forbidden element <meta>' }], |
| 109 | + }, |
| 110 | + { |
| 111 | + code: '<head><html></html></head>', |
| 112 | + output: null, |
| 113 | + errors: [{ message: 'Use of forbidden element <html>' }], |
| 114 | + }, |
| 115 | + // Custom forbidden list. |
| 116 | + { |
| 117 | + code: '<div></div>', |
| 118 | + output: null, |
| 119 | + options: [['div']], |
| 120 | + errors: [{ message: 'Use of forbidden element <div>' }], |
| 121 | + }, |
| 122 | + { |
| 123 | + code: '<Foo />', |
| 124 | + output: null, |
| 125 | + options: [['Foo']], |
| 126 | + errors: [{ message: 'Use of forbidden element <Foo>' }], |
| 127 | + }, |
| 128 | + ], |
| 129 | +}); |
0 commit comments