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.

Add more job actions tests

Khuram Niaz186a88b132c0
diff --git a/__tests__/job.actions.spec.ts b/__tests__/job.actions.spec.ts
index 5f3e934..5081ad2 100644
--- a/__tests__/job.actions.spec.ts
+++ b/__tests__/job.actions.spec.ts
@@ -1,8 +1,13 @@
import {
+ addJob,
+ createLocation,
+ deleteJobById,
getJobDetails,
getJobsList,
getJobSourceList,
getStatusList,
+ updateJob,
+ updateJobStatus,
} from "@/actions/job.actions";
import { getMockJobDetails, getMockJobsList } from "@/lib/mock.utils";
import { getCurrentUser } from "@/utils/user.utils";
@@ -24,6 +29,14 @@ jest.mock("@prisma/client", () => {
findMany: jest.fn(),
findUnique: jest.fn(),
count: jest.fn(),
+ create: jest.fn(),
+ update: jest.fn(),
+ delete: jest.fn(),
+ },
+ location: {
+ findMany: jest.fn(),
+ create: jest.fn(),
+ update: jest.fn(),
},
};
return { PrismaClient: jest.fn(() => mPrismaClient) };
@@ -33,8 +46,25 @@ jest.mock("@/utils/user.utils", () => ({
getCurrentUser: jest.fn(),
}));
-describe("getStatusList", () => {
+describe("jobActions", () => {
const mockUser = { id: "user-id" };
+ const jobData = {
+ id: "job-id",
+ title: "job-title-id",
+ company: "company-id",
+ location: "location-id",
+ type: "FT",
+ status: "status-id",
+ source: "source-id",
+ salaryRange: "$50,000 - $70,000",
+ createdAt: expect.any(Date),
+ dueDate: new Date("2023-01-01"),
+ dateApplied: new Date("2022-12-31"),
+ jobDescription: "Job description",
+ jobUrl: "https://example.com/job",
+ applied: true,
+ userId: mockUser.id,
+ };
beforeEach(() => {
jest.clearAllMocks();
});
@@ -186,7 +216,7 @@ describe("getStatusList", () => {
message: "Unexpected error",
});
});
- test("getJobDetails: should throw error when user is not authenticated", async () => {
+ it("should throw error when user is not authenticated", async () => {
(getCurrentUser as jest.Mock).mockResolvedValue(null);
await expect(getJobDetails("job123")).resolves.toStrictEqual({
@@ -194,4 +224,281 @@ describe("getStatusList", () => {
message: "Not authenticated",
});
});
+ describe("createLocation", () => {
+ it("should throw error when user is not authenticated", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(null);
+
+ await expect(createLocation("location-name")).resolves.toStrictEqual({
+ success: false,
+ message: "Not authenticated",
+ });
+ });
+ it("should throw error when location name is not provided or empty", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ await expect(createLocation(" ")).resolves.toStrictEqual({
+ success: false,
+ message: "Please provide location name",
+ });
+ });
+ it("should create with valid input", async () => {
+ const label = "New Location";
+ const mockLocation = {
+ label: "New Location",
+ value: "new location",
+ createdBy: mockUser.id,
+ };
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+
+ (prisma.location.create as jest.Mock).mockResolvedValue(mockLocation);
+
+ const result = await createLocation(label);
+
+ expect(prisma.location.create).toHaveBeenCalledTimes(1);
+ expect(prisma.location.create).toHaveBeenCalledWith({
+ data: mockLocation,
+ });
+ expect(result).toStrictEqual({
+ data: {
+ ...mockLocation,
+ },
+ success: true,
+ });
+ });
+ it("should handle unexpected errors", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+
+ (prisma.location.create as jest.Mock).mockRejectedValue(
+ new Error("Unexpected error")
+ );
+
+ await expect(createLocation("location-name")).resolves.toStrictEqual({
+ success: false,
+ message: "Unexpected error",
+ });
+ });
+ });
+ describe("addJob", () => {
+ it("should create a new job successfully", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.job.create as jest.Mock).mockResolvedValue(jobData);
+
+ const result = await addJob(jobData);
+
+ expect(result).toStrictEqual({ job: jobData, success: true });
+ expect(prisma.job.create).toHaveBeenCalledTimes(1);
+ expect(prisma.job.create).toHaveBeenCalledWith({
+ data: {
+ jobTitleId: jobData.title,
+ companyId: jobData.company,
+ locationId: jobData.location,
+ statusId: jobData.status,
+ jobSourceId: jobData.source,
+ salaryRange: jobData.salaryRange,
+ createdAt: jobData.createdAt,
+ dueDate: jobData.dueDate,
+ appliedDate: jobData.dateApplied,
+ description: jobData.jobDescription,
+ jobType: jobData.type,
+ userId: mockUser.id,
+ jobUrl: jobData.jobUrl,
+ applied: jobData.applied,
+ },
+ });
+ });
+ it("should handle undefined values for optional fields", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.job.create as jest.Mock).mockResolvedValue(jobData);
+
+ const result = await addJob({
+ ...jobData,
+ jobUrl: undefined,
+ dateApplied: undefined,
+ });
+
+ expect(prisma.job.create).toHaveBeenCalledTimes(1);
+ expect(prisma.job.create).toHaveBeenCalledWith({
+ data: {
+ jobTitleId: jobData.title,
+ companyId: jobData.company,
+ locationId: jobData.location,
+ statusId: jobData.status,
+ jobSourceId: jobData.source,
+ salaryRange: jobData.salaryRange,
+ createdAt: jobData.createdAt,
+ dueDate: jobData.dueDate,
+ description: jobData.jobDescription,
+ jobType: jobData.type,
+ userId: mockUser.id,
+ applied: jobData.applied,
+ },
+ });
+ expect(result).toEqual({ job: jobData, success: true });
+ });
+ it("should handle unexpected errors", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+
+ (prisma.job.create as jest.Mock).mockRejectedValue(
+ new Error("Unexpected error")
+ );
+
+ await expect(addJob(jobData)).resolves.toStrictEqual({
+ success: false,
+ message: "Unexpected error",
+ });
+ });
+ it("should throw error when user is not authenticated", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(null);
+
+ await expect(addJob(jobData)).resolves.toStrictEqual({
+ success: false,
+ message: "Not authenticated",
+ });
+ });
+ });
+ describe("updateJob", () => {
+ it("should update a job successfully", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.job.update as jest.Mock).mockResolvedValue(jobData);
+
+ const result = await updateJob(jobData);
+
+ expect(result).toStrictEqual({ job: jobData, success: true });
+ expect(prisma.job.update).toHaveBeenCalledTimes(1);
+ });
+ it("should handle unexpected errors", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+
+ (prisma.job.update as jest.Mock).mockRejectedValue(
+ new Error("Unexpected error")
+ );
+
+ await expect(updateJob(jobData)).resolves.toStrictEqual({
+ success: false,
+ message: "Unexpected error",
+ });
+ });
+ it("should throw error when user is not authenticated", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(null);
+
+ await expect(updateJob(jobData)).resolves.toStrictEqual({
+ success: false,
+ message: "Not authenticated",
+ });
+ });
+
+ it("should return an error if the id is not provided or no user privileges", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+
+ await expect(
+ updateJob({ ...jobData, id: undefined })
+ ).resolves.toStrictEqual({
+ success: false,
+ message: "Id is not provide or no user privilages",
+ });
+ });
+ });
+ describe("updateJobStatus", () => {
+ const jobData = {
+ id: "job-id",
+ title: "job-title-id",
+ company: "company-id",
+ location: "location-id",
+ type: "FT",
+ status: {
+ id: "status-id",
+ label: "Applied",
+ value: "applied",
+ },
+ source: "source-id",
+ salaryRange: "$50,000 - $70,000",
+ createdAt: expect.any(Date),
+ dueDate: new Date("2023-01-01"),
+ dateApplied: new Date("2022-12-31"),
+ jobDescription: "Job description",
+ jobUrl: "https://example.com/job",
+ applied: true,
+ userId: mockUser.id,
+ };
+ it("should update a job status successfully", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.job.update as jest.Mock).mockResolvedValue(jobData);
+
+ const result = await updateJobStatus(jobData.id, jobData.status);
+
+ expect(result).toStrictEqual({ job: jobData, success: true });
+ expect(prisma.job.update).toHaveBeenCalledTimes(1);
+ expect(prisma.job.update).toHaveBeenCalledWith({
+ where: {
+ id: jobData.id,
+ userId: mockUser.id,
+ },
+ data: {
+ statusId: jobData.status.id,
+ applied: true,
+ appliedDate: expect.any(Date),
+ },
+ });
+ });
+ it("should handle unexpected errors", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+
+ (prisma.job.update as jest.Mock).mockRejectedValue(
+ new Error("Unexpected error")
+ );
+
+ await expect(
+ updateJobStatus(jobData.id, jobData.status)
+ ).resolves.toStrictEqual({
+ success: false,
+ message: "Unexpected error",
+ });
+ });
+ it("should throw error when user is not authenticated", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(null);
+
+ await expect(
+ updateJobStatus(jobData.id, jobData.status)
+ ).resolves.toStrictEqual({
+ success: false,
+ message: "Not authenticated",
+ });
+ });
+ });
+ describe("deleteJobById", () => {
+ it("should return error when user is not authenticated", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(null);
+
+ await expect(deleteJobById("job-id")).resolves.toStrictEqual({
+ success: false,
+ message: "Not authenticated",
+ });
+ });
+ it("should delete a job successfully", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.job.delete as jest.Mock).mockResolvedValue(jobData);
+
+ const result = await deleteJobById("job-id");
+
+ expect(result).toStrictEqual({ res: jobData, success: true });
+ expect(prisma.job.delete).toHaveBeenCalledTimes(1);
+ expect(prisma.job.delete).toHaveBeenCalledWith({
+ where: {
+ id: "job-id",
+ userId: mockUser.id,
+ },
+ });
+ });
+ it("should handle unexpected errors", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+
+ (prisma.job.delete as jest.Mock).mockRejectedValue(
+ new Error("Unexpected error")
+ );
+
+ await expect(deleteJobById("job-id")).resolves.toStrictEqual({
+ success: false,
+ message: "Unexpected error",
+ });
+ });
+ });
});
diff --git a/src/actions/company.actions.ts b/src/actions/company.actions.ts
index 41e1d4d..ce474f8 100644
--- a/src/actions/company.actions.ts
+++ b/src/actions/company.actions.ts
@@ -1,5 +1,6 @@
"use server";
import prisma from "@/lib/db";
+import { handleError } from "@/lib/utils";
import { AddCompanyFormSchema } from "@/models/addCompanyForm.schema";
import { getCurrentUser } from "@/utils/user.utils";
import { revalidatePath } from "next/cache";
@@ -61,10 +62,7 @@ export const getCompanyList = async (
return { data, total };
} catch (error) {
const msg = "Failed to fetch company list. ";
- console.error(msg, error);
- if (error instanceof Error) {
- return { success: false, message: error.message };
- }
+ return handleError(error, msg);
}
};
@@ -84,10 +82,7 @@ export const getAllCompanies = async (): Promise<any | undefined> => {
return comapnies;
} catch (error) {
const msg = "Failed to fetch all companies. ";
- console.error(msg, error);
- if (error instanceof Error) {
- return { success: false, message: error.message };
- }
+ return handleError(error, msg);
}
};
@@ -127,10 +122,7 @@ export const addCompany = async (
return { success: true, data: res };
} catch (error) {
const msg = "Failed to create company.";
- console.error(msg, error);
- if (error instanceof Error) {
- return { success: false, message: error.message };
- }
+ return handleError(error, msg);
}
};
@@ -176,10 +168,7 @@ export const updateCompany = async (
return { success: true, data: res };
} catch (error) {
const msg = "Failed to update company.";
- console.error(msg, error);
- if (error instanceof Error) {
- return { success: false, message: error.message };
- }
+ return handleError(error, msg);
}
};
diff --git a/src/actions/job.actions.ts b/src/actions/job.actions.ts
index 9b23a6e..fff247f 100644
--- a/src/actions/job.actions.ts
+++ b/src/actions/job.actions.ts
@@ -1,5 +1,6 @@
"use server";
import prisma from "@/lib/db";
+import { handleError } from "@/lib/utils";
import { AddJobFormSchema } from "@/models/addJobForm.schema";
import { JobStatus } from "@/models/job.model";
import { getCurrentUser } from "@/utils/user.utils";
@@ -11,10 +12,7 @@ export const getStatusList = async (): Promise<any | undefined> => {
return statuses;
} catch (error) {
const msg = "Failed to fetch status list. ";
- console.error(msg, error);
- if (error instanceof Error) {
- return { success: false, message: error.message };
- }
+ return handleError(error, msg);
}
};
@@ -24,10 +22,7 @@ export const getJobSourceList = async (): Promise<any | undefined> => {
return list;
} catch (error) {
const msg = "Failed to fetch job source list. ";
- console.error(msg, error);
- if (error instanceof Error) {
- return { success: false, message: error.message };
- }
+ return handleError(error, msg);
}
};
@@ -85,10 +80,7 @@ export const getJobsList = async (
return { success: true, data, total };
} catch (error) {
const msg = "Failed to fetch jobs list. ";
- console.error(msg, error);
- if (error instanceof Error) {
- return { success: false, message: error.message };
- }
+ return handleError(error, msg);
}
};
@@ -120,10 +112,7 @@ export const getJobDetails = async (
return { job, success: true };
} catch (error) {
const msg = "Failed to fetch job details. ";
- console.error(msg, error);
- if (error instanceof Error) {
- return { success: false, message: error.message };
- }
+ return handleError(error, msg);
}
};
@@ -139,15 +128,18 @@ export const createLocation = async (
const value = label.trim().toLowerCase();
+ if (!value) {
+ throw new Error("Please provide location name");
+ }
+
const location = await prisma.location.create({
data: { label, value, createdBy: user.id },
});
- return location;
+ return { data: location, success: true };
} catch (error) {
const msg = "Failed to create job location. ";
- console.error(msg, error);
- throw new Error(msg);
+ return handleError(error, msg);
}
};
@@ -195,11 +187,10 @@ export const addJob = async (
},
});
- return job;
+ return { job, success: true };
} catch (error) {
const msg = "Failed to create job. ";
- console.error(msg, error);
- throw new Error(msg);
+ return handleError(error, msg);
}
};
@@ -253,11 +244,10 @@ export const updateJob = async (
},
});
- return job;
+ return { job, success: true };
} catch (error) {
const msg = "Failed to update job. ";
- console.error(msg, error);
- throw new Error(msg);
+ return handleError(error, msg);
}
};
@@ -291,18 +281,17 @@ export const updateJobStatus = async (
}
};
- const job = prisma.job.update({
+ const job = await prisma.job.update({
where: {
id: jobId,
userId: user.id,
},
data: dataToUpdate(),
});
- return job;
+ return { job, success: true };
} catch (error) {
const msg = "Failed to update job status.";
- console.error(msg, error);
- throw new Error(msg);
+ return handleError(error, msg);
}
};
@@ -316,16 +305,15 @@ export const deleteJobById = async (
throw new Error("Not authenticated");
}
- const res = prisma.job.delete({
+ const res = await prisma.job.delete({
where: {
id: jobId,
userId: user.id,
},
});
- return res;
+ return { res, success: true };
} catch (error) {
const msg = "Failed to delete job.";
- console.error(msg, error);
- throw new Error(msg);
+ return handleError(error, msg);
}
};
diff --git a/src/actions/profile.actions.ts b/src/actions/profile.actions.ts
index 4d9535a..b23844c 100644
--- a/src/actions/profile.actions.ts
+++ b/src/actions/profile.actions.ts
@@ -1,5 +1,6 @@
"use server";
import prisma from "@/lib/db";
+import { handleError } from "@/lib/utils";
import { AddEducationFormSchema } from "@/models/AddEductionForm.schema";
import { AddContactInfoFormSchema } from "@/models/addContactInfoForm.schema";
import { AddExperienceFormSchema } from "@/models/addExperienceForm.schema";
@@ -46,10 +47,7 @@ export const getResumeList = async (
return { data, total, success: true };
} catch (error) {
const msg = "Failed to get resume list.";
- console.error(msg, error);
- if (error instanceof Error) {
- return { success: false, message: error.message };
- }
+ return handleError(error, msg);
}
};
@@ -94,10 +92,7 @@ export const getResumeById = async (
return resume;
} catch (error) {
const msg = "Failed to get resume.";
- console.error(msg, error);
- if (error instanceof Error) {
- return { success: false, message: error.message };
- }
+ return handleError(error, msg);
}
};
@@ -135,10 +130,7 @@ export const addContactInfo = async (
return { data: res, success: true };
} catch (error) {
const msg = "Failed to create contact info.";
- console.error(msg, error);
- if (error instanceof Error) {
- return { success: false, message: error.message };
- }
+ return handleError(error, msg);
}
};
@@ -169,10 +161,7 @@ export const updateContactInfo = async (
return { data: res, success: true };
} catch (error) {
const msg = "Failed to update contact info.";
- console.error(msg, error);
- if (error instanceof Error) {
- return { success: false, message: error.message };
- }
+ return handleError(error, msg);
}
};
@@ -218,10 +207,7 @@ export const createResumeProfile = async (
return { success: true, data: res };
} catch (error) {
const msg = "Failed to create resume.";
- console.error(msg, error);
- if (error instanceof Error) {
- return { success: false, message: error.message };
- }
+ return handleError(error, msg);
}
};
@@ -249,10 +235,7 @@ export const editResume = async (
return { success: true, data: res };
} catch (error) {
const msg = "Failed to update resume.";
- console.error(msg, error);
- if (error instanceof Error) {
- return { success: false, message: error.message };
- }
+ return handleError(error, msg);
}
};
@@ -289,10 +272,7 @@ export const addResumeSummary = async (
return { data: summary, success: true };
} catch (error) {
const msg = "Failed to create summary.";
- console.error(msg, error);
- if (error instanceof Error) {
- return { success: false, message: error.message };
- }
+ return handleError(error, msg);
}
};
@@ -330,10 +310,7 @@ export const updateResumeSummary = async (
return { data: summary, success: true };
} catch (error) {
const msg = "Failed to update summary.";
- console.error(msg, error);
- if (error instanceof Error) {
- return { success: false, message: error.message };
- }
+ return handleError(error, msg);
}
};
@@ -377,10 +354,7 @@ export const addExperience = async (
return { data: experience, success: true };
} catch (error) {
const msg = "Failed to create experience.";
- console.error(msg, error);
- if (error instanceof Error) {
- return { success: false, message: error.message };
- }
+ return handleError(error, msg);
}
};
@@ -419,10 +393,7 @@ export const updateExperience = async (
return { data: summary, success: true };
} catch (error) {
const msg = "Failed to update experience.";
- console.error(msg, error);
- if (error instanceof Error) {
- return { success: false, message: error.message };
- }
+ return handleError(error, msg);
}
};
@@ -467,10 +438,7 @@ export const addEducation = async (
return { data: education, success: true };
} catch (error) {
const msg = "Failed to create education.";
- console.error(msg, error);
- if (error instanceof Error) {
- return { success: false, message: error.message };
- }
+ return handleError(error, msg);
}
};
@@ -510,9 +478,6 @@ export const updateEducation = async (
return { data: summary, success: true };
} catch (error) {
const msg = "Failed to update education.";
- console.error(msg, error);
- if (error instanceof Error) {
- return { success: false, message: error.message };
- }
+ return handleError(error, msg);
}
};
diff --git a/src/components/ComboBox.tsx b/src/components/ComboBox.tsx
index 597c431..bb2866c 100644
--- a/src/components/ComboBox.tsx
+++ b/src/components/ComboBox.tsx
@@ -26,6 +26,7 @@ import { createLocation } from "@/actions/job.actions";
import { JobForm } from "@/models/job.model";
import { addCompany } from "@/actions/company.actions";
import { createJobTitle } from "@/actions/jobtitle.actions";
+import { toast } from "./ui/use-toast";
interface ComboboxProps {
options: any[];
@@ -52,7 +53,15 @@ export function Combobox({ options, field, creatable }: ComboboxProps) {
response = await createJobTitle(label);
break;
case "location":
- response = await createLocation(label);
+ const { data, success, message } = await createLocation(label);
+ if (!success) {
+ toast({
+ variant: "destructive",
+ title: "Error!",
+ description: message,
+ });
+ }
+ response = data;
break;
default:
break;
diff --git a/src/components/myjobs/AddJob.tsx b/src/components/myjobs/AddJob.tsx
index 4cf2765..a1c6641 100644
--- a/src/components/myjobs/AddJob.tsx
+++ b/src/components/myjobs/AddJob.tsx
@@ -108,10 +108,19 @@ export function AddJob({
function onSubmit(data: z.infer<typeof AddJobFormSchema>) {
startTransition(async () => {
- const res = editJob ? await updateJob(data) : await addJob(data);
+ const { success, message } = editJob
+ ? await updateJob(data)
+ : await addJob(data);
reset();
setDialogOpen(false);
// reloadJobs();
+ if (!success) {
+ toast({
+ variant: "destructive",
+ title: "Error!",
+ description: message,
+ });
+ }
redirect("/dashboard/myjobs");
});
toast({
diff --git a/src/components/myjobs/JobsContainer.tsx b/src/components/myjobs/JobsContainer.tsx
index ed0da24..15cfc2d 100644
--- a/src/components/myjobs/JobsContainer.tsx
+++ b/src/components/myjobs/JobsContainer.tsx
@@ -110,12 +110,18 @@ function JobsContainer({
};
const onDeleteJob = async (jobId: string) => {
- const res = await deleteJobById(jobId);
- if (res) {
+ const { res, success, message } = await deleteJobById(jobId);
+ if (success) {
toast({
variant: "success",
description: `Job has been deleted successfully`,
});
+ } else {
+ toast({
+ variant: "destructive",
+ title: "Error!",
+ description: message,
+ });
}
reloadJobs();
};
@@ -134,12 +140,18 @@ function JobsContainer({
};
const onChangeJobStatus = async (jobId: string, jobStatus: JobStatus) => {
- const job = await updateJobStatus(jobId, jobStatus);
- if (job) {
+ const { success, message } = await updateJobStatus(jobId, jobStatus);
+ if (success) {
toast({
variant: "success",
description: `Job has been updated successfully`,
});
+ } else {
+ toast({
+ variant: "destructive",
+ title: "Error!",
+ description: message,
+ });
}
reloadJobs();
};
diff --git a/src/lib/utils.ts b/src/lib/utils.ts
index c5bee11..6d3f013 100644
--- a/src/lib/utils.ts
+++ b/src/lib/utils.ts
@@ -33,3 +33,10 @@ export function formatUrl(url: string) {
}
return url;
}
+
+export function handleError(error: unknown, msg = "Server Error.") {
+ console.error(error, msg);
+ if (error instanceof Error) {
+ return { success: false, message: error.message || msg };
+ }
+}