|
| 1 | +import { tsOnlyTest, run } from "../ruleTester"; |
| 2 | +import rule from "../../src/rules/no-array-handlers"; |
| 3 | + |
| 4 | +export const cases = run("no-array-handlers", rule, { |
| 5 | + valid: [ |
| 6 | + `let el = <button style={{background: 'red'}} onClick={() => 9001} />`, |
| 7 | + `const handler = () => 1+1; |
| 8 | + let el = <button onClick={handler} />`, |
| 9 | + `let el = <button onclick={() => 9001} />`, |
| 10 | + `const handler = () => 1+1; |
| 11 | + let el = <button style={{background: 'pink'}} onclick={handler} />`, |
| 12 | + `let el = <button attr:click={[(x) => x, 9001]} />`, |
| 13 | + `let el = <button prop:onClick={[(x) => x, 9001]} />`, |
| 14 | + `let el = <button on:Click={() => 1+1} />`, |
| 15 | + `function Component(props) { |
| 16 | + return <div onClick={props.onClick} />; |
| 17 | + }`, |
| 18 | + `<button onClick={() => [handler, "abc"]} />`, |
| 19 | + `<button onClick={() => [handler, {data:true}]} /> `, |
| 20 | + { |
| 21 | + code: `function Component() { |
| 22 | + return <div onClick={[(n: number) => n*n, 2] as SafeArray<number>} />; |
| 23 | + }`, |
| 24 | + ...tsOnlyTest, |
| 25 | + }, |
| 26 | + ], |
| 27 | + invalid: [ |
| 28 | + { |
| 29 | + code: `let el = <button onClick={[(n) => console.log(n), 'str']} />`, |
| 30 | + errors: [{ messageId: "noArrayHandlers" }], |
| 31 | + }, |
| 32 | + { |
| 33 | + code: `let el = <button onClick={[(k: string) => k.toUpperCase(), 'hello']} />`, |
| 34 | + errors: [{ messageId: "noArrayHandlers" }], |
| 35 | + ...tsOnlyTest, |
| 36 | + }, |
| 37 | + { |
| 38 | + code: `let el = <div onMouseOver={[1,2,3]} />`, |
| 39 | + errors: [{ messageId: "noArrayHandlers" }], |
| 40 | + }, |
| 41 | + { |
| 42 | + code: `let el = <div on:click={[handler, i()]} />`, |
| 43 | + errors: [{ messageId: "noArrayHandlers" }], |
| 44 | + }, |
| 45 | + { |
| 46 | + code: `let el = <button type="button" onclick={[handler, i() + 2]} class="btn" />`, |
| 47 | + errors: [{ messageId: "noArrayHandlers" }], |
| 48 | + }, |
| 49 | + { |
| 50 | + code: `let handler = [(x) => x*2, 54]; |
| 51 | + let el = <button style={{background: 'pink'}} onclick={handler} />`, |
| 52 | + errors: [{ messageId: "noArrayHandlers" }], |
| 53 | + }, |
| 54 | + { |
| 55 | + code: `const thing = (props) => <div onclick={[props.callback, props.id]}><button type="button" onclick={handler} class="btn" /></div>`, |
| 56 | + errors: [{ messageId: "noArrayHandlers" }], |
| 57 | + }, |
| 58 | + { |
| 59 | + code: `function Component() { |
| 60 | + const arr = [(n: number) => n*n, 2]; |
| 61 | + return <div onClick={arr} />; |
| 62 | + }`, |
| 63 | + errors: [{ messageId: "noArrayHandlers" }], |
| 64 | + ...tsOnlyTest, |
| 65 | + }, |
| 66 | + ], |
| 67 | +}); |
0 commit comments