mirror of
https://github.com/tiennm99/litellm.git
synced 2026-07-12 03:06:22 +00:00
Adding tests:
This commit is contained in:
@@ -16,6 +16,7 @@ vi.mock("@/utils/proxyUtils", () => ({
|
||||
let mockUseThemeImpl = () => ({ logoUrl: null as string | null });
|
||||
let mockUseHealthReadinessImpl = () => ({ data: null as any });
|
||||
let mockGetLocalStorageItemImpl = () => null as string | null;
|
||||
let mockUseDisableShowPromptsImpl = () => false;
|
||||
|
||||
vi.mock("@/contexts/ThemeContext", () => ({
|
||||
useTheme: () => mockUseThemeImpl(),
|
||||
@@ -25,7 +26,12 @@ vi.mock("@/app/(dashboard)/hooks/healthReadiness/useHealthReadiness", () => ({
|
||||
useHealthReadiness: () => mockUseHealthReadinessImpl(),
|
||||
}));
|
||||
|
||||
vi.mock("@/app/(dashboard)/hooks/useDisableShowPrompts", () => ({
|
||||
useDisableShowPrompts: () => mockUseDisableShowPromptsImpl(),
|
||||
}));
|
||||
|
||||
vi.mock("@/utils/localStorageUtils", () => ({
|
||||
LOCAL_STORAGE_EVENT: "local-storage-change",
|
||||
getLocalStorageItem: () => mockGetLocalStorageItemImpl(),
|
||||
setLocalStorageItem: vi.fn(),
|
||||
removeLocalStorageItem: vi.fn(),
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
import { render, screen } from "@testing-library/react";
|
||||
import { MessageSquare } from "lucide-react";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { NudgePrompt } from "./NudgePrompt";
|
||||
|
||||
vi.mock("@/app/(dashboard)/hooks/useDisableShowPrompts", () => ({
|
||||
useDisableShowPrompts: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("@/utils/localStorageUtils", () => ({
|
||||
setLocalStorageItem: vi.fn(),
|
||||
emitLocalStorageChange: vi.fn(),
|
||||
LOCAL_STORAGE_EVENT: "local-storage-change",
|
||||
}));
|
||||
|
||||
import { useDisableShowPrompts } from "@/app/(dashboard)/hooks/useDisableShowPrompts";
|
||||
import { emitLocalStorageChange, setLocalStorageItem } from "@/utils/localStorageUtils";
|
||||
|
||||
const mockUseDisableShowPrompts = vi.mocked(useDisableShowPrompts);
|
||||
const mockSetLocalStorageItem = vi.mocked(setLocalStorageItem);
|
||||
const mockEmitLocalStorageChange = vi.mocked(emitLocalStorageChange);
|
||||
|
||||
const defaultProps = {
|
||||
onOpen: vi.fn(),
|
||||
onDismiss: vi.fn(),
|
||||
isVisible: true,
|
||||
title: "Test Title",
|
||||
description: "Test Description",
|
||||
buttonText: "Open Modal",
|
||||
icon: MessageSquare,
|
||||
accentColor: "#3b82f6",
|
||||
};
|
||||
|
||||
describe("NudgePrompt", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockUseDisableShowPrompts.mockReturnValue(false);
|
||||
vi.useFakeTimers();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers();
|
||||
});
|
||||
|
||||
it("should render", () => {
|
||||
render(<NudgePrompt {...defaultProps} />);
|
||||
|
||||
expect(screen.getByText("Test Title")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("should render with all provided props", () => {
|
||||
const { container } = render(<NudgePrompt {...defaultProps} />);
|
||||
|
||||
expect(screen.getByText("Test Title")).toBeInTheDocument();
|
||||
expect(screen.getByText("Test Description")).toBeInTheDocument();
|
||||
expect(screen.getByRole("button", { name: "Open Modal" })).toBeInTheDocument();
|
||||
expect(container.querySelector("svg")).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("should not render when isVisible is false", () => {
|
||||
render(<NudgePrompt {...defaultProps} isVisible={false} />);
|
||||
|
||||
expect(screen.queryByText("Test Title")).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("should not render when disableShowPrompts is true", () => {
|
||||
mockUseDisableShowPrompts.mockReturnValue(true);
|
||||
|
||||
render(<NudgePrompt {...defaultProps} />);
|
||||
|
||||
expect(screen.queryByText("Test Title")).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("should display progress bar with correct accent color", () => {
|
||||
const { container } = render(<NudgePrompt {...defaultProps} accentColor="#ff0000" />);
|
||||
|
||||
const progressBar = container.querySelector("div[style*='width']");
|
||||
expect(progressBar).toHaveStyle({ backgroundColor: "#ff0000" });
|
||||
});
|
||||
|
||||
it("should reset progress when isVisible becomes false", () => {
|
||||
const { rerender, container } = render(<NudgePrompt {...defaultProps} />);
|
||||
|
||||
vi.advanceTimersByTime(5000);
|
||||
|
||||
rerender(<NudgePrompt {...defaultProps} isVisible={false} />);
|
||||
|
||||
rerender(<NudgePrompt {...defaultProps} isVisible={true} />);
|
||||
|
||||
const progressBar = container.querySelector("div[style*='width']");
|
||||
expect(progressBar?.getAttribute("style")).toContain("width: 100%");
|
||||
});
|
||||
|
||||
it("should apply custom button style when provided", () => {
|
||||
const buttonStyle = { backgroundColor: "#custom-color" };
|
||||
render(<NudgePrompt {...defaultProps} buttonStyle={buttonStyle} />);
|
||||
|
||||
const openButton = screen.getByRole("button", { name: "Open Modal" });
|
||||
expect(openButton).toHaveStyle(buttonStyle);
|
||||
});
|
||||
});
|
||||
@@ -138,7 +138,7 @@ export function NudgePrompt({
|
||||
onClick={handleDontAskAgain}
|
||||
className="text-xs"
|
||||
>
|
||||
Don't ask me again
|
||||
Don't ask me again
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user