|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import type { Diagnostic, ReactDoctorConfig } from "../src/types.js"; |
| 3 | +import { combineDiagnostics, computeJsxIncludePaths } from "../src/utils/combine-diagnostics.js"; |
| 4 | + |
| 5 | +const createDiagnostic = (overrides: Partial<Diagnostic> = {}): Diagnostic => ({ |
| 6 | + filePath: "src/app.tsx", |
| 7 | + plugin: "react-doctor", |
| 8 | + rule: "test-rule", |
| 9 | + severity: "warning", |
| 10 | + message: "test message", |
| 11 | + help: "test help", |
| 12 | + line: 1, |
| 13 | + column: 1, |
| 14 | + category: "Test", |
| 15 | + ...overrides, |
| 16 | +}); |
| 17 | + |
| 18 | +describe("computeJsxIncludePaths", () => { |
| 19 | + it("returns undefined for empty include paths", () => { |
| 20 | + expect(computeJsxIncludePaths([])).toBeUndefined(); |
| 21 | + }); |
| 22 | + |
| 23 | + it("filters to only JSX/TSX files", () => { |
| 24 | + const paths = ["src/app.tsx", "src/utils.ts", "src/Button.jsx", "src/config.js"]; |
| 25 | + const result = computeJsxIncludePaths(paths); |
| 26 | + expect(result).toEqual(["src/app.tsx", "src/Button.jsx"]); |
| 27 | + }); |
| 28 | + |
| 29 | + it("returns empty array when no JSX/TSX files exist", () => { |
| 30 | + const paths = ["src/utils.ts", "src/config.js"]; |
| 31 | + const result = computeJsxIncludePaths(paths); |
| 32 | + expect(result).toEqual([]); |
| 33 | + }); |
| 34 | +}); |
| 35 | + |
| 36 | +describe("combineDiagnostics", () => { |
| 37 | + it("merges lint and dead code diagnostics", () => { |
| 38 | + const lintDiagnostics = [createDiagnostic({ rule: "lint-rule" })]; |
| 39 | + const deadCodeDiagnostics = [createDiagnostic({ rule: "dead-code-rule" })]; |
| 40 | + |
| 41 | + const result = combineDiagnostics(lintDiagnostics, deadCodeDiagnostics, "/tmp", true, null); |
| 42 | + expect(result).toHaveLength(2); |
| 43 | + expect(result[0].rule).toBe("lint-rule"); |
| 44 | + expect(result[1].rule).toBe("dead-code-rule"); |
| 45 | + }); |
| 46 | + |
| 47 | + it("returns empty array when both inputs are empty in diff mode", () => { |
| 48 | + const result = combineDiagnostics([], [], "/tmp", true, null); |
| 49 | + expect(result).toEqual([]); |
| 50 | + }); |
| 51 | + |
| 52 | + it("applies config filtering when userConfig is provided", () => { |
| 53 | + const diagnostics = [ |
| 54 | + createDiagnostic({ plugin: "react", rule: "no-danger" }), |
| 55 | + createDiagnostic({ plugin: "react-doctor", rule: "no-giant-component" }), |
| 56 | + ]; |
| 57 | + const config: ReactDoctorConfig = { |
| 58 | + ignore: { rules: ["react/no-danger"] }, |
| 59 | + }; |
| 60 | + |
| 61 | + const result = combineDiagnostics(diagnostics, [], "/tmp", true, config); |
| 62 | + expect(result).toHaveLength(1); |
| 63 | + expect(result[0].rule).toBe("no-giant-component"); |
| 64 | + }); |
| 65 | + |
| 66 | + it("skips config filtering when userConfig is null", () => { |
| 67 | + const diagnostics = [createDiagnostic(), createDiagnostic()]; |
| 68 | + const result = combineDiagnostics(diagnostics, [], "/tmp", true, null); |
| 69 | + expect(result).toHaveLength(2); |
| 70 | + }); |
| 71 | +}); |
0 commit comments