From a103f21d17b18999a3e2231121fe48d2e6a28cb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Asl=C4=B1=20G=C3=B6k?= Date: Mon, 16 Dec 2024 17:49:53 +0300 Subject: [PATCH] add glossary test --- frontend/src/routes/glossary.test.tsx | 79 +++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 frontend/src/routes/glossary.test.tsx diff --git a/frontend/src/routes/glossary.test.tsx b/frontend/src/routes/glossary.test.tsx new file mode 100644 index 0000000..3a84833 --- /dev/null +++ b/frontend/src/routes/glossary.test.tsx @@ -0,0 +1,79 @@ +// glossary.test.tsx +import { useSearchTags } from "@/services/api/programmingForumComponents"; +import { TagDetails } from "@/services/api/programmingForumSchemas"; +import { render, screen } from "@testing-library/react"; +import { MemoryRouter, Route, Routes } from "react-router-dom"; +import { beforeEach, describe, expect, it, Mock, vi } from "vitest"; +import GlossaryPage from "./glossary"; + +const mockTagData = vi.hoisted( + () => + ({ + tagId: "1", + name: "javascript", + description: + "A popular programming language primarily used for web development.", + questionCount: 50, + followerCount: 1000, + logoImage: "https://example.com/logo.jpg", + officialWebsite: "https://example.com", + //createdAt: "2023-01-01T00:00:00Z", + }) satisfies TagDetails, +); + +vi.mock("@/services/api/programmingForumComponents", () => ({ + useSearchTags: vi.fn(() => ({ + data: { data: { items: [mockTagData] } }, + isLoading: false, + error: null, + })), +})); + +describe("GlossaryPage", () => { + beforeEach(() => { + (useSearchTags as Mock).mockReturnValue({ + data: { data: { items: [mockTagData] } }, + isLoading: false, + error: null, + }); + }); + + it("renders glossary title correctly", () => { + render( + + + } /> + + , + ); + + // Check if glossary title is rendered + expect(screen.getByText(/Explore Various Tag Types/i)).toBeInTheDocument(); + }); + + it("renders tag counts and descriptions correctly", () => { + render( + + + } /> + + , + ); + }); + + it("renders error alert when there is an error", () => { + (useSearchTags as Mock).mockReturnValue({ + data: null, + isLoading: false, + error: new Error("Something went wrong"), + }); + + render( + + + } /> + + , + ); + }); +});