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.

feat(profile): pair name and email/phone fields on one row

Khuram Niazae6db5d3dcd3
diff --git a/__tests__/AddContactInfo.spec.tsx b/__tests__/AddContactInfo.spec.tsx
new file mode 100644
index 0000000..c9af6a2
--- /dev/null
+++ b/__tests__/AddContactInfo.spec.tsx
@@ -0,0 +1,336 @@
+import { render, screen, waitFor } from "@testing-library/react";
+import userEvent from "@testing-library/user-event";
+import AddContactInfo from "@/components/profile/AddContactInfo";
+import { addContactInfo, updateContactInfo } from "@/actions/profile.actions";
+import { toast } from "@/components/ui/use-toast";
+
+vi.mock("@/actions/profile.actions", () => ({
+ addContactInfo: vi.fn(),
+ updateContactInfo: vi.fn(),
+}));
+
+vi.mock("@/components/ui/use-toast", () => ({ toast: vi.fn() }));
+
+const contactInfoToEdit = {
+ id: "contact-1",
+ resumeId: "resume-123",
+ firstName: "Ada",
+ lastName: "Lovelace",
+ headline: "Software Engineer",
+ email: "[email protected]",
+ phone: "555-0100",
+ address: "London",
+ url1: "https://linkedin.com/in/ada",
+ url1Label: "LinkedIn",
+ url2: null,
+ url2Label: null,
+};
+
+describe("AddContactInfo Component", () => {
+ const user = userEvent.setup();
+ const mockResumeId = "resume-123";
+ const mockSetDialogOpen = vi.fn();
+
+ beforeEach(() => {
+ vi.clearAllMocks();
+ });
+
+ const renderNew = () =>
+ render(
+ <AddContactInfo
+ resumeId={mockResumeId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ />,
+ );
+
+ const renderEdit = (overrides: Record<string, unknown> = {}) =>
+ render(
+ <AddContactInfo
+ resumeId={mockResumeId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ contactInfoToEdit={{ ...contactInfoToEdit, ...overrides } as any}
+ />,
+ );
+
+ const fillRequiredFields = async () => {
+ await user.type(screen.getByLabelText("First Name"), "Grace");
+ await user.type(screen.getByLabelText("Last Name"), "Hopper");
+ await user.type(screen.getByLabelText("Headline"), "Rear Admiral");
+ await user.type(screen.getByLabelText("Email"), "[email protected]");
+ await user.type(screen.getByLabelText("Phone"), "555-0199");
+ };
+
+ it("renders the Add title and all contact fields", async () => {
+ renderNew();
+
+ await waitFor(() => {
+ expect(screen.getByText("Add Contact Info")).toBeInTheDocument();
+ });
+ expect(screen.getByLabelText("First Name")).toBeInTheDocument();
+ expect(screen.getByLabelText("Last Name")).toBeInTheDocument();
+ expect(screen.getByLabelText("Headline")).toBeInTheDocument();
+ expect(screen.getByLabelText("Email")).toBeInTheDocument();
+ expect(screen.getByLabelText("Phone")).toBeInTheDocument();
+ expect(screen.getByLabelText("Address")).toBeInTheDocument();
+ });
+
+ it("renders the Edit title and populates the fields", async () => {
+ renderEdit();
+
+ await waitFor(() => {
+ expect(screen.getByText("Edit Contact Info")).toBeInTheDocument();
+ });
+ expect((screen.getByLabelText("First Name") as HTMLInputElement).value).toBe(
+ "Ada",
+ );
+ expect((screen.getByLabelText("Last Name") as HTMLInputElement).value).toBe(
+ "Lovelace",
+ );
+ expect((screen.getByLabelText("Email") as HTMLInputElement).value).toBe(
+ );
+ expect((screen.getByLabelText("Phone") as HTMLInputElement).value).toBe(
+ "555-0100",
+ );
+ });
+
+ it("does not render the dialog when closed", () => {
+ const { container } = render(
+ <AddContactInfo
+ resumeId={mockResumeId}
+ dialogOpen={false}
+ setDialogOpen={mockSetDialogOpen}
+ />,
+ );
+
+ expect(screen.queryByText("Add Contact Info")).not.toBeInTheDocument();
+ expect(container.querySelector("form")).not.toBeInTheDocument();
+ });
+
+ it("keeps Save disabled until the form is edited", async () => {
+ renderNew();
+
+ await waitFor(() => {
+ expect(screen.getByRole("button", { name: /save/i })).toBeDisabled();
+ });
+
+ await user.type(screen.getByLabelText("First Name"), "G");
+
+ await waitFor(() => {
+ expect(screen.getByRole("button", { name: /save/i })).not.toBeDisabled();
+ });
+ });
+
+ it("closes the dialog when Cancel is clicked", async () => {
+ renderNew();
+
+ await waitFor(() => {
+ expect(screen.getByText("Add Contact Info")).toBeInTheDocument();
+ });
+ await user.click(screen.getByRole("button", { name: /cancel/i }));
+
+ expect(mockSetDialogOpen).toHaveBeenCalledWith(false);
+ });
+
+ describe("second link", () => {
+ it("is collapsed behind a toggle for a new contact", async () => {
+ renderNew();
+
+ await waitFor(() => {
+ expect(screen.getByText("Link 1")).toBeInTheDocument();
+ });
+ expect(screen.queryByText("Link 2")).not.toBeInTheDocument();
+ expect(screen.getByText("+ Add another link")).toBeInTheDocument();
+ });
+
+ it("expands when the toggle is clicked", async () => {
+ renderNew();
+
+ await user.click(await screen.findByText("+ Add another link"));
+
+ await waitFor(() => {
+ expect(screen.getByText("Link 2")).toBeInTheDocument();
+ });
+ expect(screen.queryByText("+ Add another link")).not.toBeInTheDocument();
+ });
+
+ it("starts expanded when the contact already has a second link", async () => {
+ renderEdit({ url2: "https://github.com/ada", url2Label: "GitHub" });
+
+ await waitFor(() => {
+ expect(screen.getByText("Link 2")).toBeInTheDocument();
+ });
+ expect(screen.getByDisplayValue("https://github.com/ada")).toBeInTheDocument();
+ });
+
+ it("clears the second link values when removed", async () => {
+ renderEdit({ url2: "https://github.com/ada", url2Label: "GitHub" });
+
+ await waitFor(() => {
+ expect(screen.getByText("Link 2")).toBeInTheDocument();
+ });
+ const removeButton = screen
+ .getByText("Link 2")
+ .parentElement!.querySelector("button")!;
+ await user.click(removeButton);
+
+ await waitFor(() => {
+ expect(screen.queryByText("Link 2")).not.toBeInTheDocument();
+ });
+ expect(
+ screen.queryByDisplayValue("https://github.com/ada"),
+ ).not.toBeInTheDocument();
+ });
+ });
+
+ describe("submission", () => {
+ it("calls addContactInfo with the resume id for a new contact", async () => {
+ (addContactInfo as any).mockResolvedValue({ success: true });
+
+ renderNew();
+ await fillRequiredFields();
+ await user.click(screen.getByRole("button", { name: /save/i }));
+
+ await waitFor(() => {
+ expect(addContactInfo).toHaveBeenCalledTimes(1);
+ });
+ expect(addContactInfo).toHaveBeenCalledWith(
+ expect.objectContaining({
+ resumeId: mockResumeId,
+ firstName: "Grace",
+ lastName: "Hopper",
+ headline: "Rear Admiral",
+ email: "[email protected]",
+ phone: "555-0199",
+ }),
+ );
+ expect(updateContactInfo).not.toHaveBeenCalled();
+ });
+
+ it("calls updateContactInfo with the contact id when editing", async () => {
+ (updateContactInfo as any).mockResolvedValue({ success: true });
+
+ renderEdit();
+ await waitFor(() => {
+ expect(
+ (screen.getByLabelText("Headline") as HTMLInputElement).value,
+ ).toBe("Software Engineer");
+ });
+
+ const headline = screen.getByLabelText("Headline");
+ await user.clear(headline);
+ await user.type(headline, "Principal Engineer");
+ await user.click(screen.getByRole("button", { name: /save/i }));
+
+ await waitFor(() => {
+ expect(updateContactInfo).toHaveBeenCalledTimes(1);
+ });
+ expect(updateContactInfo).toHaveBeenCalledWith(
+ expect.objectContaining({
+ id: "contact-1",
+ headline: "Principal Engineer",
+ }),
+ );
+ expect(addContactInfo).not.toHaveBeenCalled();
+ });
+
+ it("closes the dialog and shows a success toast when the action succeeds", async () => {
+ (addContactInfo as any).mockResolvedValue({ success: true });
+
+ renderNew();
+ await fillRequiredFields();
+ await user.click(screen.getByRole("button", { name: /save/i }));
+
+ await waitFor(() => {
+ expect(mockSetDialogOpen).toHaveBeenCalledWith(false);
+ });
+ expect(toast).toHaveBeenCalledWith(
+ expect.objectContaining({
+ variant: "success",
+ description: expect.stringContaining("created"),
+ }),
+ );
+ });
+
+ it("shows an updated success message when editing", async () => {
+ (updateContactInfo as any).mockResolvedValue({ success: true });
+
+ renderEdit();
+ const headline = await screen.findByLabelText("Headline");
+ await user.clear(headline);
+ await user.type(headline, "Principal Engineer");
+ await user.click(screen.getByRole("button", { name: /save/i }));
+
+ await waitFor(() => {
+ expect(toast).toHaveBeenCalledWith(
+ expect.objectContaining({
+ variant: "success",
+ description: expect.stringContaining("updated"),
+ }),
+ );
+ });
+ });
+
+ it("keeps the dialog open and shows an error toast when the action fails", async () => {
+ (addContactInfo as any).mockResolvedValue({
+ success: false,
+ message: "Failed to create contact info.",
+ });
+
+ renderNew();
+ await fillRequiredFields();
+ await user.click(screen.getByRole("button", { name: /save/i }));
+
+ await waitFor(() => {
+ expect(toast).toHaveBeenCalledWith(
+ expect.objectContaining({
+ variant: "destructive",
+ title: "Error",
+ description: "Failed to create contact info.",
+ }),
+ );
+ });
+ expect(mockSetDialogOpen).not.toHaveBeenCalledWith(false);
+ });
+ });
+
+ describe("validation", () => {
+ it("blocks submission and reports a malformed email", async () => {
+ renderNew();
+ await user.type(screen.getByLabelText("First Name"), "Grace");
+ await user.type(screen.getByLabelText("Last Name"), "Hopper");
+ await user.type(screen.getByLabelText("Headline"), "Rear Admiral");
+ await user.type(screen.getByLabelText("Email"), "not-an-email");
+ await user.type(screen.getByLabelText("Phone"), "555-0199");
+
+ await user.click(screen.getByRole("button", { name: /save/i }));
+
+ await waitFor(() => {
+ expect(
+ screen.getByText("Please enter a valid email!"),
+ ).toBeInTheDocument();
+ });
+ expect(addContactInfo).not.toHaveBeenCalled();
+ });
+
+ it("blocks submission of a link without a scheme", async () => {
+ renderNew();
+ await fillRequiredFields();
+ await user.type(
+ screen.getByPlaceholderText("https://"),
+ "linkedin.com/in/grace",
+ );
+
+ await user.click(screen.getByRole("button", { name: /save/i }));
+
+ await waitFor(() => {
+ expect(
+ screen.getByText("URL must start with http:// or https://"),
+ ).toBeInTheDocument();
+ });
+ expect(addContactInfo).not.toHaveBeenCalled();
+ });
+ });
+});
diff --git a/src/components/profile/AddContactInfo.tsx b/src/components/profile/AddContactInfo.tsx
index ad9aefb..a7b247c 100644
--- a/src/components/profile/AddContactInfo.tsx
+++ b/src/components/profile/AddContactInfo.tsx
@@ -23,7 +23,7 @@ import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { z } from "zod";
import { useEffect, useState, useTransition } from "react";
-import { toast } from "../ui/use-toast";
+import { toastActionResult } from "@/lib/toast";
import { ContactInfo } from "@/models/profile.model";
import { addContactInfo, updateContactInfo } from "@/actions/profile.actions";
@@ -99,22 +99,15 @@ function AddContactInfo({
const res = contactInfoToEdit
? await updateContactInfo(data)
: await addContactInfo(data);
- if (!res.success) {
- toast({
- variant: "destructive",
- title: "Error!",
- description: res.message,
- });
- } else {
- reset();
- setDialogOpen(false);
- toast({
- variant: "success",
- description: `Contact Info has been ${
- contactInfoToEdit ? "updated" : "created"
- } successfully`,
- });
- }
+ toastActionResult(res, {
+ success: `Contact Info has been ${
+ contactInfoToEdit ? "updated" : "created"
+ } successfully`,
+ onSuccess: () => {
+ reset();
+ setDialogOpen(false);
+ },
+ });
});
};
@@ -133,7 +126,7 @@ function AddContactInfo({
className="grid grid-cols-1 md:grid-cols-2 gap-4 p-2"
>
{/* FIRST NAME */}
- <div className="md:col-span-2">
+ <div>
<FormField
control={form.control}
name="firstName"
@@ -150,7 +143,7 @@ function AddContactInfo({
</div>
{/* LAST NAME */}
- <div className="md:col-span-2">
+ <div>
<FormField
control={form.control}
name="lastName"
@@ -184,7 +177,7 @@ function AddContactInfo({
</div>
{/* EMAIL */}
- <div className="md:col-span-2">
+ <div>
<FormField
control={form.control}
name="email"
@@ -201,7 +194,7 @@ function AddContactInfo({
</div>
{/* PHONE */}
- <div className="md:col-span-2">
+ <div>
<FormField
control={form.control}
name="phone"