|
| 1 | +import { expect } from "@std/expect"; |
| 2 | +import { testPlugin } from "../test-utils.ts"; |
| 3 | +import * as testRule from "./server-event-handlers.ts"; |
| 4 | + |
| 5 | +const okCases = new Set<[filename: string, code: string]>([ |
| 6 | + ["file:///foo.jsx", "<Foo onClick={() => {}} />"], |
| 7 | + ["file:///foo.jsx", "<button onClick={() => {}} />"], |
| 8 | + ["file:///foo.jsx", "<button onClick={function () {}} />"], |
| 9 | + ["file:///foo.jsx", "<button onclick={function () {}} />"], |
| 10 | + ["file:///foo.jsx", "<button onClick=\"console.log('hey')\" />"], |
| 11 | + ["file:///foo.jsx", '<button online="foo" />'], |
| 12 | + ["file:///foo.jsx", "<x-foo onClick=\"console.log('hey')\" />"], |
| 13 | + [ |
| 14 | + "file:///routes/foo/(_islands)/foo.jsx", |
| 15 | + "<button onClick={function () {}} />", |
| 16 | + ], |
| 17 | +]); |
| 18 | + |
| 19 | +const errCases = new Set<[file: string, code: string, range: [number, number]]>( |
| 20 | + [ |
| 21 | + ["file:///routes/index.tsx", "<button onClick={() => {}} />", [8, 26]], |
| 22 | + ["file:///routes/index.tsx", "<button onTouchMove={() => {}} />", [8, 30]], |
| 23 | + [ |
| 24 | + "file:///routes/index.tsx", |
| 25 | + `<button onTouchMove={"console.log('hey')"} />`, |
| 26 | + [8, 42], |
| 27 | + ], |
| 28 | + ["file:///routes/index.tsx", "<foo-button foo={() => {}} />", [12, 26]], |
| 29 | + ["file:///routes/index.tsx", "<foo-button foo={function () {}} />", [ |
| 30 | + 12, |
| 31 | + 32, |
| 32 | + ]], |
| 33 | + ], |
| 34 | +); |
| 35 | + |
| 36 | +Deno.test("fresh/server-event-handlers - ok", () => { |
| 37 | + for (const [file, code] of okCases) { |
| 38 | + const diagnostics = Deno.lint.runPlugin(testPlugin(testRule), file, code); |
| 39 | + |
| 40 | + expect(diagnostics.length).toBe(0); |
| 41 | + } |
| 42 | +}); |
| 43 | + |
| 44 | +Deno.test("fresh/server-event-handlers - err", () => { |
| 45 | + for (const [file, code, range] of errCases) { |
| 46 | + const [d, ...rest] = Deno.lint.runPlugin(testPlugin(testRule), file, code); |
| 47 | + |
| 48 | + expect(rest.length).toBe(0); |
| 49 | + expect(d.fix).toEqual([]); |
| 50 | + expect(d.range).toEqual(range); |
| 51 | + expect(d.id).toBe("fresh/server-event-handlers"); |
| 52 | + expect(d.message).toBe( |
| 53 | + "Server components cannot install client side event handlers.", |
| 54 | + ); |
| 55 | + expect(d.hint).toBe( |
| 56 | + "Remove this property or turn the enclosing component into an island", |
| 57 | + ); |
| 58 | + } |
| 59 | +}); |
0 commit comments