diff --git a/frontend/app/chat/components/ChatsList/__tests__/ChatList.test.tsx b/frontend/app/chat/components/ChatsList/__tests__/ChatList.test.tsx new file mode 100644 index 000000000..db9547d2b --- /dev/null +++ b/frontend/app/chat/components/ChatsList/__tests__/ChatList.test.tsx @@ -0,0 +1,64 @@ +import { fireEvent, render, screen } from "@testing-library/react"; +import { describe, expect, it, vi } from "vitest"; + +import { ChatsList } from "../index"; + +const setOpenMock = vi.fn(() => ({})); + +vi.mock("../hooks/useChatsList", () => ({ + useChatsList: () => ({ + open: false, + setOpen: () => setOpenMock(), + }), +})); + +vi.mock("@/lib/context/ChatsProvider/hooks/useChatsContext", () => ({ + useChatsContext: () => ({ + allChats: [ + { chat_id: 1, name: "Chat 1" }, + { chat_id: 2, name: "Chat 2" }, + ], + deleteChat: vi.fn(), + }), +})); + +vi.mock("@/lib/hooks", async () => { + const actual = await vi.importActual( + "@/lib/hooks" + ); + + return { + ...actual, + useAxios: () => ({ + axiosInstance: vi.fn(), + }), + }; +}); + +describe("ChatsList", () => { + it("should render correctly", () => { + const { getByTestId } = render(); + const chatsList = getByTestId("chats-list"); + expect(chatsList).toBeDefined(); + + const newChatButton = getByTestId("new-chat-button"); + expect(newChatButton).toBeDefined(); + + const toggleButton = getByTestId("chats-list-toggle"); + expect(toggleButton).toBeDefined(); + }); + + it("renders the chats list with correct number of items", () => { + render(); + const chatItems = screen.getAllByTestId("chats-list-item"); + expect(chatItems).toHaveLength(2); + }); + + it("toggles the open state when the button is clicked", () => { + render(); + const toggleButton = screen.getByTestId("chats-list-toggle"); + fireEvent.click(toggleButton); + + expect(setOpenMock).toHaveBeenCalledTimes(1); + }); +}); diff --git a/frontend/app/chat/components/ChatsList/components/ChatsListItem/ChatsListItem.tsx b/frontend/app/chat/components/ChatsList/components/ChatsListItem/ChatsListItem.tsx index de9b8d500..45fa48786 100644 --- a/frontend/app/chat/components/ChatsList/components/ChatsListItem/ChatsListItem.tsx +++ b/frontend/app/chat/components/ChatsList/components/ChatsListItem/ChatsListItem.tsx @@ -53,6 +53,7 @@ export const ChatsListItem = ({ ? "bg-gray-100 dark:bg-gray-800 text-primary dark:text-white" : "" )} + data-testid="chats-list-item" > ( New Chat diff --git a/frontend/app/chat/components/ChatsList/index.tsx b/frontend/app/chat/components/ChatsList/index.tsx index 3da7f9e1e..4e4c7596e 100644 --- a/frontend/app/chat/components/ChatsList/index.tsx +++ b/frontend/app/chat/components/ChatsList/index.tsx @@ -1,6 +1,6 @@ /* eslint-disable */ "use client"; -import useChatsContext from "@/lib/context/ChatsProvider/hooks/useChatsContext"; +import { useChatsContext } from "@/lib/context/ChatsProvider/hooks/useChatsContext"; import { cn } from "@/lib/utils"; import { MotionConfig, motion } from "framer-motion"; import { MdChevronRight } from "react-icons/md"; @@ -37,10 +37,14 @@ export const ChatsList = (): JSX.Element => { : "10px 10px 16px rgba(0, 0, 0, 0.5)", }} className={cn("overflow-hidden flex flex-col flex-1")} + data-testid="chats-list" >
-
+
{allChats.map((chat) => ( { setOpen(!open); }} className="absolute left-full top-16 text-3xl bg-black dark:bg-white text-white dark:text-black rounded-r-full p-3 pl-1" + data-testid="chats-list-toggle" > { +export const useChatsContext = () => { const context = useContext(ChatsContext); if (context === undefined) { @@ -12,5 +12,3 @@ const useChatsContext = () => { return context; }; - -export default useChatsContext;