Adding tests
This commit is contained in:
parent
28bae2264c
commit
bdf47eb0f0
@ -10,12 +10,6 @@ vi.mock("@/components/networking", () => ({
|
||||
getUiSettings: vi.fn(),
|
||||
}));
|
||||
|
||||
// Mock useAuthorized hook - we can override this in individual tests
|
||||
const mockUseAuthorized = vi.fn();
|
||||
vi.mock("../useAuthorized", () => ({
|
||||
default: () => mockUseAuthorized(),
|
||||
}));
|
||||
|
||||
// Mock data
|
||||
const mockUISettings: Record<string, any> = {
|
||||
theme: "dark",
|
||||
@ -39,18 +33,6 @@ describe("useUISettings", () => {
|
||||
|
||||
// Reset all mocks
|
||||
vi.clearAllMocks();
|
||||
|
||||
// Set default mock for useAuthorized (enabled state)
|
||||
mockUseAuthorized.mockReturnValue({
|
||||
accessToken: "test-access-token",
|
||||
userRole: "Admin",
|
||||
userId: "test-user-id",
|
||||
token: "test-token",
|
||||
userEmail: "test@example.com",
|
||||
premiumUser: false,
|
||||
disabledPersonalKeyCreation: null,
|
||||
showSSOBanner: false,
|
||||
});
|
||||
});
|
||||
|
||||
const wrapper = ({ children }: { children: ReactNode }) =>
|
||||
@ -74,7 +56,7 @@ describe("useUISettings", () => {
|
||||
|
||||
expect(result.current.data).toEqual(mockUISettings);
|
||||
expect(result.current.error).toBeNull();
|
||||
expect(getUiSettings).toHaveBeenCalledWith("test-access-token");
|
||||
expect(getUiSettings).toHaveBeenCalledWith();
|
||||
expect(getUiSettings).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
@ -98,58 +80,10 @@ describe("useUISettings", () => {
|
||||
|
||||
expect(result.current.error).toEqual(testError);
|
||||
expect(result.current.data).toBeUndefined();
|
||||
expect(getUiSettings).toHaveBeenCalledWith("test-access-token");
|
||||
expect(getUiSettings).toHaveBeenCalledWith();
|
||||
expect(getUiSettings).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("should not execute query when accessToken is missing", async () => {
|
||||
// Mock missing accessToken
|
||||
mockUseAuthorized.mockReturnValue({
|
||||
accessToken: null,
|
||||
userRole: "Admin",
|
||||
userId: "test-user-id",
|
||||
token: null,
|
||||
userEmail: "test@example.com",
|
||||
premiumUser: false,
|
||||
disabledPersonalKeyCreation: null,
|
||||
showSSOBanner: false,
|
||||
});
|
||||
|
||||
const { result } = renderHook(() => useUISettings(), { wrapper });
|
||||
|
||||
// Query should not execute
|
||||
expect(result.current.isLoading).toBe(false);
|
||||
expect(result.current.data).toBeUndefined();
|
||||
expect(result.current.isFetched).toBe(false);
|
||||
|
||||
// API should not be called
|
||||
expect(getUiSettings).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should not execute query when accessToken is empty string", async () => {
|
||||
// Mock empty accessToken
|
||||
mockUseAuthorized.mockReturnValue({
|
||||
accessToken: "",
|
||||
userRole: "Admin",
|
||||
userId: "test-user-id",
|
||||
token: "",
|
||||
userEmail: "test@example.com",
|
||||
premiumUser: false,
|
||||
disabledPersonalKeyCreation: null,
|
||||
showSSOBanner: false,
|
||||
});
|
||||
|
||||
const { result } = renderHook(() => useUISettings(), { wrapper });
|
||||
|
||||
// Query should not execute
|
||||
expect(result.current.isLoading).toBe(false);
|
||||
expect(result.current.data).toBeUndefined();
|
||||
expect(result.current.isFetched).toBe(false);
|
||||
|
||||
// API should not be called
|
||||
expect(getUiSettings).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("should return empty object when API returns empty settings", async () => {
|
||||
// Mock API returning empty object
|
||||
(getUiSettings as any).mockResolvedValue({});
|
||||
@ -163,7 +97,7 @@ describe("useUISettings", () => {
|
||||
});
|
||||
|
||||
expect(result.current.data).toEqual({});
|
||||
expect(getUiSettings).toHaveBeenCalledWith("test-access-token");
|
||||
expect(getUiSettings).toHaveBeenCalledWith();
|
||||
});
|
||||
|
||||
it("should handle network timeout error", async () => {
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
import { getUiSettings } from "@/components/networking";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { createQueryKeys } from "../common/queryKeysFactory";
|
||||
import useAuthorized from "../useAuthorized";
|
||||
|
||||
const uiSettingsKeys = createQueryKeys("uiSettings");
|
||||
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import * as networking from "@/components/networking";
|
||||
import { render, screen, waitFor } from "@testing-library/react";
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { renderWithProviders, screen, waitFor } from "../../../tests/test-utils";
|
||||
import ModelHubTable from "./ModelHubTable";
|
||||
|
||||
vi.mock("@/components/networking", () => ({
|
||||
@ -11,6 +11,8 @@ vi.mock("@/components/networking", () => ({
|
||||
getProxyBaseUrl: vi.fn(() => "http://localhost:4000"),
|
||||
getAgentsList: vi.fn(),
|
||||
fetchMCPServers: vi.fn(),
|
||||
getUiSettings: vi.fn(),
|
||||
getClaudeCodeMarketplace: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("next/navigation", () => ({
|
||||
@ -39,8 +41,11 @@ describe("ModelHubTable", () => {
|
||||
agents: [],
|
||||
});
|
||||
vi.mocked(networking.fetchMCPServers).mockResolvedValue([]);
|
||||
vi.mocked(networking.getUiSettings).mockResolvedValue({
|
||||
values: {},
|
||||
});
|
||||
|
||||
render(<ModelHubTable accessToken="test-token" publicPage={false} premiumUser={false} userRole={null} />);
|
||||
renderWithProviders(<ModelHubTable accessToken="test-token" publicPage={false} premiumUser={false} userRole={null} />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText("AI Hub")).toBeInTheDocument();
|
||||
@ -58,8 +63,11 @@ describe("ModelHubTable", () => {
|
||||
admin_ui_disabled: false,
|
||||
});
|
||||
modelHubPublicModelsCallMock.mockResolvedValue([]);
|
||||
vi.mocked(networking.getUiSettings).mockResolvedValue({
|
||||
values: {},
|
||||
});
|
||||
|
||||
render(<ModelHubTable accessToken={null} publicPage={true} premiumUser={false} userRole={null} />);
|
||||
renderWithProviders(<ModelHubTable accessToken={null} publicPage={true} premiumUser={false} userRole={null} />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(getUiConfigMock).toHaveBeenCalled();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user