Skip to content

Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.

chore: Write unit tests for question bank feature

Khuram Niaz86e785055e65
diff --git a/__tests__/addQuestionForm.schema.spec.ts b/__tests__/addQuestionForm.schema.spec.ts
new file mode 100644
index 0000000..d8a8876
--- /dev/null
+++ b/__tests__/addQuestionForm.schema.spec.ts
@@ -0,0 +1,93 @@
+import { AddQuestionFormSchema } from "@/models/addQuestionForm.schema";
+
+describe("AddQuestionFormSchema", () => {
+ describe("valid data", () => {
+ it("should accept valid question with all fields", () => {
+ const data = {
+ question: "What is React?",
+ answer: "A JavaScript library for building UIs.",
+ tagIds: ["tag-1", "tag-2"],
+ };
+ const result = AddQuestionFormSchema.parse(data);
+ expect(result.question).toBe("What is React?");
+ expect(result.answer).toBe("A JavaScript library for building UIs.");
+ expect(result.tagIds).toEqual(["tag-1", "tag-2"]);
+ });
+
+ it("should accept question without answer", () => {
+ const data = { question: "What is React?" };
+ const result = AddQuestionFormSchema.parse(data);
+ expect(result.question).toBe("What is React?");
+ expect(result.answer).toBeUndefined();
+ });
+
+ it("should accept null answer", () => {
+ const data = { question: "What is React?", answer: null };
+ const result = AddQuestionFormSchema.parse(data);
+ expect(result.answer).toBeNull();
+ });
+
+ it("should accept question without tagIds", () => {
+ const data = { question: "What is React?" };
+ const result = AddQuestionFormSchema.parse(data);
+ expect(result.tagIds).toBeUndefined();
+ });
+
+ it("should accept empty tagIds array", () => {
+ const data = { question: "What is React?", tagIds: [] };
+ const result = AddQuestionFormSchema.parse(data);
+ expect(result.tagIds).toEqual([]);
+ });
+
+ it("should accept optional id for editing", () => {
+ const data = { id: "q-123", question: "Updated question?" };
+ const result = AddQuestionFormSchema.parse(data);
+ expect(result.id).toBe("q-123");
+ });
+
+ it("should accept minimum length question (2 chars)", () => {
+ const data = { question: "ab" };
+ const result = AddQuestionFormSchema.parse(data);
+ expect(result.question).toBe("ab");
+ });
+
+ it("should accept up to 10 tags", () => {
+ const tagIds = Array.from({ length: 10 }, (_, i) => `tag-${i}`);
+ const data = { question: "What is React?", tagIds };
+ const result = AddQuestionFormSchema.parse(data);
+ expect(result.tagIds).toHaveLength(10);
+ });
+ });
+
+ describe("invalid data", () => {
+ it("should reject question shorter than 2 characters", () => {
+ const data = { question: "a" };
+ expect(() => AddQuestionFormSchema.parse(data)).toThrow();
+ });
+
+ it("should reject empty question", () => {
+ const data = { question: "" };
+ expect(() => AddQuestionFormSchema.parse(data)).toThrow();
+ });
+
+ it("should reject missing question", () => {
+ const data = { answer: "Some answer" };
+ expect(() => AddQuestionFormSchema.parse(data)).toThrow();
+ });
+
+ it("should reject answer exceeding 5000 characters", () => {
+ const data = { question: "What?", answer: "x".repeat(5001) };
+ expect(() => AddQuestionFormSchema.parse(data)).toThrow();
+ });
+
+ it("should reject more than 10 tags", () => {
+ const tagIds = Array.from({ length: 11 }, (_, i) => `tag-${i}`);
+ const data = { question: "What?", tagIds };
+ expect(() => AddQuestionFormSchema.parse(data)).toThrow();
+ });
+
+ it("should reject empty object", () => {
+ expect(() => AddQuestionFormSchema.parse({})).toThrow();
+ });
+ });
+});
diff --git a/__tests__/question.actions.spec.ts b/__tests__/question.actions.spec.ts
new file mode 100644
index 0000000..57b8694
--- /dev/null
+++ b/__tests__/question.actions.spec.ts
@@ -0,0 +1,518 @@
+import {
+ getQuestionsList,
+ getQuestionById,
+ createQuestion,
+ updateQuestion,
+ deleteQuestion,
+ getTagsWithQuestionCounts,
+} from "@/actions/question.actions";
+import { getCurrentUser } from "@/utils/user.utils";
+import { PrismaClient } from "@prisma/client";
+
+const prisma = new PrismaClient();
+
+jest.mock("@prisma/client", () => {
+ const mPrismaClient = {
+ question: {
+ findMany: jest.fn(),
+ findFirst: jest.fn(),
+ create: jest.fn(),
+ update: jest.fn(),
+ delete: jest.fn(),
+ count: jest.fn(),
+ },
+ tag: {
+ findMany: jest.fn(),
+ },
+ };
+ return { PrismaClient: jest.fn(() => mPrismaClient) };
+});
+
+jest.mock("@/utils/user.utils", () => ({
+ getCurrentUser: jest.fn(),
+}));
+
+describe("Question Actions", () => {
+ const mockUser = { id: "user-id" };
+
+ const mockQuestion = {
+ id: "q-1",
+ question: "What is React?",
+ answer: "<p>A JS library</p>",
+ createdBy: mockUser.id,
+ tags: [{ id: "tag-1", label: "React", value: "react" }],
+ createdAt: new Date(),
+ updatedAt: new Date(),
+ };
+
+ beforeEach(() => {
+ jest.clearAllMocks();
+ });
+
+ // getQuestionsList
+ describe("getQuestionsList", () => {
+ it("should return paginated questions", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.question.findMany as jest.Mock).mockResolvedValue([mockQuestion]);
+ (prisma.question.count as jest.Mock).mockResolvedValue(1);
+
+ const result = await getQuestionsList(1, 10);
+
+ expect(result).toEqual({ success: true, data: [mockQuestion], total: 1 });
+ expect(prisma.question.findMany).toHaveBeenCalledWith(
+ expect.objectContaining({
+ where: { createdBy: mockUser.id },
+ skip: 0,
+ take: 10,
+ orderBy: [{ createdAt: "desc" }],
+ include: { tags: true },
+ }),
+ );
+ });
+
+ it("should calculate offset for page 2", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.question.findMany as jest.Mock).mockResolvedValue([]);
+ (prisma.question.count as jest.Mock).mockResolvedValue(0);
+
+ await getQuestionsList(2, 10);
+
+ expect(prisma.question.findMany).toHaveBeenCalledWith(
+ expect.objectContaining({ skip: 10, take: 10 }),
+ );
+ });
+
+ it("should filter by tag when filter is provided", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.question.findMany as jest.Mock).mockResolvedValue([]);
+ (prisma.question.count as jest.Mock).mockResolvedValue(0);
+
+ await getQuestionsList(1, 10, "tag-1");
+
+ expect(prisma.question.findMany).toHaveBeenCalledWith(
+ expect.objectContaining({
+ where: {
+ createdBy: mockUser.id,
+ tags: { some: { id: "tag-1" } },
+ },
+ }),
+ );
+ });
+
+ it("should search by question and answer content", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.question.findMany as jest.Mock).mockResolvedValue([]);
+ (prisma.question.count as jest.Mock).mockResolvedValue(0);
+
+ await getQuestionsList(1, 10, undefined, "react");
+
+ expect(prisma.question.findMany).toHaveBeenCalledWith(
+ expect.objectContaining({
+ where: {
+ createdBy: mockUser.id,
+ OR: [
+ { question: { contains: "react" } },
+ { answer: { contains: "react" } },
+ ],
+ },
+ }),
+ );
+ });
+
+ it("should apply both filter and search together", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.question.findMany as jest.Mock).mockResolvedValue([]);
+ (prisma.question.count as jest.Mock).mockResolvedValue(0);
+
+ await getQuestionsList(1, 10, "tag-1", "react");
+
+ expect(prisma.question.findMany).toHaveBeenCalledWith(
+ expect.objectContaining({
+ where: {
+ createdBy: mockUser.id,
+ tags: { some: { id: "tag-1" } },
+ OR: [
+ { question: { contains: "react" } },
+ { answer: { contains: "react" } },
+ ],
+ },
+ }),
+ );
+ });
+
+ it("should return error for unauthenticated user", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(null);
+
+ const result = await getQuestionsList(1, 10);
+
+ expect(result).toEqual({
+ success: false,
+ message: "Not authenticated",
+ });
+ expect(prisma.question.findMany).not.toHaveBeenCalled();
+ });
+
+ it("should handle database errors", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.question.findMany as jest.Mock).mockRejectedValue(
+ new Error("DB error"),
+ );
+
+ const result = await getQuestionsList(1, 10);
+
+ expect(result).toEqual({ success: false, message: "DB error" });
+ });
+ });
+
+ // getQuestionById
+ describe("getQuestionById", () => {
+ it("should return question by id", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.question.findFirst as jest.Mock).mockResolvedValue(mockQuestion);
+
+ const result = await getQuestionById("q-1");
+
+ expect(result).toEqual({ success: true, data: mockQuestion });
+ expect(prisma.question.findFirst).toHaveBeenCalledWith({
+ where: { id: "q-1", createdBy: mockUser.id },
+ include: { tags: true },
+ });
+ });
+
+ it("should return not found when question does not exist", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.question.findFirst as jest.Mock).mockResolvedValue(null);
+
+ const result = await getQuestionById("nonexistent");
+
+ expect(result).toEqual({
+ success: false,
+ message: "Question not found",
+ });
+ });
+
+ it("should return error for unauthenticated user", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(null);
+
+ const result = await getQuestionById("q-1");
+
+ expect(result).toEqual({
+ success: false,
+ message: "Not authenticated",
+ });
+ expect(prisma.question.findFirst).not.toHaveBeenCalled();
+ });
+
+ it("should handle database errors", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.question.findFirst as jest.Mock).mockRejectedValue(
+ new Error("DB error"),
+ );
+
+ const result = await getQuestionById("q-1");
+
+ expect(result).toEqual({ success: false, message: "DB error" });
+ });
+ });
+
+ // createQuestion
+ describe("createQuestion", () => {
+ it("should create a question with tags", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.question.create as jest.Mock).mockResolvedValue(mockQuestion);
+
+ const result = await createQuestion({
+ question: "What is React?",
+ answer: "<p>A JS library</p>",
+ tagIds: ["tag-1"],
+ });
+
+ expect(result).toEqual({ success: true, data: mockQuestion });
+ expect(prisma.question.create).toHaveBeenCalledWith({
+ data: {
+ question: "What is React?",
+ answer: "<p>A JS library</p>",
+ createdBy: mockUser.id,
+ tags: { connect: [{ id: "tag-1" }] },
+ },
+ include: { tags: true },
+ });
+ });
+
+ it("should create a question without tags", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.question.create as jest.Mock).mockResolvedValue({
+ ...mockQuestion,
+ tags: [],
+ });
+
+ const result = await createQuestion({
+ question: "What is React?",
+ });
+
+ expect(result?.success).toBe(true);
+ expect(prisma.question.create).toHaveBeenCalledWith({
+ data: {
+ question: "What is React?",
+ answer: null,
+ createdBy: mockUser.id,
+ tags: undefined,
+ },
+ include: { tags: true },
+ });
+ });
+
+ it("should create a question with null answer", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.question.create as jest.Mock).mockResolvedValue(mockQuestion);
+
+ await createQuestion({ question: "What?", answer: null });
+
+ expect(prisma.question.create).toHaveBeenCalledWith(
+ expect.objectContaining({
+ data: expect.objectContaining({ answer: null }),
+ }),
+ );
+ });
+
+ it("should reject invalid data (question too short)", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+
+ const result = await createQuestion({ question: "a" });
+
+ expect(result?.success).toBe(false);
+ expect(prisma.question.create).not.toHaveBeenCalled();
+ });
+
+ it("should return error for unauthenticated user", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(null);
+
+ const result = await createQuestion({ question: "What is React?" });
+
+ expect(result).toEqual({
+ success: false,
+ message: "Not authenticated",
+ });
+ expect(prisma.question.create).not.toHaveBeenCalled();
+ });
+
+ it("should handle database errors", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.question.create as jest.Mock).mockRejectedValue(
+ new Error("DB error"),
+ );
+
+ const result = await createQuestion({ question: "What is React?" });
+
+ expect(result).toEqual({ success: false, message: "DB error" });
+ });
+ });
+
+ // updateQuestion
+ describe("updateQuestion", () => {
+ it("should update a question with new tags", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.question.update as jest.Mock).mockResolvedValue(mockQuestion);
+
+ const result = await updateQuestion({
+ id: "q-1",
+ question: "Updated question?",
+ answer: "<p>Updated answer</p>",
+ tagIds: ["tag-1", "tag-2"],
+ });
+
+ expect(result).toEqual({ success: true, data: mockQuestion });
+ expect(prisma.question.update).toHaveBeenCalledWith({
+ where: { id: "q-1", createdBy: mockUser.id },
+ data: {
+ question: "Updated question?",
+ answer: "<p>Updated answer</p>",
+ tags: { set: [{ id: "tag-1" }, { id: "tag-2" }] },
+ },
+ include: { tags: true },
+ });
+ });
+
+ it("should clear tags when tagIds is empty", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.question.update as jest.Mock).mockResolvedValue(mockQuestion);
+
+ await updateQuestion({
+ id: "q-1",
+ question: "Updated?",
+ tagIds: [],
+ });
+
+ expect(prisma.question.update).toHaveBeenCalledWith(
+ expect.objectContaining({
+ data: expect.objectContaining({
+ tags: { set: [] },
+ }),
+ }),
+ );
+ });
+
+ it("should return error when id is missing", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+
+ const result = await updateQuestion({ question: "No ID?" });
+
+ expect(result?.success).toBe(false);
+ expect(prisma.question.update).not.toHaveBeenCalled();
+ });
+
+ it("should return error for unauthenticated user", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(null);
+
+ const result = await updateQuestion({
+ id: "q-1",
+ question: "Updated?",
+ });
+
+ expect(result).toEqual({
+ success: false,
+ message: "Not authenticated",
+ });
+ expect(prisma.question.update).not.toHaveBeenCalled();
+ });
+
+ it("should handle database errors", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.question.update as jest.Mock).mockRejectedValue(
+ new Error("DB error"),
+ );
+
+ const result = await updateQuestion({
+ id: "q-1",
+ question: "Updated?",
+ });
+
+ expect(result).toEqual({ success: false, message: "DB error" });
+ });
+ });
+
+ // deleteQuestion
+ describe("deleteQuestion", () => {
+ it("should delete a question", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.question.delete as jest.Mock).mockResolvedValue(mockQuestion);
+
+ const result = await deleteQuestion("q-1");
+
+ expect(result).toEqual({ success: true });
+ expect(prisma.question.delete).toHaveBeenCalledWith({
+ where: { id: "q-1", createdBy: mockUser.id },
+ });
+ });
+
+ it("should return error for unauthenticated user", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(null);
+
+ const result = await deleteQuestion("q-1");
+
+ expect(result).toEqual({
+ success: false,
+ message: "Not authenticated",
+ });
+ expect(prisma.question.delete).not.toHaveBeenCalled();
+ });
+
+ it("should handle database errors", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.question.delete as jest.Mock).mockRejectedValue(
+ new Error("DB error"),
+ );
+
+ const result = await deleteQuestion("q-1");
+
+ expect(result).toEqual({ success: false, message: "DB error" });
+ });
+ });
+
+ // getTagsWithQuestionCounts
+ describe("getTagsWithQuestionCounts", () => {
+ it("should return tags with question counts", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ const mockTags = [
+ {
+ id: "tag-1",
+ label: "React",
+ value: "react",
+ _count: { questions: 5 },
+ },
+ {
+ id: "tag-2",
+ label: "TypeScript",
+ value: "typescript",
+ _count: { questions: 3 },
+ },
+ ];
+ (prisma.tag.findMany as jest.Mock).mockResolvedValue(mockTags);
+ (prisma.question.count as jest.Mock).mockResolvedValue(8);
+
+ const result = await getTagsWithQuestionCounts();
+
+ expect(result).toEqual({
+ success: true,
+ data: [
+ { id: "tag-1", label: "React", value: "react", questionCount: 5 },
+ {
+ id: "tag-2",
+ label: "TypeScript",
+ value: "typescript",
+ questionCount: 3,
+ },
+ ],
+ totalQuestions: 8,
+ });
+ });
+
+ it("should filter out tags with zero questions", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ const mockTags = [
+ {
+ id: "tag-1",
+ label: "React",
+ value: "react",
+ _count: { questions: 2 },
+ },
+ {
+ id: "tag-2",
+ label: "Unused",
+ value: "unused",
+ _count: { questions: 0 },
+ },
+ ];
+ (prisma.tag.findMany as jest.Mock).mockResolvedValue(mockTags);
+ (prisma.question.count as jest.Mock).mockResolvedValue(2);
+
+ const result = await getTagsWithQuestionCounts();
+
+ expect(result?.data).toHaveLength(1);
+ expect(result?.data[0].label).toBe("React");
+ });
+
+ it("should return error for unauthenticated user", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(null);
+
+ const result = await getTagsWithQuestionCounts();
+
+ expect(result).toEqual({
+ success: false,
+ message: "Not authenticated",
+ });
+ expect(prisma.tag.findMany).not.toHaveBeenCalled();
+ });
+
+ it("should handle database errors", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.tag.findMany as jest.Mock).mockRejectedValue(
+ new Error("DB error"),
+ );
+
+ const result = await getTagsWithQuestionCounts();
+
+ expect(result).toEqual({ success: false, message: "DB error" });
+ });
+ });
+});