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.

test(profile): cover contact info actions and form schema

Khuram Niaz091f8fb789ac
diff --git a/__tests__/addContactInfoForm.schema.spec.ts b/__tests__/addContactInfoForm.schema.spec.ts
new file mode 100644
index 0000000..dec1fd3
--- /dev/null
+++ b/__tests__/addContactInfoForm.schema.spec.ts
@@ -0,0 +1,140 @@
+import { AddContactInfoFormSchema } from "@/models/addContactInfoForm.schema";
+
+const valid = {
+ resumeId: "resume-1",
+ firstName: "Ada",
+ lastName: "Lovelace",
+ headline: "Software Engineer",
+ email: "[email protected]",
+ phone: "555-0100",
+};
+
+const errorFor = (data: unknown, path: string) => {
+ const result = AddContactInfoFormSchema.safeParse(data);
+ return result.success
+ ? undefined
+ : result.error.issues.find((i) => i.path[0] === path)?.message;
+};
+
+describe("AddContactInfoFormSchema", () => {
+ describe("valid data", () => {
+ it("accepts the required fields on their own", () => {
+ const result = AddContactInfoFormSchema.parse(valid);
+ expect(result.firstName).toBe("Ada");
+ expect(result.email).toBe("[email protected]");
+ });
+
+ it("accepts both optional links with labels", () => {
+ const result = AddContactInfoFormSchema.parse({
+ ...valid,
+ address: "London",
+ url1: "https://linkedin.com/in/ada",
+ url1Label: "LinkedIn",
+ url2: "http://github.com/ada",
+ url2Label: "GitHub",
+ });
+ expect(result.url1).toBe("https://linkedin.com/in/ada");
+ expect(result.url2).toBe("http://github.com/ada");
+ });
+
+ it("treats links, labels and address as optional", () => {
+ const result = AddContactInfoFormSchema.parse(valid);
+ expect(result.address).toBeUndefined();
+ expect(result.url1).toBeUndefined();
+ expect(result.url2Label).toBeUndefined();
+ });
+
+ it("accepts empty strings for the links, which the actions store as null", () => {
+ const result = AddContactInfoFormSchema.safeParse({
+ ...valid,
+ url1: "",
+ url1Label: "",
+ url2: "",
+ url2Label: "",
+ });
+ expect(result.success).toBe(true);
+ });
+ });
+
+ describe("name and headline", () => {
+ it("rejects a first name shorter than 2 characters", () => {
+ expect(errorFor({ ...valid, firstName: "A" }, "firstName")).toBe(
+ "First name must be at least 2 characters.",
+ );
+ });
+
+ it("rejects a last name shorter than 2 characters", () => {
+ expect(errorFor({ ...valid, lastName: "L" }, "lastName")).toBe(
+ "Last name must be at least 2 characters.",
+ );
+ });
+
+ it("rejects a headline shorter than 2 characters", () => {
+ expect(errorFor({ ...valid, headline: "E" }, "headline")).toBe(
+ "Headline must be at least 2 characters.",
+ );
+ });
+
+ it("rejects a missing first name", () => {
+ const { firstName, ...withoutFirstName } = valid;
+ expect(
+ AddContactInfoFormSchema.safeParse(withoutFirstName).success,
+ ).toBe(false);
+ });
+ });
+
+ describe("email", () => {
+ it("rejects a malformed address", () => {
+ expect(errorFor({ ...valid, email: "not-an-email" }, "email")).toBe(
+ "Please enter a valid email!",
+ );
+ });
+
+ it("rejects a missing address", () => {
+ const { email, ...withoutEmail } = valid;
+ expect(AddContactInfoFormSchema.safeParse(withoutEmail).success).toBe(
+ false,
+ );
+ });
+ });
+
+ describe("phone", () => {
+ it("rejects a missing phone", () => {
+ const { phone, ...withoutPhone } = valid;
+ expect(AddContactInfoFormSchema.safeParse(withoutPhone).success).toBe(
+ false,
+ );
+ });
+
+ it("accepts any non-empty format, since numbers vary by country", () => {
+ expect(
+ AddContactInfoFormSchema.safeParse({ ...valid, phone: "+44 20 7946" })
+ .success,
+ ).toBe(true);
+ });
+ });
+
+ describe("link URLs", () => {
+ it("rejects a first link without a scheme", () => {
+ expect(errorFor({ ...valid, url1: "linkedin.com/in/ada" }, "url1")).toBe(
+ "URL must start with http:// or https://",
+ );
+ });
+
+ it("rejects a second link without a scheme", () => {
+ expect(errorFor({ ...valid, url2: "github.com/ada" }, "url2")).toBe(
+ "URL must start with http:// or https://",
+ );
+ });
+
+ it("accepts either scheme, case-insensitively", () => {
+ expect(
+ AddContactInfoFormSchema.safeParse({
+ ...valid,
+ url1: "HTTPS://example.com",
+ url2: "Http://example.com",
+ }).success,
+ ).toBe(true);
+ });
+ });
+});
diff --git a/__tests__/contactInfo.actions.spec.ts b/__tests__/contactInfo.actions.spec.ts
new file mode 100644
index 0000000..101c801
--- /dev/null
+++ b/__tests__/contactInfo.actions.spec.ts
@@ -0,0 +1,151 @@
+import { addContactInfo, updateContactInfo } from "@/actions/profile.actions";
+import { getCurrentUser } from "@/utils/user.utils";
+import { PrismaClient } from "@prisma/client";
+
+const prisma = new PrismaClient();
+
+vi.mock("@prisma/client", () => {
+ const mPrismaClient = {
+ resume: { update: vi.fn() },
+ contactInfo: { update: vi.fn() },
+ };
+ return {
+ PrismaClient: vi.fn(function () {
+ return mPrismaClient;
+ }),
+ };
+});
+
+vi.mock("@/utils/user.utils", () => ({ getCurrentUser: vi.fn() }));
+vi.mock("next/cache", () => ({ revalidatePath: vi.fn() }));
+
+const baseForm = {
+ resumeId: "resume-1",
+ firstName: "Ada",
+ lastName: "Lovelace",
+ headline: "Engineer",
+ email: "[email protected]",
+ phone: "555-0100",
+ address: "London",
+ url1: "https://linkedin.com/in/ada",
+ url1Label: "LinkedIn",
+ url2: "",
+ url2Label: "",
+};
+
+describe("addContactInfo", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ (getCurrentUser as any).mockResolvedValue({ id: "user-1" });
+ });
+
+ it("scopes the write to a resume the user owns", async () => {
+ (prisma.resume.update as any).mockResolvedValue({ id: "resume-1" });
+
+ const result = await addContactInfo(baseForm as any);
+
+ expect(result.success).toBe(true);
+ expect((prisma.resume.update as any).mock.calls[0][0].where).toEqual({
+ id: "resume-1",
+ profile: { userId: "user-1" },
+ });
+ });
+
+ it("connectOrCreates on the resume so a second save does not duplicate", async () => {
+ (prisma.resume.update as any).mockResolvedValue({ id: "resume-1" });
+
+ await addContactInfo(baseForm as any);
+
+ const { ContactInfo } = (prisma.resume.update as any).mock.calls[0][0].data;
+ expect(ContactInfo.connectOrCreate.where).toEqual({ resumeId: "resume-1" });
+ expect(ContactInfo.connectOrCreate.create).toEqual(
+ expect.objectContaining({
+ firstName: "Ada",
+ lastName: "Lovelace",
+ email: "[email protected]",
+ phone: "555-0100",
+ }),
+ );
+ });
+
+ it("stores blank links as null rather than empty strings", async () => {
+ (prisma.resume.update as any).mockResolvedValue({ id: "resume-1" });
+
+ await addContactInfo(baseForm as any);
+
+ const { create } = (prisma.resume.update as any).mock.calls[0][0].data
+ .ContactInfo.connectOrCreate;
+ expect(create.url1).toBe("https://linkedin.com/in/ada");
+ expect(create.url1Label).toBe("LinkedIn");
+ expect(create.url2).toBeNull();
+ expect(create.url2Label).toBeNull();
+ });
+
+ it("fails without writing when unauthenticated", async () => {
+ (getCurrentUser as any).mockResolvedValue(null);
+
+ const result = await addContactInfo(baseForm as any);
+
+ expect(result).toEqual({ success: false, message: "Not authenticated" });
+ expect(prisma.resume.update).not.toHaveBeenCalled();
+ });
+
+ it("returns a failure result when the resume is not owned", async () => {
+ (prisma.resume.update as any).mockRejectedValue(
+ new Error("Record to update not found."),
+ );
+
+ const result = await addContactInfo(baseForm as any);
+
+ expect(result.success).toBe(false);
+ });
+});
+
+describe("updateContactInfo", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ (getCurrentUser as any).mockResolvedValue({ id: "user-1" });
+ });
+
+ it("scopes the update through the resume ownership chain", async () => {
+ (prisma.contactInfo.update as any).mockResolvedValue({ id: "contact-1" });
+
+ const result = await updateContactInfo({
+ ...baseForm,
+ id: "contact-1",
+ } as any);
+
+ expect(result.success).toBe(true);
+ expect((prisma.contactInfo.update as any).mock.calls[0][0].where).toEqual({
+ id: "contact-1",
+ resume: { profile: { userId: "user-1" } },
+ });
+ });
+
+ it("clears a removed second link back to null", async () => {
+ (prisma.contactInfo.update as any).mockResolvedValue({ id: "contact-1" });
+
+ await updateContactInfo({
+ ...baseForm,
+ id: "contact-1",
+ url2: "",
+ url2Label: "",
+ } as any);
+
+ const { data } = (prisma.contactInfo.update as any).mock.calls[0][0];
+ expect(data.url2).toBeNull();
+ expect(data.url2Label).toBeNull();
+ });
+
+ it("fails without writing when unauthenticated", async () => {
+ (getCurrentUser as any).mockResolvedValue(null);
+
+ const result = await updateContactInfo({
+ ...baseForm,
+ id: "contact-1",
+ } as any);
+
+ expect(result).toEqual({ success: false, message: "Not authenticated" });
+ expect(prisma.contactInfo.update).not.toHaveBeenCalled();
+ });
+});