Skip to content

Commit

Permalink
fix: frontend tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mmtftr committed Dec 16, 2024
1 parent efc15ff commit fd22ad6
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions frontend/src/routes/question.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { useGetQuestionDetails, useSearchTags } from "@/services/api/programmingForumComponents";
import { DifficultyBar } from "@/components/DifficultyBar";
import {
useGetQuestionDetails,
useSearchTags,
} from "@/services/api/programmingForumComponents";
import { QuestionDetails } from "@/services/api/programmingForumSchemas";
import useAuthStore from "@/services/auth";
import { testAccessibility } from "@/utils/test-accessibility";
import { render, screen, fireEvent} from "@testing-library/react";
import { fireEvent, render, screen } from "@testing-library/react";
import {
createMemoryRouter,
MemoryRouter,
Expand All @@ -13,9 +17,6 @@ import {
import { beforeEach, describe, expect, it, Mock, vi } from "vitest";
import { routeConfig } from ".";
import QuestionPage from "./question";
import { DifficultyBar } from "@/components/DifficultyBar";



const mockQuestionData = vi.hoisted(
() =>
Expand Down Expand Up @@ -90,7 +91,14 @@ vi.mock("@/services/api/programmingForumComponents", () => ({
}),
})),
useSearchTags: vi.fn(() => ({
data: { data: { items: [{ tagId: "1", name: "Tag1" }, { tagId: "2", name: "Tag2" }] } },
data: {
data: {
items: [
{ tagId: "1", name: "Tag1" },
{ tagId: "2", name: "Tag2" },
],
},
},
isLoading: false,
})),
useUpdateQuestion: vi.fn(() => ({
Expand Down Expand Up @@ -127,7 +135,14 @@ describe("QuestionPage", () => {
error: null,
});
(useSearchTags as Mock).mockReturnValue({
data: { data: { items: [{ tagId: "1", name: "Tag1" }, { tagId: "2", name: "Tag2" }] } },
data: {
data: {
items: [
{ tagId: "1", name: "Tag1" },
{ tagId: "2", name: "Tag2" },
],
},
},
isLoading: false,
});
vi.mocked(useAuthStore).mockReturnValue({
Expand Down Expand Up @@ -218,7 +233,7 @@ describe("QuestionPage", () => {

it("renders bookmark button", () => {
vi.mocked(useAuthStore).mockReturnValue({
selfProfile: { id: 1},
selfProfile: { id: 1 },
token: "mock-token",
});
render(
Expand All @@ -229,7 +244,9 @@ describe("QuestionPage", () => {
</MemoryRouter>,
);

expect(screen.getByRole("button", { name: /bookmark/i })).toBeInTheDocument();
expect(
screen.getByRole("button", { name: /bookmark/i }),
).toBeInTheDocument();
});

it("updates difficulty counts when voting", async () => {
Expand All @@ -238,14 +255,16 @@ describe("QuestionPage", () => {
selfProfile: { id: 2 },
token: "mock-token",
});

render(
<MemoryRouter initialEntries={["/question/1"]}>
<Routes>
<Route
path="/question/:questionId"
element={
<DifficultyBar
selfDifficultyVote={"EASY"}
onVote={() => {}}
questionId={1}
easyCount={5}
mediumCount={3}
Expand All @@ -256,17 +275,14 @@ describe("QuestionPage", () => {
</Routes>
</MemoryRouter>,
);

// Simulate a vote on "Medium"
const mediumButton = screen.getByText("Medium");
fireEvent.click(mediumButton);

// Verify the button is disabled after voting
expect(await screen.findByText("Easy: 0 votes")).toBeInTheDocument();
expect(await screen.findByText("Medium: 1 votes")).toBeInTheDocument();
expect(await screen.findByText("Hard: 0 votes")).toBeInTheDocument();

});


});

0 comments on commit fd22ad6

Please sign in to comment.