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.

4.8 KB
Raw Blame
import { JobContactsTab } from "@/components/myjobs/job-details/JobContactsTab";
import { render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import {
  getAllContacts,
  getJobContacts,
  removeJobContact,
} from "@/actions/contact.actions";
import { getAllContactRoles } from "@/actions/contactRole.actions";

vi.mock("@/actions/contact.actions", () => ({
  getAllContacts: vi.fn(),
  getJobContacts: vi.fn(),
  addJobContact: vi.fn(),
  removeJobContact: vi.fn(),
  createContact: vi.fn(),
  updateContact: vi.fn(),
}));

vi.mock("@/actions/contactRole.actions", () => ({
  getAllContactRoles: vi.fn(),
  createContactRole: vi.fn(),
}));

vi.mock("@/actions/company.actions", () => ({
  getAllCompanies: vi.fn().mockResolvedValue([]),
  addCompany: vi.fn(),
}));
vi.mock("@/actions/jobLocation.actions", () => ({
  getAllJobLocations: vi.fn().mockResolvedValue([]),
}));

vi.mock("@/lib/toast", () => ({
  toastSuccess: vi.fn(),
  toastError: vi.fn(),
  toastActionResult: vi.fn((res, opts) => res?.success && opts?.onSuccess?.()),
}));

const link = {
  id: "jc1",
  jobId: "j1",
  contactId: "c1",
  roleId: "r1",
  createdAt: new Date(),
  Role: { id: "r1", label: "Recruiter", value: "recruiter", createdBy: "u1" },
  Contact: {
    id: "c1",
    name: "Sarah Cole",
    title: "Talent Partner",
    email: "[email protected]",
    phone: null,
    linkedinUrl: null,
    lastContactedAt: null,
    Company: { id: "co1", label: "Vercel" },
  },
};

describe("JobContactsTab", () => {
  const user = userEvent.setup({ skipHover: true });

  beforeEach(() => {
    vi.clearAllMocks();
    (getAllContacts as any).mockResolvedValue([
      { id: "c2", label: "Priya Nair", value: "priya nair" },
    ]);
    (getAllContactRoles as any).mockResolvedValue([
      { id: "r1", label: "Recruiter", value: "recruiter", createdBy: "u1" },
    ]);
    (getJobContacts as any).mockResolvedValue([link]);
  });

  it("lists the linked contacts with their role", () => {
    render(
      <JobContactsTab
        jobId="j1"
        links={[link] as any}
        onLinksChange={vi.fn()}
      />,
    );
    expect(screen.getByText("Sarah Cole")).toBeInTheDocument();
    expect(screen.getByText("Recruiter")).toBeInTheDocument();
  });

  it("shows the last contacted date when the contact has one", () => {
    const contacted = {
      ...link,
      Contact: { ...link.Contact, lastContactedAt: new Date(2026, 7, 1) },
    };
    render(
      <JobContactsTab
        jobId="j1"
        links={[contacted] as any}
        onLinksChange={vi.fn()}
      />,
    );
    expect(screen.getByText(/last contacted aug 1, 2026/i)).toBeInTheDocument();
  });

  it("omits the last contacted date when the contact has none", () => {
    render(
      <JobContactsTab
        jobId="j1"
        links={[link] as any}
        onLinksChange={vi.fn()}
      />,
    );
    expect(screen.queryByText(/last contacted/i)).not.toBeInTheDocument();
  });

  it("shows an empty state with no links", () => {
    render(<JobContactsTab jobId="j1" links={[]} onLinksChange={vi.fn()} />);
    expect(screen.getByText(/no contacts on this job/i)).toBeInTheDocument();
  });

  it("does not fetch the picker lists until the add form is opened", () => {
    render(
      <JobContactsTab
        jobId="j1"
        links={[link] as any}
        onLinksChange={vi.fn()}
      />,
    );
    expect(getAllContacts).not.toHaveBeenCalled();
    expect(getAllContactRoles).not.toHaveBeenCalled();
  });

  it("fetches contacts and roles on first open of the add form", async () => {
    render(
      <JobContactsTab
        jobId="j1"
        links={[link] as any}
        onLinksChange={vi.fn()}
      />,
    );
    await user.click(screen.getByRole("button", { name: /add contact/i }));

    await waitFor(() => expect(getAllContacts).toHaveBeenCalledTimes(1));
    expect(getAllContactRoles).toHaveBeenCalledTimes(1);

    await user.click(screen.getByRole("button", { name: /cancel/i }));
    await user.click(screen.getByRole("button", { name: /add contact/i }));

    expect(getAllContacts).toHaveBeenCalledTimes(1);
  });

  it("unlinks a contact and hands the refreshed list to the parent", async () => {
    (removeJobContact as any).mockResolvedValue({ success: true });
    (getJobContacts as any).mockResolvedValue([]);
    const onLinksChange = vi.fn();

    render(
      <JobContactsTab
        jobId="j1"
        links={[link] as any}
        onLinksChange={onLinksChange}
      />,
    );
    await user.click(screen.getByRole("button", { name: /remove sarah cole/i }));
    await user.click(await screen.findByRole("button", { name: "Delete" }));

    await waitFor(() => expect(removeJobContact).toHaveBeenCalledWith("jc1"));
    await waitFor(() => expect(getJobContacts).toHaveBeenCalledWith("j1"));
    await waitFor(() => expect(onLinksChange).toHaveBeenCalledWith([]));
  });
});