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 unit tests for activity types

Khuram Niaz1cf0c7f408a4
diff --git a/__tests__/AdminTabsContainer.spec.tsx b/__tests__/AdminTabsContainer.spec.tsx
index 17d5f29..2d440bc 100644
--- a/__tests__/AdminTabsContainer.spec.tsx
+++ b/__tests__/AdminTabsContainer.spec.tsx
@@ -24,6 +24,11 @@ jest.mock("@/actions/jobLocation.actions", () => ({
getJobLocationsList: jest.fn().mockResolvedValue({ data: [], total: 0 }),
}));
+jest.mock("@/actions/activity.actions", () => ({
+ getActivityTypeList: jest.fn().mockResolvedValue({ data: [], total: 0 }),
+ createActivityType: jest.fn(),
+}));
+
describe("AdminTabsContainer", () => {
const user = userEvent.setup({ skipHover: true });
@@ -31,7 +36,7 @@ describe("AdminTabsContainer", () => {
jest.clearAllMocks();
});
- it("should render all three tabs", () => {
+ it("should render all tabs", () => {
render(<AdminTabsContainer />);
expect(screen.getByRole("tab", { name: "Companies" })).toBeInTheDocument();
@@ -41,6 +46,9 @@ describe("AdminTabsContainer", () => {
expect(
screen.getByRole("tab", { name: "Locations" })
).toBeInTheDocument();
+ expect(
+ screen.getByRole("tab", { name: "Activity Types" })
+ ).toBeInTheDocument();
});
it("should default to companies tab", () => {
@@ -72,6 +80,19 @@ describe("AdminTabsContainer", () => {
);
});
+ it("should switch to activity types tab and update URL", async () => {
+ render(<AdminTabsContainer />);
+
+ const activityTypesTab = screen.getByRole("tab", {
+ name: "Activity Types",
+ });
+ await user.click(activityTypesTab);
+
+ expect(mockPush).toHaveBeenCalledWith(
+ "/dashboard/admin?tab=activity-types"
+ );
+ });
+
it("should render companies tab panel content by default", async () => {
render(<AdminTabsContainer />);
diff --git a/__tests__/activityType.actions.spec.ts b/__tests__/activityType.actions.spec.ts
new file mode 100644
index 0000000..e4a4778
--- /dev/null
+++ b/__tests__/activityType.actions.spec.ts
@@ -0,0 +1,249 @@
+import {
+ getActivityTypeList,
+ deleteActivityTypeById,
+} from "@/actions/activity.actions";
+import { getCurrentUser } from "@/utils/user.utils";
+import { PrismaClient } from "@prisma/client";
+
+const prisma = new PrismaClient();
+
+jest.mock("@prisma/client", () => {
+ const mPrismaClient = {
+ activityType: {
+ findMany: jest.fn(),
+ count: jest.fn(),
+ delete: jest.fn(),
+ },
+ activity: {
+ groupBy: jest.fn(),
+ count: jest.fn(),
+ },
+ task: {
+ count: jest.fn(),
+ },
+ };
+ return { PrismaClient: jest.fn(() => mPrismaClient) };
+});
+
+jest.mock("@/utils/user.utils", () => ({
+ getCurrentUser: jest.fn(),
+}));
+
+describe("Activity Type Actions", () => {
+ const mockUser = { id: "user-id" };
+
+ const mockActivityTypes = [
+ {
+ id: "at-1",
+ label: "Learning",
+ value: "learning",
+ _count: { Activities: 5, Tasks: 2 },
+ },
+ {
+ id: "at-2",
+ label: "Job Search",
+ value: "job-search",
+ _count: { Activities: 3, Tasks: 1 },
+ },
+ {
+ id: "at-3",
+ label: "Networking",
+ value: "networking",
+ _count: { Activities: 0, Tasks: 0 },
+ },
+ ];
+
+ const mockDurationSums = [
+ { activityTypeId: "at-1", _sum: { duration: 300 } },
+ { activityTypeId: "at-2", _sum: { duration: 120 } },
+ ];
+
+ beforeEach(() => {
+ jest.clearAllMocks();
+ });
+
+ describe("getActivityTypeList", () => {
+ it("should return paginated activity types sorted by total duration", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.activityType.count as jest.Mock).mockResolvedValue(3);
+ (prisma.activity.groupBy as jest.Mock).mockResolvedValue(mockDurationSums);
+ (prisma.activityType.findMany as jest.Mock).mockResolvedValue(
+ mockActivityTypes,
+ );
+
+ const result = await getActivityTypeList(1, 10);
+
+ expect(result.total).toBe(3);
+ expect(result.data).toHaveLength(3);
+ expect(result.data[0].id).toBe("at-1");
+ expect(result.data[0].totalDuration).toBe(300);
+ expect(result.data[1].id).toBe("at-2");
+ expect(result.data[1].totalDuration).toBe(120);
+ expect(result.data[2].id).toBe("at-3");
+ expect(result.data[2].totalDuration).toBe(0);
+ });
+
+ it("should handle pagination correctly for page 2", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.activityType.count as jest.Mock).mockResolvedValue(3);
+ (prisma.activity.groupBy as jest.Mock).mockResolvedValue(mockDurationSums);
+ (prisma.activityType.findMany as jest.Mock).mockResolvedValue(
+ mockActivityTypes,
+ );
+
+ const result = await getActivityTypeList(2, 2);
+
+ expect(result.data).toHaveLength(1);
+ expect(result.data[0].id).toBe("at-3");
+ });
+
+ it("should return empty data when page exceeds total", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.activityType.count as jest.Mock).mockResolvedValue(3);
+ (prisma.activity.groupBy as jest.Mock).mockResolvedValue(mockDurationSums);
+ (prisma.activityType.findMany as jest.Mock).mockResolvedValue(
+ mockActivityTypes,
+ );
+
+ const result = await getActivityTypeList(5, 10);
+
+ expect(result.data).toHaveLength(0);
+ });
+
+ it("should assign 0 duration for types with no completed activities", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.activityType.count as jest.Mock).mockResolvedValue(3);
+ (prisma.activity.groupBy as jest.Mock).mockResolvedValue([]);
+ (prisma.activityType.findMany as jest.Mock).mockResolvedValue(
+ mockActivityTypes,
+ );
+
+ const result = await getActivityTypeList(1, 10);
+
+ result.data.forEach((at: any) => {
+ expect(at.totalDuration).toBe(0);
+ });
+ });
+
+ it("should only sum completed activities (endTime not null)", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.activityType.count as jest.Mock).mockResolvedValue(0);
+ (prisma.activity.groupBy as jest.Mock).mockResolvedValue([]);
+ (prisma.activityType.findMany as jest.Mock).mockResolvedValue([]);
+
+ await getActivityTypeList(1, 10);
+
+ expect(prisma.activity.groupBy).toHaveBeenCalledWith(
+ expect.objectContaining({
+ where: { userId: mockUser.id, endTime: { not: null } },
+ }),
+ );
+ });
+
+ it("should return error for unauthenticated user", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(null);
+
+ const result = await getActivityTypeList(1, 10);
+
+ expect(result).toEqual({ success: false, message: "Not authenticated" });
+ expect(prisma.activityType.findMany).not.toHaveBeenCalled();
+ });
+
+ it("should handle database errors", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.activityType.count as jest.Mock).mockRejectedValue(
+ new Error("DB error"),
+ );
+
+ const result = await getActivityTypeList(1, 10);
+
+ expect(result).toEqual({ success: false, message: "DB error" });
+ });
+ });
+
+ describe("deleteActivityTypeById", () => {
+ it("should delete an activity type with no linked records", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.activity.count as jest.Mock).mockResolvedValue(0);
+ (prisma.task.count as jest.Mock).mockResolvedValue(0);
+ const mockDeleted = { id: "at-3", label: "Networking", value: "networking" };
+ (prisma.activityType.delete as jest.Mock).mockResolvedValue(mockDeleted);
+
+ const result = await deleteActivityTypeById("at-3");
+
+ expect(result).toEqual({ res: mockDeleted, success: true });
+ expect(prisma.activityType.delete).toHaveBeenCalledWith({
+ where: { id: "at-3", createdBy: mockUser.id },
+ });
+ });
+
+ it("should return error when linked to activities only", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.activity.count as jest.Mock).mockResolvedValue(5);
+ (prisma.task.count as jest.Mock).mockResolvedValue(0);
+
+ const result = await deleteActivityTypeById("at-1");
+
+ expect(result).toEqual({
+ success: false,
+ message:
+ "Activity type cannot be deleted because it is linked to 5 activity(ies).",
+ });
+ expect(prisma.activityType.delete).not.toHaveBeenCalled();
+ });
+
+ it("should return error when linked to tasks only", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.activity.count as jest.Mock).mockResolvedValue(0);
+ (prisma.task.count as jest.Mock).mockResolvedValue(3);
+
+ const result = await deleteActivityTypeById("at-1");
+
+ expect(result).toEqual({
+ success: false,
+ message:
+ "Activity type cannot be deleted because it is linked to 3 task(s).",
+ });
+ expect(prisma.activityType.delete).not.toHaveBeenCalled();
+ });
+
+ it("should return error when linked to both activities and tasks", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.activity.count as jest.Mock).mockResolvedValue(5);
+ (prisma.task.count as jest.Mock).mockResolvedValue(3);
+
+ const result = await deleteActivityTypeById("at-1");
+
+ expect(result).toEqual({
+ success: false,
+ message:
+ "Activity type cannot be deleted because it is linked to 5 activity(ies) and 3 task(s).",
+ });
+ expect(prisma.activityType.delete).not.toHaveBeenCalled();
+ });
+
+ it("should return error for unauthenticated user", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(null);
+
+ const result = await deleteActivityTypeById("at-1");
+
+ expect(result).toEqual({ success: false, message: "Not authenticated" });
+ expect(prisma.activity.count).not.toHaveBeenCalled();
+ expect(prisma.task.count).not.toHaveBeenCalled();
+ expect(prisma.activityType.delete).not.toHaveBeenCalled();
+ });
+
+ it("should handle database errors during deletion", async () => {
+ (getCurrentUser as jest.Mock).mockResolvedValue(mockUser);
+ (prisma.activity.count as jest.Mock).mockResolvedValue(0);
+ (prisma.task.count as jest.Mock).mockResolvedValue(0);
+ (prisma.activityType.delete as jest.Mock).mockRejectedValue(
+ new Error("DB error"),
+ );
+
+ const result = await deleteActivityTypeById("at-1");
+
+ expect(result).toEqual({ success: false, message: "DB error" });
+ });
+ });
+});