From 6d9a44a0e39a72026049d3c415eb8643da498033 Mon Sep 17 00:00:00 2001 From: Dan Ditomaso Date: Fri, 28 Mar 2025 21:11:01 -0400 Subject: [PATCH] added tests --- src/components/CommandPalette/index.tsx | 2 +- src/core/hooks/usePinnedItems.test.ts | 65 +++++++++++++++++++ .../{usePinnedItems.tsx => usePinnedItems.ts} | 0 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 src/core/hooks/usePinnedItems.test.ts rename src/core/hooks/{usePinnedItems.tsx => usePinnedItems.ts} (100%) diff --git a/src/components/CommandPalette/index.tsx b/src/components/CommandPalette/index.tsx index 185a1ee6..289f07fc 100644 --- a/src/components/CommandPalette/index.tsx +++ b/src/components/CommandPalette/index.tsx @@ -33,7 +33,7 @@ import { import { useEffect } from "react"; import { Avatar } from "@components/UI/Avatar.tsx"; import { cn } from "@core/utils/cn.ts"; -import { usePinnedItems } from "@core/hooks/usePinnedItems.tsx"; +import { usePinnedItems } from "@core/hooks/usePinnedItems.ts"; export interface Group { label: string; diff --git a/src/core/hooks/usePinnedItems.test.ts b/src/core/hooks/usePinnedItems.test.ts new file mode 100644 index 00000000..d761fba1 --- /dev/null +++ b/src/core/hooks/usePinnedItems.test.ts @@ -0,0 +1,65 @@ +import { renderHook, act } from "@testing-library/react"; +import { describe, it, expect, vi, beforeEach } from "vitest"; +import { usePinnedItems } from "./usePinnedItems.ts"; + +const mockSetPinnedItems = vi.fn(); +const mockUseLocalStorage = vi.fn(); + +vi.mock("@core/hooks/useLocalStorage.ts", () => ({ + default: (...args: any[]) => mockUseLocalStorage(...args), +})); + +describe("usePinnedItems", () => { + beforeEach(() => { + vi.clearAllMocks(); + }); + + it("returns default pinnedItems and togglePinnedItem", () => { + mockUseLocalStorage.mockReturnValue([[], mockSetPinnedItems]); + + const { result } = renderHook(() => + usePinnedItems({ storageName: "test-storage" }) + ); + + expect(result.current.pinnedItems).toEqual([]); + expect(typeof result.current.togglePinnedItem).toBe("function"); + }); + + it("adds an item if it's not already pinned", () => { + mockUseLocalStorage.mockReturnValue([["item1"], mockSetPinnedItems]); + + const { result } = renderHook(() => + usePinnedItems({ storageName: "test-storage" }) + ); + + act(() => { + result.current.togglePinnedItem("item2"); + }); + + expect(mockSetPinnedItems).toHaveBeenCalledWith(expect.any(Function)); + + const updater = mockSetPinnedItems.mock.calls[0][0]; + const updated = updater(["item1"]); + + expect(updated).toEqual(["item1", "item2"]); + }); + + it("removes an item if it's already pinned", () => { + mockUseLocalStorage.mockReturnValue([["item1", "item2"], mockSetPinnedItems]); + + const { result } = renderHook(() => + usePinnedItems({ storageName: "test-storage" }) + ); + + act(() => { + result.current.togglePinnedItem("item1"); + }); + + expect(mockSetPinnedItems).toHaveBeenCalledWith(expect.any(Function)); + + const updater = mockSetPinnedItems.mock.calls[0][0]; + const updated = updater(["item1", "item2"]); + + expect(updated).toEqual(["item2"]); + }); +}); diff --git a/src/core/hooks/usePinnedItems.tsx b/src/core/hooks/usePinnedItems.ts similarity index 100% rename from src/core/hooks/usePinnedItems.tsx rename to src/core/hooks/usePinnedItems.ts