Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import { renderHook, waitFor } from "@testing-library/react";
import { useDisableShowNewBadge } from "./useDisableShowNewBadge";
import { useDisableShowBadges } from "./useDisableShowBadges";
import { LOCAL_STORAGE_EVENT } from "@/utils/localStorageUtils";

describe("useDisableShowNewBadge", () => {
const STORAGE_KEY = "disableShowNewBadge";
describe("useDisableShowBadges", () => {
const STORAGE_KEY = "disableShowBadges";

beforeEach(() => {
localStorage.clear();
Expand All @@ -16,37 +16,37 @@ describe("useDisableShowNewBadge", () => {
});

it("should return false when localStorage is empty", () => {
const { result } = renderHook(() => useDisableShowNewBadge());
const { result } = renderHook(() => useDisableShowBadges());

expect(result.current).toBe(false);
});

it("should return false when localStorage value is not 'true'", () => {
localStorage.setItem(STORAGE_KEY, "false");

const { result } = renderHook(() => useDisableShowNewBadge());
const { result } = renderHook(() => useDisableShowBadges());

expect(result.current).toBe(false);
});

it("should return true when localStorage value is 'true'", () => {
localStorage.setItem(STORAGE_KEY, "true");

const { result } = renderHook(() => useDisableShowNewBadge());
const { result } = renderHook(() => useDisableShowBadges());

expect(result.current).toBe(true);
});

it("should return false when localStorage value is an empty string", () => {
localStorage.setItem(STORAGE_KEY, "");

const { result } = renderHook(() => useDisableShowNewBadge());
const { result } = renderHook(() => useDisableShowBadges());

expect(result.current).toBe(false);
});

it("should update when storage event fires for the correct key", async () => {
const { result } = renderHook(() => useDisableShowNewBadge());
const { result } = renderHook(() => useDisableShowBadges());

expect(result.current).toBe(false);

Expand All @@ -64,7 +64,7 @@ describe("useDisableShowNewBadge", () => {

it("should not update when storage event fires for a different key", () => {
localStorage.setItem(STORAGE_KEY, "false");
const { result } = renderHook(() => useDisableShowNewBadge());
const { result } = renderHook(() => useDisableShowBadges());

expect(result.current).toBe(false);

Expand All @@ -78,7 +78,7 @@ describe("useDisableShowNewBadge", () => {
});

it("should update when custom LOCAL_STORAGE_EVENT fires for the correct key", async () => {
const { result } = renderHook(() => useDisableShowNewBadge());
const { result } = renderHook(() => useDisableShowBadges());

expect(result.current).toBe(false);

Expand All @@ -95,7 +95,7 @@ describe("useDisableShowNewBadge", () => {

it("should not update when custom LOCAL_STORAGE_EVENT fires for a different key", () => {
localStorage.setItem(STORAGE_KEY, "false");
const { result } = renderHook(() => useDisableShowNewBadge());
const { result } = renderHook(() => useDisableShowBadges());

expect(result.current).toBe(false);

Expand All @@ -109,7 +109,7 @@ describe("useDisableShowNewBadge", () => {

it("should update when localStorage changes from false to true via custom event", async () => {
localStorage.setItem(STORAGE_KEY, "false");
const { result } = renderHook(() => useDisableShowNewBadge());
const { result } = renderHook(() => useDisableShowBadges());

expect(result.current).toBe(false);

Expand All @@ -126,7 +126,7 @@ describe("useDisableShowNewBadge", () => {

it("should update when localStorage changes from true to false via storage event", async () => {
localStorage.setItem(STORAGE_KEY, "true");
const { result } = renderHook(() => useDisableShowNewBadge());
const { result } = renderHook(() => useDisableShowBadges());

expect(result.current).toBe(true);

Expand All @@ -146,7 +146,7 @@ describe("useDisableShowNewBadge", () => {
const addEventListenerSpy = vi.spyOn(window, "addEventListener");
const removeEventListenerSpy = vi.spyOn(window, "removeEventListener");

const { unmount } = renderHook(() => useDisableShowNewBadge());
const { unmount } = renderHook(() => useDisableShowBadges());

expect(addEventListenerSpy).toHaveBeenCalledTimes(2);
expect(addEventListenerSpy).toHaveBeenCalledWith("storage", expect.any(Function));
Expand All @@ -160,8 +160,8 @@ describe("useDisableShowNewBadge", () => {
});

it("should handle multiple hooks independently", async () => {
const { result: result1 } = renderHook(() => useDisableShowNewBadge());
const { result: result2 } = renderHook(() => useDisableShowNewBadge());
const { result: result1 } = renderHook(() => useDisableShowBadges());
const { result: result2 } = renderHook(() => useDisableShowBadges());

expect(result1.current).toBe(false);
expect(result2.current).toBe(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// hooks/useDisableShowNewBadge.ts
// hooks/useDisableShowBadges.ts
import { useSyncExternalStore } from "react";
import { getLocalStorageItem } from "@/utils/localStorageUtils";
import { LOCAL_STORAGE_EVENT } from "@/utils/localStorageUtils";

function subscribe(callback: () => void) {
const onStorage = (e: StorageEvent) => {
if (e.key === "disableShowNewBadge") {
if (e.key === "disableShowBadges") {
callback();
}
};

const onCustom = (e: Event) => {
const { key } = (e as CustomEvent).detail;
if (key === "disableShowNewBadge") {
if (key === "disableShowBadges") {
callback();
}
};
Expand All @@ -27,9 +27,9 @@ function subscribe(callback: () => void) {
}

function getSnapshot() {
return getLocalStorageItem("disableShowNewBadge") === "true";
return getLocalStorageItem("disableShowBadges") === "true";
Comment thread
ryan-crabbe-berri marked this conversation as resolved.
Resolved
}

export function useDisableShowNewBadge() {
export function useDisableShowBadges() {
return useSyncExternalStore(subscribe, getSnapshot);
}
70 changes: 70 additions & 0 deletions ui/litellm-dashboard/src/components/BetaBadge.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { render, screen } from "@testing-library/react";
import { describe, it, expect, vi, beforeEach } from "vitest";
import BetaBadge from "./BetaBadge";

// Mock the hook directly
vi.mock("@/app/(dashboard)/hooks/useDisableShowBadges", () => ({
useDisableShowBadges: vi.fn(),
}));

import { useDisableShowBadges } from "@/app/(dashboard)/hooks/useDisableShowBadges";

const mockUseDisableShowBadges = vi.mocked(useDisableShowBadges);

describe("BetaBadge", () => {
beforeEach(() => {
vi.clearAllMocks();
});

it("should render the badge when disableShowBadges is false", () => {
mockUseDisableShowBadges.mockReturnValue(false);

render(<BetaBadge>Test Content</BetaBadge>);

expect(screen.getByText("Beta")).toBeInTheDocument();
expect(screen.getByText("Test Content")).toBeInTheDocument();
});

it("should render the badge when disableShowBadges is not set", () => {
mockUseDisableShowBadges.mockReturnValue(false);

render(<BetaBadge />);

expect(screen.getByText("Beta")).toBeInTheDocument();
});

it("should render only children when disableShowBadges is true", () => {
mockUseDisableShowBadges.mockReturnValue(true);

render(<BetaBadge>Test Content</BetaBadge>);

expect(screen.queryByText("Beta")).not.toBeInTheDocument();
expect(screen.getByText("Test Content")).toBeInTheDocument();
});

it("should render nothing when disableShowBadges is true and no children", () => {
mockUseDisableShowBadges.mockReturnValue(true);

const { container } = render(<BetaBadge />);

expect(container.firstChild).toBeNull();
});

it("should render badge with dot instead of text when dot prop is true", () => {
mockUseDisableShowBadges.mockReturnValue(false);

render(<BetaBadge dot={true}>Test Content</BetaBadge>);

expect(screen.queryByText("Beta")).not.toBeInTheDocument();
expect(screen.getByText("Test Content")).toBeInTheDocument();
});

it("should render badge with 'Beta' text when dot prop is not provided (defaults to false)", () => {
mockUseDisableShowBadges.mockReturnValue(false);

render(<BetaBadge>Test Content</BetaBadge>);

expect(screen.getByText("Beta")).toBeInTheDocument();
expect(screen.getByText("Test Content")).toBeInTheDocument();
});
});
18 changes: 18 additions & 0 deletions ui/litellm-dashboard/src/components/BetaBadge.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Badge } from "antd";
import { useDisableShowBadges } from "@/app/(dashboard)/hooks/useDisableShowBadges";

export default function BetaBadge({ children, dot = false }: { children?: React.ReactNode; dot?: boolean }) {
const disableShowBadges = useDisableShowBadges();

if (disableShowBadges) {
return children ? <>{children}</> : null;
}

return children ? (
<Badge color="gold" count={dot ? undefined : "Beta"} dot={dot}>
{children}
</Badge>
) : (
<Badge color="gold" count={dot ? undefined : "Beta"} dot={dot} />
);
}
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
let mockUseDisableShowPromptsImpl = () => false;

let mockGetLocalStorageItemImpl = (key: string): string | null => {
if (key === "disableShowNewBadge") return null;
if (key === "disableShowBadges") return null;
if (key === "disableShowPrompts") return null;
return null;
};
Expand Down Expand Up @@ -49,7 +49,7 @@
});
mockUseDisableShowPromptsImpl = () => false;
mockGetLocalStorageItemImpl = (key: string): string | null => {
if (key === "disableShowNewBadge") return null;
if (key === "disableShowBadges") return null;
if (key === "disableShowPrompts") return null;
return null;
};
Expand Down Expand Up @@ -143,7 +143,7 @@
expect(mockOnLogout).toHaveBeenCalledTimes(1);
});

it("should toggle hide new feature indicators switch", async () => {
it("should toggle hide feature badges switch", async () => {
const user = userEvent.setup();
renderWithProviders(<UserDropdown onLogout={mockOnLogout} />);

Expand All @@ -153,20 +153,20 @@
expect(screen.getAllByText("test@example.com").length).toBeGreaterThan(0);
});

const toggle = screen.getByLabelText("Toggle hide new feature indicators");
const toggle = screen.getByLabelText("Toggle hide feature badges");
expect(toggle).not.toBeChecked();

await user.click(toggle);

const localStorageUtils = vi.mocked(await import("@/utils/localStorageUtils"));
expect(localStorageUtils.setLocalStorageItem).toHaveBeenCalledWith("disableShowNewBadge", "true");
expect(localStorageUtils.emitLocalStorageChange).toHaveBeenCalledWith("disableShowNewBadge");
expect(localStorageUtils.setLocalStorageItem).toHaveBeenCalledWith("disableShowBadges", "true");
expect(localStorageUtils.emitLocalStorageChange).toHaveBeenCalledWith("disableShowBadges");
});

it("should toggle hide new feature indicators switch off", async () => {
it("should toggle hide feature badges switch off", async () => {
const user = userEvent.setup();
mockGetLocalStorageItemImpl = (key: string): string | null => {
if (key === "disableShowNewBadge") return "true";
if (key === "disableShowBadges") return "true";
return null;
};

Expand All @@ -178,14 +178,14 @@
expect(screen.getAllByText("test@example.com").length).toBeGreaterThan(0);
});

const toggle = screen.getByLabelText("Toggle hide new feature indicators");
const toggle = screen.getByLabelText("Toggle hide feature badges");
expect(toggle).toBeChecked();

await user.click(toggle);

const localStorageUtils = vi.mocked(await import("@/utils/localStorageUtils"));
expect(localStorageUtils.removeLocalStorageItem).toHaveBeenCalledWith("disableShowNewBadge");
expect(localStorageUtils.emitLocalStorageChange).toHaveBeenCalledWith("disableShowNewBadge");
expect(localStorageUtils.removeLocalStorageItem).toHaveBeenCalledWith("disableShowBadges");
expect(localStorageUtils.emitLocalStorageChange).toHaveBeenCalledWith("disableShowBadges");
});

it("should toggle hide all prompts switch", async () => {
Expand Down Expand Up @@ -237,7 +237,7 @@
it("should show Account in the trigger when user id is the default placeholder", () => {
mockUseAuthorizedImpl = () => ({
userId: "default_user_id",
userEmail: null as any,

Check warning on line 240 in ui/litellm-dashboard/src/components/Navbar/UserDropdown/UserDropdown.test.tsx

View workflow job for this annotation

GitHub Actions / frontend-lint

Unexpected any. Specify a different type
userRole: "Admin",
premiumUser: false,
});
Expand All @@ -249,7 +249,7 @@
const user = userEvent.setup();
mockUseAuthorizedImpl = () => ({
userId: "test-user-id",
userEmail: null as any,

Check warning on line 252 in ui/litellm-dashboard/src/components/Navbar/UserDropdown/UserDropdown.test.tsx

View workflow job for this annotation

GitHub Actions / frontend-lint

Unexpected any. Specify a different type
userRole: "Admin",
premiumUser: false,
});
Expand All @@ -266,7 +266,7 @@
it("should display dash when user ID is not available", async () => {
const user = userEvent.setup();
mockUseAuthorizedImpl = () => ({
userId: null as any,

Check warning on line 269 in ui/litellm-dashboard/src/components/Navbar/UserDropdown/UserDropdown.test.tsx

View workflow job for this annotation

GitHub Actions / frontend-lint

Unexpected any. Specify a different type
userEmail: "test@example.com",
userRole: "Admin",
premiumUser: false,
Expand All @@ -282,10 +282,10 @@
});
});

it("should initialize hide new feature indicators from localStorage", async () => {
it("should initialize hide feature badges from localStorage", async () => {
const user = userEvent.setup();
mockGetLocalStorageItemImpl = (key: string): string | null => {
if (key === "disableShowNewBadge") return "true";
if (key === "disableShowBadges") return "true";
return null;
};

Expand All @@ -297,7 +297,7 @@
expect(screen.getAllByText("test@example.com").length).toBeGreaterThan(0);
});

const toggle = screen.getByLabelText("Toggle hide new feature indicators");
const toggle = screen.getByLabelText("Toggle hide feature badges");
expect(toggle).toBeChecked();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ const UserDropdown: React.FC<UserDropdownProps> = ({ onLogout, variant = "navbar
const disableUsageIndicator = useDisableUsageIndicator();
const disableBlogPosts = useDisableBlogPosts();
const disableBouncingIcon = useDisableBouncingIcon();
const [disableShowNewBadge, setDisableShowNewBadge] = useState(false);
const [disableShowBadges, setDisableShowBadges] = useState(false);

useEffect(() => {
const storedValue = getLocalStorageItem("disableShowNewBadge");
setDisableShowNewBadge(storedValue === "true");
const storedValue = getLocalStorageItem("disableShowBadges");
setDisableShowBadges(storedValue === "true");
}, []);

const userItems: MenuProps["items"] = [
Expand Down Expand Up @@ -131,21 +131,21 @@ const UserDropdown: React.FC<UserDropdownProps> = ({ onLogout, variant = "navbar
</Space>
<Divider style={{ margin: "8px 0" }} />
<Space style={{ width: "100%", justifyContent: "space-between" }}>
<Text type="secondary">Hide New Feature Indicators</Text>
<Text type="secondary">Hide Feature Badges</Text>
<Switch
size="small"
checked={disableShowNewBadge}
checked={disableShowBadges}
onChange={(checked) => {
setDisableShowNewBadge(checked);
setDisableShowBadges(checked);
if (checked) {
setLocalStorageItem("disableShowNewBadge", "true");
emitLocalStorageChange("disableShowNewBadge");
setLocalStorageItem("disableShowBadges", "true");
emitLocalStorageChange("disableShowBadges");
} else {
removeLocalStorageItem("disableShowNewBadge");
emitLocalStorageChange("disableShowNewBadge");
removeLocalStorageItem("disableShowBadges");
emitLocalStorageChange("disableShowBadges");
}
}}
aria-label="Toggle hide new feature indicators"
aria-label="Toggle hide feature badges"
/>
</Space>
<Space style={{ width: "100%", justifyContent: "space-between" }}>
Expand Down
Loading
Loading