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.

job-action-tests

Khuram Niaz4cc422d821ef
diff --git a/__tests__/company.actions.spec.ts b/__tests__/company.actions.spec.ts
index d42dccd..8d7154e 100644
--- a/__tests__/company.actions.spec.ts
+++ b/__tests__/company.actions.spec.ts
@@ -7,8 +7,8 @@ import {
} from "@/actions/company.actions";
import { getCurrentUser } from "@/utils/user.utils";
import { revalidatePath } from "next/cache";
+import { PrismaClient } from "@prisma/client";
-const { PrismaClient } = require("@prisma/client");
const prisma = new PrismaClient();
// Mock the Prisma Client
@@ -72,9 +72,10 @@ describe("Company Actions", () => {
it("should throw an error for unauthenticated user", async () => {
(getCurrentUser as jest.Mock).mockResolvedValue(null);
- await expect(getCompanyList(1, 10)).rejects.toThrow(
- "Failed to fetch company list. "
- );
+ await expect(getCompanyList(1, 10)).resolves.toStrictEqual({
+ success: false,
+ message: "Not authenticated",
+ });
expect(prisma.company.findMany).not.toHaveBeenCalled();
expect(prisma.company.count).not.toHaveBeenCalled();
@@ -131,9 +132,10 @@ describe("Company Actions", () => {
new Error("Database error")
);
- await expect(getCompanyList(1, 10)).rejects.toThrow(
- "Failed to fetch company list. "
- );
+ await expect(getCompanyList(1, 10)).resolves.toStrictEqual({
+ success: false,
+ message: "Database error",
+ });
expect(prisma.company.findMany).not.toHaveBeenCalled();
expect(prisma.company.count).not.toHaveBeenCalled();
@@ -161,23 +163,23 @@ describe("Company Actions", () => {
it("should throw an error for unauthenticated user", async () => {
(getCurrentUser as jest.Mock).mockResolvedValue(null);
- await expect(getAllCompanies()).rejects.toThrow(
- "Failed to fetch all companies. "
- );
+ await expect(getAllCompanies()).resolves.toStrictEqual({
+ success: false,
+ message: "Not authenticated",
+ });
expect(prisma.company.findMany).not.toHaveBeenCalled();
});
- it("should handle errors", async () => {
+ it("should handle unexpected errors", async () => {
(getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
(prisma.company.findMany as jest.Mock).mockRejectedValue(
- new Error("Database error")
+ new Error("Unexpected error")
);
- await expect(getAllCompanies()).rejects.toThrow(
- "Failed to fetch all companies. "
- );
+ const result = await getAllCompanies();
+ expect(result).toEqual({ success: false, message: "Unexpected error" });
expect(prisma.company.findMany).toHaveBeenCalledWith({
where: { createdBy: mockUser.id },
});
@@ -381,9 +383,10 @@ describe("Company Actions", () => {
});
it("should throw error when companyId is not provided", async () => {
- await expect(getCompanyById("")).rejects.toThrow(
- "Failed to fetch company by Id. "
- );
+ await expect(getCompanyById("")).resolves.toStrictEqual({
+ success: false,
+ message: "Please provide company id",
+ });
expect(prisma.company.findUnique).not.toHaveBeenCalled();
});
@@ -391,11 +394,28 @@ describe("Company Actions", () => {
it("should throw error when user is not authenticated", async () => {
(getCurrentUser as jest.Mock).mockResolvedValue(null);
- await expect(getCompanyById(mockCompanyId)).rejects.toThrow(
- "Failed to fetch company by Id. "
- );
+ await expect(getCompanyById(mockCompanyId)).resolves.toStrictEqual({
+ success: false,
+ message: "Not authenticated",
+ });
expect(prisma.company.findUnique).not.toHaveBeenCalled();
});
+
+ it("should handle unexpected errors", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.company.findUnique as jest.Mock).mockRejectedValue(
+ new Error("Unexpected error")
+ );
+
+ await expect(getCompanyById(mockCompanyId)).resolves.toStrictEqual({
+ success: false,
+ message: "Unexpected error",
+ });
+
+ expect(prisma.company.findUnique).toHaveBeenCalledWith({
+ where: { id: mockCompanyId },
+ });
+ });
});
});
diff --git a/__tests__/job.actions.spec.ts b/__tests__/job.actions.spec.ts
new file mode 100644
index 0000000..5f3e934
--- /dev/null
+++ b/__tests__/job.actions.spec.ts
@@ -0,0 +1,197 @@
+import {
+ getJobDetails,
+ getJobsList,
+ getJobSourceList,
+ getStatusList,
+} from "@/actions/job.actions";
+import { getMockJobDetails, getMockJobsList } from "@/lib/mock.utils";
+import { getCurrentUser } from "@/utils/user.utils";
+import { PrismaClient } from "@prisma/client";
+const prisma = new PrismaClient();
+
+// Mock the Prisma Client
+jest.mock("@prisma/client", () => {
+ const mPrismaClient = {
+ jobStatus: {
+ findMany: jest.fn(),
+ create: jest.fn(),
+ update: jest.fn(),
+ },
+ jobSource: {
+ findMany: jest.fn(),
+ },
+ job: {
+ findMany: jest.fn(),
+ findUnique: jest.fn(),
+ count: jest.fn(),
+ },
+ };
+ return { PrismaClient: jest.fn(() => mPrismaClient) };
+});
+
+jest.mock("@/utils/user.utils", () => ({
+ getCurrentUser: jest.fn(),
+}));
+
+describe("getStatusList", () => {
+ const mockUser = { id: "user-id" };
+ beforeEach(() => {
+ jest.clearAllMocks();
+ });
+ describe("getStatusList", () => {
+ it("should return status list on successful query", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+
+ const mockStatuses = [
+ { id: "1", label: "Pending", value: "pending" },
+ { id: "2", label: "Processing", value: "processing" },
+ ];
+ (prisma.jobStatus.findMany as jest.Mock).mockResolvedValue(mockStatuses);
+
+ const result = await getStatusList();
+ expect(result).toEqual(mockStatuses);
+ });
+
+ it("should throw error on failure", async () => {
+ const mockErrorResponse = {
+ success: false,
+ message: "Failed to fetch status list.",
+ };
+ (prisma.jobStatus.findMany as jest.Mock).mockRejectedValue(
+ new Error("Failed to fetch status list.")
+ );
+
+ await expect(getStatusList()).resolves.toStrictEqual(mockErrorResponse);
+ });
+ });
+ describe("getJobSourceList", () => {
+ it("should return job source list on successful query", async () => {
+ const mockData = [
+ { id: "1", label: "Source 1", value: "source1" },
+ { id: "2", label: "Source 2", value: "source2" },
+ ];
+ (prisma.jobSource.findMany as jest.Mock).mockResolvedValue(mockData);
+
+ const result = await getJobSourceList();
+
+ expect(result).toEqual(mockData);
+ expect(prisma.jobSource.findMany).toHaveBeenCalledTimes(1);
+ });
+
+ it("should returns failure response on error", async () => {
+ (prisma.jobSource.findMany as jest.Mock).mockRejectedValue(
+ new Error("Failed to fetch job source list.")
+ );
+
+ const result = await getJobSourceList();
+
+ expect(result).toEqual({
+ success: false,
+ message: "Failed to fetch job source list.",
+ });
+
+ expect(prisma.jobSource.findMany).toHaveBeenCalledTimes(1);
+ });
+ });
+
+ describe("getJobsList", () => {
+ it("should retrieve jobs with default parameters", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ const { data, total } = await getMockJobsList(1, 10);
+ (prisma.job.findMany as jest.Mock).mockResolvedValue(data);
+ (prisma.job.count as jest.Mock).mockResolvedValue(total);
+
+ const result = await getJobsList();
+
+ expect(result).toEqual({
+ success: true,
+ data,
+ total,
+ });
+ expect(prisma.job.findMany).toHaveBeenCalledTimes(1);
+ expect(prisma.job.count).toHaveBeenCalledTimes(1);
+ });
+ it("should return error when fetching data fails", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+
+ (prisma.job.findMany as jest.Mock).mockRejectedValue(
+ new Error("Database error")
+ );
+
+ const result = await getJobsList();
+
+ expect(result).toEqual({
+ success: false,
+ message: "Database error",
+ });
+ });
+ it("should return error when user is not authenticated", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(null);
+
+ const result = await getJobsList();
+
+ expect(result).toEqual({
+ success: false,
+ message: "Not authenticated",
+ });
+ });
+ });
+ describe("getJobDetails", () => {
+ it("should throw error when jobId is not provided", async () => {
+ await expect(getJobDetails("")).resolves.toStrictEqual({
+ success: false,
+ message: "Please provide job id",
+ });
+ });
+ it("should throw error when user is not authenticated", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(null);
+
+ await expect(getJobDetails("job123")).resolves.toStrictEqual({
+ success: false,
+ message: "Not authenticated",
+ });
+ });
+ });
+ it("should return job details on successful query", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ const mockJob = await getMockJobDetails("2");
+ (prisma.job.findUnique as jest.Mock).mockResolvedValue(mockJob);
+
+ const result = await getJobDetails("2");
+
+ expect(result).toStrictEqual({ job: mockJob, success: true });
+ expect(prisma.job.findUnique).toHaveBeenCalledWith({
+ where: {
+ id: "2",
+ },
+ include: {
+ JobSource: true,
+ JobTitle: true,
+ Company: true,
+ Status: true,
+ Location: true,
+ },
+ });
+ });
+
+ it("should handle unexpected errors", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue({ id: "user123" });
+
+ (prisma.job.findUnique as jest.Mock).mockRejectedValue(
+ new Error("Unexpected error")
+ );
+
+ await expect(getJobDetails("job123")).resolves.toStrictEqual({
+ success: false,
+ message: "Unexpected error",
+ });
+ });
+ test("getJobDetails: should throw error when user is not authenticated", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(null);
+
+ await expect(getJobDetails("job123")).resolves.toStrictEqual({
+ success: false,
+ message: "Not authenticated",
+ });
+ });
+});
diff --git a/src/actions/company.actions.ts b/src/actions/company.actions.ts
index 488cf3e..41e1d4d 100644
--- a/src/actions/company.actions.ts
+++ b/src/actions/company.actions.ts
@@ -62,7 +62,9 @@ export const getCompanyList = async (
} catch (error) {
const msg = "Failed to fetch company list. ";
console.error(msg, error);
- throw new Error(msg);
+ if (error instanceof Error) {
+ return { success: false, message: error.message };
+ }
}
};
@@ -83,7 +85,9 @@ export const getAllCompanies = async (): Promise<any | undefined> => {
} catch (error) {
const msg = "Failed to fetch all companies. ";
console.error(msg, error);
- throw new Error(msg);
+ if (error instanceof Error) {
+ return { success: false, message: error.message };
+ }
}
};
@@ -192,7 +196,7 @@ export const getCompanyById = async (
throw new Error("Not authenticated");
}
- const company = prisma.company.findUnique({
+ const company = await prisma.company.findUnique({
where: {
id: companyId,
},
@@ -200,7 +204,9 @@ export const getCompanyById = async (
return company;
} catch (error) {
const msg = "Failed to fetch company by Id. ";
- console.error(msg, error);
- throw new Error(msg);
+ console.error(msg);
+ if (error instanceof Error) {
+ return { success: false, message: error.message };
+ }
}
};
diff --git a/src/actions/job.actions.ts b/src/actions/job.actions.ts
index d1add4e..9b23a6e 100644
--- a/src/actions/job.actions.ts
+++ b/src/actions/job.actions.ts
@@ -12,7 +12,9 @@ export const getStatusList = async (): Promise<any | undefined> => {
} catch (error) {
const msg = "Failed to fetch status list. ";
console.error(msg, error);
- throw new Error(msg);
+ if (error instanceof Error) {
+ return { success: false, message: error.message };
+ }
}
};
@@ -23,7 +25,9 @@ export const getJobSourceList = async (): Promise<any | undefined> => {
} catch (error) {
const msg = "Failed to fetch job source list. ";
console.error(msg, error);
- throw new Error(msg);
+ if (error instanceof Error) {
+ return { success: false, message: error.message };
+ }
}
};
@@ -78,11 +82,13 @@ export const getJobsList = async (
},
}),
]);
- return { data, total };
+ return { success: true, data, total };
} catch (error) {
const msg = "Failed to fetch jobs list. ";
console.error(msg, error);
- throw new Error(msg);
+ if (error instanceof Error) {
+ return { success: false, message: error.message };
+ }
}
};
@@ -99,7 +105,7 @@ export const getJobDetails = async (
throw new Error("Not authenticated");
}
- const job = prisma.job.findUnique({
+ const job = await prisma.job.findUnique({
where: {
id: jobId,
},
@@ -111,11 +117,13 @@ export const getJobDetails = async (
Location: true,
},
});
- return job;
+ return { job, success: true };
} catch (error) {
const msg = "Failed to fetch job details. ";
console.error(msg, error);
- throw new Error(msg);
+ if (error instanceof Error) {
+ return { success: false, message: error.message };
+ }
}
};
diff --git a/src/components/myjobs/JobsContainer.tsx b/src/components/myjobs/JobsContainer.tsx
index 841d38f..ed0da24 100644
--- a/src/components/myjobs/JobsContainer.tsx
+++ b/src/components/myjobs/JobsContainer.tsx
@@ -83,7 +83,19 @@ function JobsContainer({
const loadJobs = useCallback(
async (page: number, filter?: string) => {
setLoading(true);
- const { data, total } = await getJobsList(page, jobsPerPage, filter);
+ const { success, data, total, message } = await getJobsList(
+ page,
+ jobsPerPage,
+ filter
+ );
+ if (!success) {
+ toast({
+ variant: "destructive",
+ title: "Error!",
+ description: message,
+ });
+ return;
+ }
setJobs(data);
setTotalJobs(total);
if (data) {
@@ -109,7 +121,15 @@ function JobsContainer({
};
const onEditJob = async (jobId: string) => {
- const job = await getJobDetails(jobId);
+ const { job, success, message } = await getJobDetails(jobId);
+ if (!success) {
+ toast({
+ variant: "destructive",
+ title: "Error!",
+ description: message,
+ });
+ return;
+ }
setEditJob(job);
};