From ff5a3acc1c8b8cd3fb2a856597b982dae24b7af9 Mon Sep 17 00:00:00 2001 From: yuneng-jiang Date: Mon, 9 Feb 2026 12:07:53 -0800 Subject: [PATCH] addressing feedback around tests --- .../mcpServers/useMCPAccessGroups.test.ts | 38 ++++++++++++++-- .../hooks/mcpServers/useMCPServers.test.ts | 44 ++++++++++++++++--- 2 files changed, 71 insertions(+), 11 deletions(-) diff --git a/ui/litellm-dashboard/src/app/(dashboard)/hooks/mcpServers/useMCPAccessGroups.test.ts b/ui/litellm-dashboard/src/app/(dashboard)/hooks/mcpServers/useMCPAccessGroups.test.ts index b57f5d182d..9c555ff123 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/hooks/mcpServers/useMCPAccessGroups.test.ts +++ b/ui/litellm-dashboard/src/app/(dashboard)/hooks/mcpServers/useMCPAccessGroups.test.ts @@ -31,6 +31,7 @@ const wrapper = ({ children }: { children: React.ReactNode }) => { return React.createElement(QueryClientProvider, { client: queryClient }, children); }; +const mockAccessToken = "test-token-456"; const mockAccessGroups = ["group-1", "group-2", "group-3"]; describe("useMCPAccessGroups", () => { @@ -38,10 +39,22 @@ describe("useMCPAccessGroups", () => { vi.clearAllMocks(); const useAuthorizedModule = await import("@/app/(dashboard)/hooks/useAuthorized"); vi.mocked(useAuthorizedModule.default).mockReturnValue({ - accessToken: "test-token-456", + accessToken: mockAccessToken, } as any); }); + it("should return hook result without errors", () => { + vi.mocked(networking.fetchMCPAccessGroups).mockResolvedValue([]); + + const { result } = renderHook(() => useMCPAccessGroups(), { wrapper }); + + expect(result.current).toBeDefined(); + expect(result.current).toHaveProperty("data"); + expect(result.current).toHaveProperty("isSuccess"); + expect(result.current).toHaveProperty("isError"); + expect(result.current).toHaveProperty("status"); + }); + it("should return MCP access groups when access token is present", async () => { vi.mocked(networking.fetchMCPAccessGroups).mockResolvedValue(mockAccessGroups); @@ -51,11 +64,11 @@ describe("useMCPAccessGroups", () => { expect(result.current.isSuccess).toBe(true); }); - expect(networking.fetchMCPAccessGroups).toHaveBeenCalledWith("test-token-456"); + expect(networking.fetchMCPAccessGroups).toHaveBeenCalledWith(mockAccessToken); expect(result.current.data).toEqual(mockAccessGroups); }); - it("should not fetch when access token is not available", async () => { + it("should not fetch when access token is null", async () => { const useAuthorizedModule = await import("@/app/(dashboard)/hooks/useAuthorized"); vi.mocked(useAuthorizedModule.default).mockReturnValue({ accessToken: null, @@ -63,7 +76,23 @@ describe("useMCPAccessGroups", () => { const { result } = renderHook(() => useMCPAccessGroups(), { wrapper }); - expect(result.current.status).toBe("pending"); + expect(result.current.isFetching).toBe(false); + expect(result.current.isLoading).toBe(false); + expect(result.current.data).toBeUndefined(); + expect(networking.fetchMCPAccessGroups).not.toHaveBeenCalled(); + }); + + it("should not fetch when access token is empty string", async () => { + const useAuthorizedModule = await import("@/app/(dashboard)/hooks/useAuthorized"); + vi.mocked(useAuthorizedModule.default).mockReturnValue({ + accessToken: "", + } as any); + + const { result } = renderHook(() => useMCPAccessGroups(), { wrapper }); + + expect(result.current.isFetching).toBe(false); + expect(result.current.isLoading).toBe(false); + expect(result.current.data).toBeUndefined(); expect(networking.fetchMCPAccessGroups).not.toHaveBeenCalled(); }); @@ -78,6 +107,7 @@ describe("useMCPAccessGroups", () => { }); expect(result.current.error).toEqual(mockError); + expect(result.current.data).toBeUndefined(); }); it("should return empty array when API returns no groups", async () => { diff --git a/ui/litellm-dashboard/src/app/(dashboard)/hooks/mcpServers/useMCPServers.test.ts b/ui/litellm-dashboard/src/app/(dashboard)/hooks/mcpServers/useMCPServers.test.ts index b30591cf1c..3681ffc747 100644 --- a/ui/litellm-dashboard/src/app/(dashboard)/hooks/mcpServers/useMCPServers.test.ts +++ b/ui/litellm-dashboard/src/app/(dashboard)/hooks/mcpServers/useMCPServers.test.ts @@ -10,7 +10,7 @@ vi.mock("@/components/networking", () => ({ fetchMCPServers: vi.fn(), })); -vi.mock("../useAuthorized", () => ({ +vi.mock("@/app/(dashboard)/hooks/useAuthorized", () => ({ default: vi.fn(() => ({ accessToken: "test-token-123", })), @@ -31,6 +31,7 @@ const wrapper = ({ children }: { children: React.ReactNode }) => { return React.createElement(QueryClientProvider, { client: queryClient }, children); }; +const mockAccessToken = "test-token-123"; const mockServers = [ { server_id: "server-1", @@ -46,12 +47,24 @@ const mockServers = [ describe("useMCPServers", () => { beforeEach(async () => { vi.clearAllMocks(); - const useAuthorizedModule = await import("../useAuthorized"); + const useAuthorizedModule = await import("@/app/(dashboard)/hooks/useAuthorized"); vi.mocked(useAuthorizedModule.default).mockReturnValue({ - accessToken: "test-token-123", + accessToken: mockAccessToken, } as any); }); + it("should return hook result without errors", () => { + vi.mocked(networking.fetchMCPServers).mockResolvedValue([]); + + const { result } = renderHook(() => useMCPServers(), { wrapper }); + + expect(result.current).toBeDefined(); + expect(result.current).toHaveProperty("data"); + expect(result.current).toHaveProperty("isSuccess"); + expect(result.current).toHaveProperty("isError"); + expect(result.current).toHaveProperty("status"); + }); + it("should return MCP servers when access token is present", async () => { vi.mocked(networking.fetchMCPServers).mockResolvedValue(mockServers); @@ -61,19 +74,35 @@ describe("useMCPServers", () => { expect(result.current.isSuccess).toBe(true); }); - expect(networking.fetchMCPServers).toHaveBeenCalledWith("test-token-123"); + expect(networking.fetchMCPServers).toHaveBeenCalledWith(mockAccessToken); expect(result.current.data).toEqual(mockServers); }); - it("should not fetch when access token is not available", async () => { - const useAuthorizedModule = await import("../useAuthorized"); + it("should not fetch when access token is null", async () => { + const useAuthorizedModule = await import("@/app/(dashboard)/hooks/useAuthorized"); vi.mocked(useAuthorizedModule.default).mockReturnValue({ accessToken: null, } as any); const { result } = renderHook(() => useMCPServers(), { wrapper }); - expect(result.current.status).toBe("pending"); + expect(result.current.isFetching).toBe(false); + expect(result.current.isLoading).toBe(false); + expect(result.current.data).toBeUndefined(); + expect(networking.fetchMCPServers).not.toHaveBeenCalled(); + }); + + it("should not fetch when access token is empty string", async () => { + const useAuthorizedModule = await import("@/app/(dashboard)/hooks/useAuthorized"); + vi.mocked(useAuthorizedModule.default).mockReturnValue({ + accessToken: "", + } as any); + + const { result } = renderHook(() => useMCPServers(), { wrapper }); + + expect(result.current.isFetching).toBe(false); + expect(result.current.isLoading).toBe(false); + expect(result.current.data).toBeUndefined(); expect(networking.fetchMCPServers).not.toHaveBeenCalled(); }); @@ -88,6 +117,7 @@ describe("useMCPServers", () => { }); expect(result.current.error).toEqual(mockError); + expect(result.current.data).toBeUndefined(); }); it("should return empty array when API returns empty list", async () => {