|
| 1 | +const rule = require('../../../lib/rules/template-no-action-on-submit-button'); |
| 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 | + |
| 9 | +const ERROR_MESSAGE = |
| 10 | + 'In a `<form>`, a `<button>` with `type="submit"` should have no click action'; |
| 11 | + |
| 12 | +ruleTester.run('template-no-action-on-submit-button', rule, { |
| 13 | + valid: [ |
| 14 | + '<template>button</template>', |
| 15 | + '<template><form><button type="button" /></form></template>', |
| 16 | + '<template><form><button type="button" {{action this.handleClick}} /></form></template>', |
| 17 | + '<template><form><button type="button" {{action this.handleClick on="click"}} /></form></template>', |
| 18 | + '<template><form><button type="button" {{action this.handleMouseover on="mouseOver"}} /></form></template>', |
| 19 | + '<template><form><button type="button" {{on "click" this.handleClick}} /></form></template>', |
| 20 | + '<template><form><button type="button" {{on "mouseover" this.handleMouseover}} /></form></template>', |
| 21 | + '<template>submit</template>', |
| 22 | + '<template><form><button /></form></template>', |
| 23 | + '<template><form><button type="submit" /></form></template>', |
| 24 | + '<template><form><button type="submit" {{action this.handleMouseover on="mouseOver"}} /></form></template>', |
| 25 | + '<template><form><button type="submit" {{on "mouseover" this.handleMouseover}} /></form></template>', |
| 26 | + '<template><form><div/></form></template>', |
| 27 | + '<template><form><div></div></form></template>', |
| 28 | + '<template><form><div type="submit"></div></form></template>', |
| 29 | + '<template><form><div type="submit" {{action this.handleClick}}></div></form></template>', |
| 30 | + '<template><form><div type="submit" {{on "click" this.handleClick}}></div></form></template>', |
| 31 | + // Outside a form — valid |
| 32 | + '<template><button {{action this.handleClick}} /></template>', |
| 33 | + '<template><button {{action this.handleClick on="click"}}/></template>', |
| 34 | + '<template><button {{on "click" this.handleClick}} /></template>', |
| 35 | + '<template><button type="submit" {{action this.handleClick}} /></template>', |
| 36 | + '<template><button type="submit" {{action this.handleClick on="click"}} /></template>', |
| 37 | + '<template><button type="submit" {{action (fn this.someAction "foo")}} /></template>', |
| 38 | + '<template><button type="submit" {{on "click" this.handleClick}} /></template>', |
| 39 | + '<template><button type="submit" {{on "click" (fn this.addNumber 123)}} /></template>', |
| 40 | + ], |
| 41 | + |
| 42 | + invalid: [ |
| 43 | + { |
| 44 | + code: '<template><form><button {{action this.handleClick}} /></form></template>', |
| 45 | + output: null, |
| 46 | + errors: [{ message: ERROR_MESSAGE }], |
| 47 | + }, |
| 48 | + { |
| 49 | + code: '<template><form><button {{action this.handleClick on="click"}}/></form></template>', |
| 50 | + output: null, |
| 51 | + errors: [{ message: ERROR_MESSAGE }], |
| 52 | + }, |
| 53 | + { |
| 54 | + code: '<template><form><button {{on "click" this.handleClick}} /></form></template>', |
| 55 | + output: null, |
| 56 | + errors: [{ message: ERROR_MESSAGE }], |
| 57 | + }, |
| 58 | + { |
| 59 | + code: '<template><form><button type="submit" {{action this.handleClick}} /></form></template>', |
| 60 | + output: null, |
| 61 | + errors: [{ message: ERROR_MESSAGE }], |
| 62 | + }, |
| 63 | + { |
| 64 | + code: '<template><form><button type="submit" {{action this.handleClick on="click"}} /></form></template>', |
| 65 | + output: null, |
| 66 | + errors: [{ message: ERROR_MESSAGE }], |
| 67 | + }, |
| 68 | + { |
| 69 | + code: '<template><form><button type="submit" {{action (fn this.someAction "foo")}} /></form></template>', |
| 70 | + output: null, |
| 71 | + errors: [{ message: ERROR_MESSAGE }], |
| 72 | + }, |
| 73 | + { |
| 74 | + code: '<template><form><button type="submit" {{on "click" this.handleClick}} /></form></template>', |
| 75 | + output: null, |
| 76 | + errors: [{ message: ERROR_MESSAGE }], |
| 77 | + }, |
| 78 | + { |
| 79 | + code: '<template><form><button type="submit" {{on "click" (fn this.addNumber 123)}} /></form></template>', |
| 80 | + output: null, |
| 81 | + errors: [{ message: ERROR_MESSAGE }], |
| 82 | + }, |
| 83 | + { |
| 84 | + code: '<template><form><div><button type="submit" {{action this.handleClick}} /></div></form></template>', |
| 85 | + output: null, |
| 86 | + errors: [{ message: ERROR_MESSAGE }], |
| 87 | + }, |
| 88 | + ], |
| 89 | +}); |
| 90 | + |
| 91 | +const hbsRuleTester = new RuleTester({ |
| 92 | + parser: require.resolve('ember-eslint-parser/hbs'), |
| 93 | + parserOptions: { ecmaVersion: 2022, sourceType: 'module' }, |
| 94 | +}); |
| 95 | + |
| 96 | +hbsRuleTester.run('template-no-action-on-submit-button (hbs)', rule, { |
| 97 | + valid: [ |
| 98 | + '<form><button type="button" /></form>', |
| 99 | + '<form><button type="button" {{action this.handleClick}} /></form>', |
| 100 | + '<form><button type="button" {{action this.handleClick on="click"}} /></form>', |
| 101 | + '<form><button type="button" {{action this.handleMouseover on="mouseOver"}} /></form>', |
| 102 | + '<form><button type="button" {{on "click" this.handleClick}} /></form>', |
| 103 | + '<form><button type="button" {{on "mouseover" this.handleMouseover}} /></form>', |
| 104 | + '<form><button /></form>', |
| 105 | + '<form><button type="submit" /></form>', |
| 106 | + '<form><button type="submit" {{action this.handleMouseover on="mouseOver"}} /></form>', |
| 107 | + '<form><button type="submit" {{on "mouseover" this.handleMouseover}} /></form>', |
| 108 | + '<form><div/></form>', |
| 109 | + '<form><div></div></form>', |
| 110 | + '<form><div type="submit"></div></form>', |
| 111 | + '<form><div type="submit" {{action this.handleClick}}></div></form>', |
| 112 | + '<form><div type="submit" {{on "click" this.handleClick}}></div></form>', |
| 113 | + // Outside a form — valid |
| 114 | + '<button {{action this.handleClick}} />', |
| 115 | + '<button {{action this.handleClick on="click"}}/>', |
| 116 | + '<button {{on "click" this.handleClick}} />', |
| 117 | + '<button type="submit" {{action this.handleClick}} />', |
| 118 | + '<button type="submit" {{action this.handleClick on="click"}} />', |
| 119 | + '<button type="submit" {{action (fn this.someAction "foo")}} />', |
| 120 | + '<button type="submit" {{on "click" this.handleClick}} />', |
| 121 | + '<button type="submit" {{on "click" (fn this.addNumber 123)}} />', |
| 122 | + ], |
| 123 | + invalid: [ |
| 124 | + { |
| 125 | + code: '<form><button {{action this.handleClick}} /></form>', |
| 126 | + output: null, |
| 127 | + errors: [{ message: ERROR_MESSAGE }], |
| 128 | + }, |
| 129 | + { |
| 130 | + code: '<form><button {{action this.handleClick on="click"}}/></form>', |
| 131 | + output: null, |
| 132 | + errors: [{ message: ERROR_MESSAGE }], |
| 133 | + }, |
| 134 | + { |
| 135 | + code: '<form><button {{on "click" this.handleClick}} /></form>', |
| 136 | + output: null, |
| 137 | + errors: [{ message: ERROR_MESSAGE }], |
| 138 | + }, |
| 139 | + { |
| 140 | + code: '<form><button type="submit" {{action this.handleClick}} /></form>', |
| 141 | + output: null, |
| 142 | + errors: [{ message: ERROR_MESSAGE }], |
| 143 | + }, |
| 144 | + { |
| 145 | + code: '<form><button type="submit" {{action this.handleClick on="click"}} /></form>', |
| 146 | + output: null, |
| 147 | + errors: [{ message: ERROR_MESSAGE }], |
| 148 | + }, |
| 149 | + { |
| 150 | + code: '<form><button type="submit" {{action (fn this.someAction "foo")}} /></form>', |
| 151 | + output: null, |
| 152 | + errors: [{ message: ERROR_MESSAGE }], |
| 153 | + }, |
| 154 | + { |
| 155 | + code: '<form><button type="submit" {{on "click" this.handleClick}} /></form>', |
| 156 | + output: null, |
| 157 | + errors: [{ message: ERROR_MESSAGE }], |
| 158 | + }, |
| 159 | + { |
| 160 | + code: '<form><button type="submit" {{on "click" (fn this.addNumber 123)}} /></form>', |
| 161 | + output: null, |
| 162 | + errors: [{ message: ERROR_MESSAGE }], |
| 163 | + }, |
| 164 | + { |
| 165 | + code: '<form><div><button type="submit" {{action this.handleClick}} /></div></form>', |
| 166 | + output: null, |
| 167 | + errors: [{ message: ERROR_MESSAGE }], |
| 168 | + }, |
| 169 | + ], |
| 170 | +}); |
0 commit comments