|
| 1 | +/** |
| 2 | + * @fileoverview Prevent usage of button without an explicit type attribute |
| 3 | + * @author Jonathan Santerre |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +// ------------------------------------------------------------------------------ |
| 8 | +// Requirements |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | + |
| 11 | +var rule = require('../../../lib/rules/html-button-has-type') |
| 12 | + |
| 13 | +var RuleTester = require('eslint').RuleTester |
| 14 | + |
| 15 | +// ------------------------------------------------------------------------------ |
| 16 | +// Tests |
| 17 | +// ------------------------------------------------------------------------------ |
| 18 | + |
| 19 | +var ruleTester = new RuleTester({ |
| 20 | + parser: require.resolve('vue-eslint-parser'), |
| 21 | + parserOptions: { ecmaVersion: 2015 } |
| 22 | +}) |
| 23 | +ruleTester.run('html-button-has-type', rule, { |
| 24 | + |
| 25 | + valid: [ |
| 26 | + { |
| 27 | + filename: 'test.vue', |
| 28 | + code: '<template><button type="button">Hello World</button></template>' |
| 29 | + }, |
| 30 | + { |
| 31 | + filename: 'test.vue', |
| 32 | + code: '<template><button type="submit">Hello World</button></template>' |
| 33 | + }, |
| 34 | + { |
| 35 | + filename: 'test.vue', |
| 36 | + code: '<template><button type="reset">Hello World</button></template>' |
| 37 | + }, |
| 38 | + { |
| 39 | + filename: 'test.vue', |
| 40 | + code: '<template><slot><button type="button">Hello World</button></slot></template>' |
| 41 | + }, |
| 42 | + { |
| 43 | + filename: 'test.vue', |
| 44 | + code: `<template> |
| 45 | + <button type="button">Hello World</button> |
| 46 | + <button type="submit">Hello World</button> |
| 47 | + <button type="reset">Hello World</button> |
| 48 | + </template>` |
| 49 | + }, |
| 50 | + { |
| 51 | + filename: 'test.vue', |
| 52 | + code: `<template><button :type="buttonType">Hello World</button></template>` |
| 53 | + }, |
| 54 | + { |
| 55 | + filename: 'test.vue', |
| 56 | + code: '' |
| 57 | + } |
| 58 | + ], |
| 59 | + |
| 60 | + invalid: [ |
| 61 | + { |
| 62 | + filename: 'test.vue', |
| 63 | + code: '<template><button>Hello World</button></template>', |
| 64 | + errors: [{ |
| 65 | + message: 'Missing an explicit type attribute for button.' |
| 66 | + }] |
| 67 | + }, |
| 68 | + { |
| 69 | + filename: 'test.vue', |
| 70 | + code: '<template><button type="">Hello World</button></template>', |
| 71 | + errors: [{ |
| 72 | + message: 'A value must be set for button type attribute.' |
| 73 | + }] |
| 74 | + }, |
| 75 | + { |
| 76 | + filename: 'test.vue', |
| 77 | + code: '<template><button type="foo">Hello World</button></template>', |
| 78 | + errors: [{ |
| 79 | + message: 'foo is an invalid value for button type attribute.' |
| 80 | + }] |
| 81 | + }, |
| 82 | + { |
| 83 | + filename: 'test.vue', |
| 84 | + options: [{ button: false }], |
| 85 | + code: '<template><button type="button">Hello World</button></template>', |
| 86 | + errors: [{ |
| 87 | + message: 'button is a forbidden value for button type attribute.' |
| 88 | + }] |
| 89 | + }, |
| 90 | + { |
| 91 | + filename: 'test.vue', |
| 92 | + options: [{ submit: false }], |
| 93 | + code: '<template><button type="submit">Hello World</button></template>', |
| 94 | + errors: [{ |
| 95 | + message: 'submit is a forbidden value for button type attribute.' |
| 96 | + }] |
| 97 | + }, |
| 98 | + { |
| 99 | + filename: 'test.vue', |
| 100 | + options: [{ reset: false }], |
| 101 | + code: '<template><button type="reset">Hello World</button></template>', |
| 102 | + errors: [{ |
| 103 | + message: 'reset is a forbidden value for button type attribute.' |
| 104 | + }] |
| 105 | + }, |
| 106 | + { |
| 107 | + filename: 'test.vue', |
| 108 | + options: [{ button: false, submit: false, reset: false }], |
| 109 | + code: `<template> |
| 110 | + <button type="button">Hello World</button> |
| 111 | + <button type="submit">Hello World</button> |
| 112 | + <button type="reset">Hello World</button> |
| 113 | + </template>`, |
| 114 | + errors: [ |
| 115 | + { |
| 116 | + message: 'button is a forbidden value for button type attribute.' |
| 117 | + }, |
| 118 | + { |
| 119 | + message: 'submit is a forbidden value for button type attribute.' |
| 120 | + }, |
| 121 | + { |
| 122 | + message: 'reset is a forbidden value for button type attribute.' |
| 123 | + } |
| 124 | + ] |
| 125 | + }, |
| 126 | + { |
| 127 | + filename: 'test.vue', |
| 128 | + options: [{ button: true, submit: true, reset: false }], |
| 129 | + code: `<template> |
| 130 | + <button type="button">Hello World</button> |
| 131 | + <button type="submit">Hello World</button> |
| 132 | + <button type="reset">Hello World</button> |
| 133 | + <button type="">Hello World</button> |
| 134 | + <button type="foo">Hello World</button> |
| 135 | + </template>`, |
| 136 | + errors: [ |
| 137 | + { |
| 138 | + message: 'reset is a forbidden value for button type attribute.' |
| 139 | + }, |
| 140 | + { |
| 141 | + message: 'A value must be set for button type attribute.' |
| 142 | + }, |
| 143 | + { |
| 144 | + message: 'foo is an invalid value for button type attribute.' |
| 145 | + } |
| 146 | + ] |
| 147 | + }, |
| 148 | + { |
| 149 | + filename: 'test.vue', |
| 150 | + code: '<template><button>Hello World</button><button>Hello World</button></template>', |
| 151 | + errors: [ |
| 152 | + { |
| 153 | + message: 'Missing an explicit type attribute for button.' |
| 154 | + }, |
| 155 | + { |
| 156 | + message: 'Missing an explicit type attribute for button.' |
| 157 | + } |
| 158 | + ] |
| 159 | + }, |
| 160 | + { |
| 161 | + filename: 'test.vue', |
| 162 | + code: `<template><button :type="">Hello World</button></template>`, |
| 163 | + errors: [ |
| 164 | + { |
| 165 | + message: 'A value must be set for button type attribute.' |
| 166 | + } |
| 167 | + ] |
| 168 | + } |
| 169 | + ] |
| 170 | +}) |
0 commit comments