|
| 1 | +// @vitest-environment jsdom |
| 2 | + |
| 3 | +import { screen } from "@testing-library/react"; |
| 4 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | + |
| 6 | +import { renderWithProviders } from "test-utils/render-with-providers"; |
| 7 | +import { findServiceBlockByLabel } from "test-utils/widget-assertions"; |
| 8 | + |
| 9 | +const { useWidgetAPI } = vi.hoisted(() => ({ useWidgetAPI: vi.fn() })); |
| 10 | +vi.mock("utils/proxy/use-widget-api", () => ({ default: useWidgetAPI })); |
| 11 | + |
| 12 | +import Component from "./component"; |
| 13 | + |
| 14 | +function expectBlockValue(container, label, value) { |
| 15 | + const block = findServiceBlockByLabel(container, label); |
| 16 | + expect(block, `missing block for ${label}`).toBeTruthy(); |
| 17 | + expect(block.textContent).toContain(String(value)); |
| 18 | +} |
| 19 | + |
| 20 | +describe("widgets/gitea/component", () => { |
| 21 | + beforeEach(() => { |
| 22 | + vi.clearAllMocks(); |
| 23 | + }); |
| 24 | + |
| 25 | + it("renders placeholders while loading", () => { |
| 26 | + useWidgetAPI |
| 27 | + .mockReturnValueOnce({ data: undefined, error: undefined }) // notifications |
| 28 | + .mockReturnValueOnce({ data: undefined, error: undefined }) // issues |
| 29 | + .mockReturnValueOnce({ data: undefined, error: undefined }); // repositories |
| 30 | + |
| 31 | + const { container } = renderWithProviders(<Component service={{ widget: { type: "gitea", url: "http://x" } }} />, { |
| 32 | + settings: { hideErrors: false }, |
| 33 | + }); |
| 34 | + |
| 35 | + expect(container.querySelectorAll(".service-block")).toHaveLength(4); |
| 36 | + expect(screen.getByText("gitea.notifications")).toBeInTheDocument(); |
| 37 | + expect(screen.getByText("gitea.issues")).toBeInTheDocument(); |
| 38 | + expect(screen.getByText("gitea.pulls")).toBeInTheDocument(); |
| 39 | + expect(screen.getByText("gitea.repositories")).toBeInTheDocument(); |
| 40 | + expect(screen.getAllByText("-")).toHaveLength(4); |
| 41 | + }); |
| 42 | + |
| 43 | + it("renders error UI when any endpoint errors", () => { |
| 44 | + useWidgetAPI |
| 45 | + .mockReturnValueOnce({ data: undefined, error: undefined }) |
| 46 | + .mockReturnValueOnce({ data: undefined, error: { message: "nope" } }) |
| 47 | + .mockReturnValueOnce({ data: undefined, error: undefined }); |
| 48 | + |
| 49 | + renderWithProviders(<Component service={{ widget: { type: "gitea", url: "http://x" } }} />, { |
| 50 | + settings: { hideErrors: false }, |
| 51 | + }); |
| 52 | + |
| 53 | + expect(screen.getAllByText(/widget\.api_error/i).length).toBeGreaterThan(0); |
| 54 | + expect(screen.getByText("nope")).toBeInTheDocument(); |
| 55 | + }); |
| 56 | + |
| 57 | + it("renders computed counts when loaded", () => { |
| 58 | + useWidgetAPI |
| 59 | + .mockReturnValueOnce({ data: [{ id: 1 }, { id: 2 }], error: undefined }) |
| 60 | + .mockReturnValueOnce({ |
| 61 | + data: { issues: [{ id: 1 }], pulls: [{ id: 1 }, { id: 2 }, { id: 3 }] }, |
| 62 | + error: undefined, |
| 63 | + }) |
| 64 | + .mockReturnValueOnce({ data: { data: [{ id: 1 }] }, error: undefined }); |
| 65 | + |
| 66 | + const { container } = renderWithProviders(<Component service={{ widget: { type: "gitea", url: "http://x" } }} />, { |
| 67 | + settings: { hideErrors: false }, |
| 68 | + }); |
| 69 | + |
| 70 | + expectBlockValue(container, "gitea.notifications", 2); |
| 71 | + expectBlockValue(container, "gitea.issues", 1); |
| 72 | + expectBlockValue(container, "gitea.pulls", 3); |
| 73 | + expectBlockValue(container, "gitea.repositories", 1); |
| 74 | + }); |
| 75 | +}); |
0 commit comments