|
| 1 | +import { describe, it, expect, vi, afterEach } from "vitest"; |
| 2 | +import type { ChartType, Theme } from "../../src/charts/types.js"; |
| 3 | +import { generateChartData } from "../../src/charts/generate.js"; |
| 4 | +import { createDonutSegments } from "../../src/charts/geometry.js"; |
| 5 | +import { createLegend } from "../../src/charts/legend.js"; |
| 6 | + |
| 7 | +vi.mock("../../src/charts/geometry.js", () => ({ |
| 8 | + createDonutSegments: vi.fn(() => `<path d="mockSegment"/>`) |
| 9 | +})); |
| 10 | + |
| 11 | +vi.mock("../../src/charts/legend.js", () => ({ |
| 12 | + createLegend: vi.fn(() => "<rect/><text>mockLegend</text>") |
| 13 | +})); |
| 14 | + |
| 15 | +const mockCreateDonutSegments = vi.mocked(createDonutSegments); |
| 16 | +const mockCreateLegend = vi.mocked(createLegend); |
| 17 | + |
| 18 | +const theme: Theme = { colours: ["#f00", "#0f0"], text: "#333", bg: "#fff", gap: "#000" }; |
| 19 | + |
| 20 | +describe("generateChartData", () => { |
| 21 | + afterEach(() => { |
| 22 | + vi.clearAllMocks(); |
| 23 | + }); |
| 24 | + |
| 25 | + ["donut" as ChartType, "pie" as ChartType].forEach((type) => { |
| 26 | + describe(`${type} context`, () => { |
| 27 | + it("returns segments, legend, and content dimensions", () => { |
| 28 | + const langs = [{ lang: "JS", pct: 100 }]; |
| 29 | + const result = generateChartData(langs, theme, type, "gap", false); |
| 30 | + expect(result).toHaveProperty("segments"); |
| 31 | + expect(result).toHaveProperty("legend"); |
| 32 | + expect(result).toHaveProperty("contentWidth"); |
| 33 | + expect(result).toHaveProperty("contentHeight"); |
| 34 | + expect(mockCreateDonutSegments).toHaveBeenCalled(); |
| 35 | + expect(mockCreateLegend).toHaveBeenCalled(); |
| 36 | + }); |
| 37 | + |
| 38 | + it("computes chartX and legendStartX as numbers", () => { |
| 39 | + const langs = [{ lang: "Python", pct: 100 }]; |
| 40 | + generateChartData(langs, theme, type, "gap", false); |
| 41 | + const segmentCall = mockCreateDonutSegments.mock.calls.at(-1); |
| 42 | + const legendCall = mockCreateLegend.mock.calls.at(-1) ?? []; |
| 43 | + expect(typeof segmentCall![1]).toBe("number"); |
| 44 | + expect(typeof legendCall[2]).toBe("number"); |
| 45 | + expect(typeof legendCall[4]).toBe("number"); |
| 46 | + }); |
| 47 | + |
| 48 | + it("passes theme to both segments and legend", () => { |
| 49 | + const langs = [{ lang: "HTML", pct: 100 }]; |
| 50 | + generateChartData(langs, theme, type, "gap", false); |
| 51 | + const segmentsCall = mockCreateDonutSegments.mock.calls.at(-1)!; |
| 52 | + const legendCall = mockCreateLegend.mock.calls.at(-1) ?? []; |
| 53 | + expect(segmentsCall[3]).toEqual(theme.colours); |
| 54 | + expect(legendCall[1]).toBe(theme); |
| 55 | + }); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe("donut", () => { |
| 60 | + it("falls back to donut geometry for unrecognized chartType", () => { |
| 61 | + const langs = [{ lang: "JS", pct: 100 }]; |
| 62 | + generateChartData(langs, theme, "bigbadwolf" as ChartType, "gap", false); |
| 63 | + const call = mockCreateDonutSegments.mock.calls.at(-1)!; |
| 64 | + expect(call[2].INNER_RADIUS).not.toBe(0); |
| 65 | + }); |
| 66 | + |
| 67 | + it("passes non-zero INNER_RADIUS for donut", () => { |
| 68 | + const langs = [{ lang: "HTML", pct: 100 }]; |
| 69 | + generateChartData(langs, theme, "donut", "gap", false); |
| 70 | + const call = mockCreateDonutSegments.mock.calls.at(-1)!; |
| 71 | + const geometry = call[2]; |
| 72 | + expect(geometry.INNER_RADIUS).not.toBe(0); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + describe("pie", () => { |
| 77 | + it("passes INNER_RADIUS: 0 for filled pie", () => { |
| 78 | + const langs = [{ lang: "Python", pct: 100 }]; |
| 79 | + generateChartData(langs, theme, "pie", "gap", false); |
| 80 | + const call = mockCreateDonutSegments.mock.calls.at(-1)!; |
| 81 | + const geometry = call[2]; |
| 82 | + expect(geometry.INNER_RADIUS).toBe(0); |
| 83 | + }); |
| 84 | + }); |
| 85 | +}); |
0 commit comments