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 resume summary, experience & education

Khuram Niazd9d399c51a9f
diff --git a/__tests__/AddEducation.spec.tsx b/__tests__/AddEducation.spec.tsx
new file mode 100644
index 0000000..7705a2b
--- /dev/null
+++ b/__tests__/AddEducation.spec.tsx
@@ -0,0 +1,728 @@
+import { render, screen, waitFor } from "@testing-library/react";
+import userEvent from "@testing-library/user-event";
+import AddEducation from "@/components/profile/AddEducation";
+import { addEducation, updateEducation } from "@/actions/profile.actions";
+import { getAllJobLocations } from "@/actions/jobLocation.actions";
+
+// Mock the actions
+jest.mock("@/actions/profile.actions", () => ({
+ addEducation: jest.fn(),
+ updateEducation: jest.fn(),
+}));
+
+jest.mock("@/actions/jobLocation.actions", () => ({
+ getAllJobLocations: jest.fn(),
+}));
+
+// Mock toast
+jest.mock("@/components/ui/use-toast", () => ({
+ toast: jest.fn(),
+}));
+
+// Mock Combobox component
+jest.mock("@/components/ComboBox", () => ({
+ Combobox: ({ field, options }: any) => (
+ <select
+ data-testid="combobox-location"
+ value={field.value || ""}
+ onChange={(e) => field.onChange(e.target.value)}
+ >
+ <option value="">Select...</option>
+ {options?.map((option: any) => (
+ <option key={option.id} value={option.id}>
+ {option.label}
+ </option>
+ ))}
+ </select>
+ ),
+}));
+
+// Mock DatePicker component
+jest.mock("@/components/DatePicker", () => ({
+ DatePicker: ({ field, isEnabled }: any) => (
+ <input
+ type="date"
+ data-testid={`datepicker-${field.name}`}
+ value={
+ field.value ? new Date(field.value).toISOString().split("T")[0] : ""
+ }
+ onChange={(e) => field.onChange(new Date(e.target.value))}
+ disabled={!isEnabled}
+ />
+ ),
+}));
+
+// Mock TiptapEditor component
+jest.mock("@/components/TiptapEditor", () => ({
+ __esModule: true,
+ default: ({ field }: any) => (
+ <textarea
+ data-testid="tiptap-editor"
+ value={field.value || ""}
+ onChange={(e) => field.onChange(e.target.value)}
+ />
+ ),
+}));
+
+describe("AddEducation Component", () => {
+ const user = userEvent.setup();
+ const mockResumeId = "resume-123";
+ const mockSectionId = "section-456";
+ const mockSetDialogOpen = jest.fn();
+
+ const mockLocations = [
+ { id: "location-1", label: "New York, NY", value: "New York, NY" },
+ {
+ id: "location-2",
+ label: "San Francisco, CA",
+ value: "San Francisco, CA",
+ },
+ ];
+
+ beforeEach(() => {
+ jest.clearAllMocks();
+ (getAllJobLocations as jest.Mock).mockResolvedValue(mockLocations);
+ });
+
+ it("should render Add Education dialog with correct title", async () => {
+ render(
+ <AddEducation
+ resumeId={mockResumeId}
+ sectionId={mockSectionId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ await waitFor(() => {
+ expect(screen.getByText("Add Education")).toBeInTheDocument();
+ });
+ });
+
+ it("should render Edit Education dialog when educationToEdit is provided", async () => {
+ const mockEducationToEdit = {
+ id: "education-1",
+ educations: [
+ {
+ id: "edu-1",
+ institution: "Stanford University",
+ degree: "Bachelor's",
+ fieldOfStudy: "Computer Science",
+ location: {
+ id: "location-1",
+ label: "Stanford, CA",
+ value: "Stanford, CA",
+ },
+ startDate: new Date("2018-09-01"),
+ endDate: new Date("2022-06-01"),
+ description: "Studied computer science",
+ },
+ ],
+ };
+
+ render(
+ <AddEducation
+ resumeId={mockResumeId}
+ sectionId={mockSectionId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ educationToEdit={mockEducationToEdit as any}
+ />
+ );
+
+ await waitFor(() => {
+ expect(screen.getByText("Edit Education")).toBeInTheDocument();
+ });
+ });
+
+ it("should render section title field when sectionId is not provided", async () => {
+ render(
+ <AddEducation
+ resumeId={mockResumeId}
+ sectionId={undefined}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ await waitFor(() => {
+ expect(screen.getByLabelText(/section title/i)).toBeInTheDocument();
+ });
+ });
+
+ it("should not render section title field when sectionId is provided", async () => {
+ render(
+ <AddEducation
+ resumeId={mockResumeId}
+ sectionId={mockSectionId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ await waitFor(() => {
+ expect(screen.getByText("Add Education")).toBeInTheDocument();
+ });
+
+ expect(screen.queryByLabelText(/section title/i)).not.toBeInTheDocument();
+ });
+
+ it("should render all form fields correctly", async () => {
+ render(
+ <AddEducation
+ resumeId={mockResumeId}
+ sectionId={mockSectionId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ await waitFor(() => {
+ expect(screen.getByText("School")).toBeInTheDocument();
+ expect(screen.getByTestId("combobox-location")).toBeInTheDocument();
+ expect(screen.getByText("Degree")).toBeInTheDocument();
+ expect(screen.getByText("Field of study")).toBeInTheDocument();
+ expect(screen.getByText("Start Date")).toBeInTheDocument();
+ expect(screen.getByText("End Date")).toBeInTheDocument();
+ expect(screen.getByText(/degree completed/i)).toBeInTheDocument();
+ expect(screen.getByText("Description")).toBeInTheDocument();
+ });
+ });
+
+ it("should populate form fields when editing an education", async () => {
+ const mockEducationToEdit = {
+ id: "education-1",
+ educations: [
+ {
+ id: "edu-1",
+ institution: "Stanford University",
+ degree: "Bachelor's",
+ fieldOfStudy: "Computer Science",
+ location: {
+ id: "location-1",
+ label: "Stanford, CA",
+ value: "Stanford, CA",
+ },
+ startDate: new Date("2018-09-01"),
+ endDate: new Date("2022-06-01"),
+ description: "Studied computer science",
+ },
+ ],
+ };
+
+ render(
+ <AddEducation
+ resumeId={mockResumeId}
+ sectionId={mockSectionId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ educationToEdit={mockEducationToEdit as any}
+ />
+ );
+
+ await waitFor(() => {
+ const institutionInput = screen.getByPlaceholderText(
+ "Ex: Stanford"
+ ) as HTMLInputElement;
+ expect(institutionInput.value).toBe("Stanford University");
+
+ const degreeInput = screen.getByPlaceholderText(
+ "Ex: Bachelor's"
+ ) as HTMLInputElement;
+ expect(degreeInput.value).toBe("Bachelor's");
+
+ const fieldOfStudyInput = screen.getByPlaceholderText(
+ "Ex: Computer Science"
+ ) as HTMLInputElement;
+ expect(fieldOfStudyInput.value).toBe("Computer Science");
+ });
+ });
+
+ it("should close dialog when Cancel button is clicked", async () => {
+ render(
+ <AddEducation
+ resumeId={mockResumeId}
+ sectionId={mockSectionId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ await waitFor(() => {
+ expect(screen.getByText("Add Education")).toBeInTheDocument();
+ });
+
+ const cancelButton = screen.getByRole("button", { name: /cancel/i });
+ await user.click(cancelButton);
+
+ expect(mockSetDialogOpen).toHaveBeenCalledWith(false);
+ });
+
+ it("should toggle degree completed label text", async () => {
+ render(
+ <AddEducation
+ resumeId={mockResumeId}
+ sectionId={mockSectionId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ await waitFor(() => {
+ expect(screen.getByText(/degree completed/i)).toBeInTheDocument();
+ });
+
+ const degreeCompletedSwitch = screen.getByRole("switch");
+ await user.click(degreeCompletedSwitch);
+
+ await waitFor(() => {
+ expect(screen.getByText(/currently studying/i)).toBeInTheDocument();
+ });
+ });
+
+ it("should call addEducation when submitting a new education", async () => {
+ (addEducation as jest.Mock).mockResolvedValue({
+ success: true,
+ message: "Education added successfully",
+ });
+
+ render(
+ <AddEducation
+ resumeId={mockResumeId}
+ sectionId={mockSectionId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ // Wait for options to be loaded and rendered in the select element
+ await waitFor(() => {
+ const locationSelect = screen.getByTestId(
+ "combobox-location"
+ ) as HTMLSelectElement;
+ expect(locationSelect.options.length).toBeGreaterThan(1);
+ });
+
+ const institutionInput = screen.getByPlaceholderText(
+ "Ex: Stanford"
+ ) as HTMLInputElement;
+ const locationSelect = screen.getByTestId(
+ "combobox-location"
+ ) as HTMLSelectElement;
+ const degreeInput = screen.getByPlaceholderText(
+ "Ex: Bachelor's"
+ ) as HTMLInputElement;
+ const fieldOfStudyInput = screen.getByPlaceholderText(
+ "Ex: Computer Science"
+ ) as HTMLInputElement;
+ const startDateInput = screen.getByTestId(
+ "datepicker-startDate"
+ ) as HTMLInputElement;
+ const descriptionEditor = screen.getByTestId(
+ "tiptap-editor"
+ ) as HTMLTextAreaElement;
+
+ await user.type(institutionInput, "MIT");
+ await user.selectOptions(locationSelect, "location-1");
+ await user.type(degreeInput, "Master's");
+ await user.type(fieldOfStudyInput, "Artificial Intelligence");
+ await user.type(startDateInput, "2020-09-01");
+ await user.type(descriptionEditor, "Advanced AI studies");
+
+ // Wait for form to be valid
+ await waitFor(() => {
+ const saveButton = screen.getByRole("button", { name: /save/i });
+ expect(saveButton).not.toBeDisabled();
+ });
+
+ const saveButton = screen.getByRole("button", { name: /save/i });
+ await user.click(saveButton);
+
+ await waitFor(() => {
+ expect(addEducation).toHaveBeenCalledTimes(1);
+ expect(addEducation).toHaveBeenCalledWith(
+ expect.objectContaining({
+ institution: "MIT",
+ location: "location-1",
+ degree: "Master's",
+ fieldOfStudy: "Artificial Intelligence",
+ description: "Advanced AI studies",
+ })
+ );
+ });
+ });
+
+ it("should call updateEducation when editing an existing education", async () => {
+ const mockEducationToEdit = {
+ id: "education-1",
+ educations: [
+ {
+ id: "edu-1",
+ institution: "Stanford University",
+ degree: "Bachelor's",
+ fieldOfStudy: "Computer Science",
+ location: {
+ id: "location-1",
+ label: "Stanford, CA",
+ value: "Stanford, CA",
+ },
+ startDate: new Date("2018-09-01"),
+ endDate: new Date("2022-06-01"),
+ description: "Studied computer science",
+ },
+ ],
+ };
+
+ (updateEducation as jest.Mock).mockResolvedValue({
+ success: true,
+ message: "Education updated successfully",
+ });
+
+ render(
+ <AddEducation
+ resumeId={mockResumeId}
+ sectionId={mockSectionId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ educationToEdit={mockEducationToEdit as any}
+ />
+ );
+
+ await waitFor(() => {
+ const institutionInput = screen.getByPlaceholderText(
+ "Ex: Stanford"
+ ) as HTMLInputElement;
+ expect(institutionInput.value).toBe("Stanford University");
+ });
+
+ const degreeInput = screen.getByPlaceholderText(
+ "Ex: Bachelor's"
+ ) as HTMLInputElement;
+ await user.clear(degreeInput);
+ await user.type(degreeInput, "Master's");
+
+ // Wait for form to be valid
+ await waitFor(() => {
+ const saveButton = screen.getByRole("button", { name: /save/i });
+ expect(saveButton).not.toBeDisabled();
+ });
+
+ const saveButton = screen.getByRole("button", { name: /save/i });
+ await user.click(saveButton);
+
+ await waitFor(() => {
+ expect(updateEducation).toHaveBeenCalledTimes(1);
+ expect(updateEducation).toHaveBeenCalledWith(
+ expect.objectContaining({
+ id: "edu-1",
+ degree: "Master's",
+ })
+ );
+ });
+ });
+
+ it("should close dialog and show success toast on successful submission", async () => {
+ const { toast } = require("@/components/ui/use-toast");
+
+ (addEducation as jest.Mock).mockResolvedValue({
+ success: true,
+ message: "Education added successfully",
+ });
+
+ render(
+ <AddEducation
+ resumeId={mockResumeId}
+ sectionId={mockSectionId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ // Wait for options to be loaded
+ await waitFor(() => {
+ const locationSelect = screen.getByTestId(
+ "combobox-location"
+ ) as HTMLSelectElement;
+ expect(locationSelect.options.length).toBeGreaterThan(1);
+ });
+
+ const institutionInput = screen.getByPlaceholderText(
+ "Ex: Stanford"
+ ) as HTMLInputElement;
+ const locationSelect = screen.getByTestId(
+ "combobox-location"
+ ) as HTMLSelectElement;
+ const degreeInput = screen.getByPlaceholderText(
+ "Ex: Bachelor's"
+ ) as HTMLInputElement;
+ const fieldOfStudyInput = screen.getByPlaceholderText(
+ "Ex: Computer Science"
+ ) as HTMLInputElement;
+ const startDateInput = screen.getByTestId(
+ "datepicker-startDate"
+ ) as HTMLInputElement;
+
+ await user.type(institutionInput, "Harvard");
+ await user.selectOptions(locationSelect, "location-1");
+ await user.type(degreeInput, "PhD");
+ await user.type(fieldOfStudyInput, "Mathematics");
+ await user.type(startDateInput, "2019-09-01");
+
+ // Wait for form to be valid
+ await waitFor(() => {
+ const saveButton = screen.getByRole("button", { name: /save/i });
+ expect(saveButton).not.toBeDisabled();
+ });
+
+ const saveButton = screen.getByRole("button", { name: /save/i });
+ await user.click(saveButton);
+
+ await waitFor(() => {
+ expect(mockSetDialogOpen).toHaveBeenCalledWith(false);
+ expect(toast).toHaveBeenCalledWith(
+ expect.objectContaining({
+ variant: "success",
+ description: expect.stringContaining("added"),
+ })
+ );
+ });
+ });
+
+ it("should show error toast on failed submission", async () => {
+ const { toast } = require("@/components/ui/use-toast");
+
+ (addEducation as jest.Mock).mockResolvedValue({
+ success: false,
+ message: "Failed to add education",
+ });
+
+ render(
+ <AddEducation
+ resumeId={mockResumeId}
+ sectionId={mockSectionId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ // Wait for options to be loaded
+ await waitFor(() => {
+ const locationSelect = screen.getByTestId(
+ "combobox-location"
+ ) as HTMLSelectElement;
+ expect(locationSelect.options.length).toBeGreaterThan(1);
+ });
+
+ const institutionInput = screen.getByPlaceholderText(
+ "Ex: Stanford"
+ ) as HTMLInputElement;
+ const locationSelect = screen.getByTestId(
+ "combobox-location"
+ ) as HTMLSelectElement;
+ const degreeInput = screen.getByPlaceholderText(
+ "Ex: Bachelor's"
+ ) as HTMLInputElement;
+ const fieldOfStudyInput = screen.getByPlaceholderText(
+ "Ex: Computer Science"
+ ) as HTMLInputElement;
+ const startDateInput = screen.getByTestId(
+ "datepicker-startDate"
+ ) as HTMLInputElement;
+
+ await user.type(institutionInput, "Yale");
+ await user.selectOptions(locationSelect, "location-2");
+ await user.type(degreeInput, "Bachelor's");
+ await user.type(fieldOfStudyInput, "Physics");
+ await user.type(startDateInput, "2021-09-01");
+
+ // Wait for form to be valid
+ await waitFor(() => {
+ const saveButton = screen.getByRole("button", { name: /save/i });
+ expect(saveButton).not.toBeDisabled();
+ });
+
+ const saveButton = screen.getByRole("button", { name: /save/i });
+ await user.click(saveButton);
+
+ await waitFor(() => {
+ expect(toast).toHaveBeenCalledWith(
+ expect.objectContaining({
+ variant: "destructive",
+ title: "Error!",
+ description: "Failed to add education",
+ })
+ );
+ expect(mockSetDialogOpen).not.toHaveBeenCalledWith(false);
+ });
+ });
+
+ it("should show updated success message when editing", async () => {
+ const { toast } = require("@/components/ui/use-toast");
+
+ const mockEducationToEdit = {
+ id: "education-1",
+ educations: [
+ {
+ id: "edu-1",
+ institution: "MIT",
+ degree: "Bachelor's",
+ fieldOfStudy: "Engineering",
+ location: {
+ id: "location-1",
+ label: "Cambridge, MA",
+ value: "Cambridge, MA",
+ },
+ startDate: new Date("2017-09-01"),
+ endDate: new Date("2021-06-01"),
+ description: "Engineering studies",
+ },
+ ],
+ };
+
+ (updateEducation as jest.Mock).mockResolvedValue({
+ success: true,
+ message: "Education updated successfully",
+ });
+
+ render(
+ <AddEducation
+ resumeId={mockResumeId}
+ sectionId={mockSectionId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ educationToEdit={mockEducationToEdit as any}
+ />
+ );
+
+ await waitFor(() => {
+ const institutionInput = screen.getByPlaceholderText(
+ "Ex: Stanford"
+ ) as HTMLInputElement;
+ expect(institutionInput.value).toBe("MIT");
+ });
+
+ const institutionInput = screen.getByPlaceholderText(
+ "Ex: Stanford"
+ ) as HTMLInputElement;
+ await user.clear(institutionInput);
+ await user.type(institutionInput, "MIT Updated");
+
+ // Wait for form to be valid
+ await waitFor(() => {
+ const saveButton = screen.getByRole("button", { name: /save/i });
+ expect(saveButton).not.toBeDisabled();
+ });
+
+ const saveButton = screen.getByRole("button", { name: /save/i });
+ await user.click(saveButton);
+
+ await waitFor(() => {
+ expect(toast).toHaveBeenCalledWith(
+ expect.objectContaining({
+ variant: "success",
+ description: expect.stringContaining("updated"),
+ })
+ );
+ });
+ });
+
+ it("should not render dialog when dialogOpen is false", () => {
+ const { container } = render(
+ <AddEducation
+ resumeId={mockResumeId}
+ sectionId={mockSectionId}
+ dialogOpen={false}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ expect(screen.queryByText("Add Education")).not.toBeInTheDocument();
+ expect(container.querySelector("form")).not.toBeInTheDocument();
+ });
+
+ it("should load locations on mount", async () => {
+ render(
+ <AddEducation
+ resumeId={mockResumeId}
+ sectionId={mockSectionId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ await waitFor(() => {
+ expect(getAllJobLocations).toHaveBeenCalledTimes(1);
+ });
+
+ await waitFor(() => {
+ const locationSelect = screen.getByTestId(
+ "combobox-location"
+ ) as HTMLSelectElement;
+ expect(locationSelect.options.length).toBe(3); // "Select..." + 2 locations
+ });
+ });
+
+ it("should display loading indicator when form is submitting", async () => {
+ (addEducation as jest.Mock).mockImplementation(
+ () =>
+ new Promise((resolve) =>
+ setTimeout(() => resolve({ success: true, message: "Success" }), 100)
+ )
+ );
+
+ render(
+ <AddEducation
+ resumeId={mockResumeId}
+ sectionId={mockSectionId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ // Wait for options to be loaded
+ await waitFor(() => {
+ const locationSelect = screen.getByTestId(
+ "combobox-location"
+ ) as HTMLSelectElement;
+ expect(locationSelect.options.length).toBeGreaterThan(1);
+ });
+
+ const institutionInput = screen.getByPlaceholderText(
+ "Ex: Stanford"
+ ) as HTMLInputElement;
+ const locationSelect = screen.getByTestId(
+ "combobox-location"
+ ) as HTMLSelectElement;
+ const degreeInput = screen.getByPlaceholderText(
+ "Ex: Bachelor's"
+ ) as HTMLInputElement;
+ const fieldOfStudyInput = screen.getByPlaceholderText(
+ "Ex: Computer Science"
+ ) as HTMLInputElement;
+ const startDateInput = screen.getByTestId(
+ "datepicker-startDate"
+ ) as HTMLInputElement;
+
+ await user.type(institutionInput, "Oxford");
+ await user.selectOptions(locationSelect, "location-1");
+ await user.type(degreeInput, "Master's");
+ await user.type(fieldOfStudyInput, "Literature");
+ await user.type(startDateInput, "2022-09-01");
+
+ // Wait for form to be valid
+ await waitFor(() => {
+ const saveButton = screen.getByRole("button", { name: /save/i });
+ expect(saveButton).not.toBeDisabled();
+ });
+
+ const saveButton = screen.getByRole("button", { name: /save/i });
+ await user.click(saveButton);
+
+ // Check for loading spinner
+ await waitFor(() => {
+ expect(screen.getByText("Save").closest("button")).toContainHTML(
+ "spinner"
+ );
+ });
+ });
+});
diff --git a/__tests__/AddExperience.spec.tsx b/__tests__/AddExperience.spec.tsx
new file mode 100644
index 0000000..73102fb
--- /dev/null
+++ b/__tests__/AddExperience.spec.tsx
@@ -0,0 +1,759 @@
+import AddExperience from "@/components/profile/AddExperience";
+import { addExperience, updateExperience } from "@/actions/profile.actions";
+import { getAllCompanies } from "@/actions/company.actions";
+import { getAllJobTitles } from "@/actions/jobtitle.actions";
+import { getAllJobLocations } from "@/actions/jobLocation.actions";
+import "@testing-library/jest-dom";
+import { screen, render, waitFor, fireEvent } from "@testing-library/react";
+import userEvent from "@testing-library/user-event";
+import { ResumeSection } from "@/models/profile.model";
+
+jest.mock("@/actions/profile.actions", () => ({
+ addExperience: jest.fn(),
+ updateExperience: jest.fn(),
+}));
+
+jest.mock("@/actions/company.actions", () => ({
+ getAllCompanies: jest.fn(),
+}));
+
+jest.mock("@/actions/jobtitle.actions", () => ({
+ getAllJobTitles: jest.fn(),
+}));
+
+jest.mock("@/actions/jobLocation.actions", () => ({
+ getAllJobLocations: jest.fn(),
+}));
+
+// Mock TiptapEditor component
+jest.mock("@/components/TiptapEditor", () => {
+ return function TiptapEditor({ field }: any) {
+ return (
+ <textarea
+ data-testid="tiptap-editor"
+ value={field.value || ""}
+ onChange={(e) => field.onChange(e.target.value)}
+ placeholder="Enter job description"
+ />
+ );
+ };
+});
+
+// Mock Combobox component
+jest.mock("@/components/ComboBox", () => ({
+ Combobox: ({ field, options }: any) => {
+ return (
+ <select
+ data-testid={`combobox-${field.name}`}
+ value={field.value || ""}
+ onChange={(e) => field.onChange(e.target.value)}
+ >
+ <option value="">Select...</option>
+ {options?.map((option: any) => (
+ <option key={option.id} value={option.id}>
+ {option.label || option.value}
+ </option>
+ ))}
+ </select>
+ );
+ },
+}));
+
+// Mock DatePicker component
+jest.mock("@/components/DatePicker", () => ({
+ DatePicker: ({ field, isEnabled }: any) => {
+ return (
+ <input
+ data-testid={`datepicker-${field.name}`}
+ type="date"
+ value={
+ field.value ? new Date(field.value).toISOString().split("T")[0] : ""
+ }
+ onChange={(e) =>
+ field.onChange(e.target.value ? new Date(e.target.value) : undefined)
+ }
+ disabled={!isEnabled}
+ />
+ );
+ },
+}));
+
+// Mock toast
+jest.mock("@/components/ui/use-toast", () => ({
+ toast: jest.fn(),
+}));
+
+describe("AddExperience Component", () => {
+ const mockSetDialogOpen = jest.fn();
+ const mockResumeId = "resume-123";
+ const mockSectionId = "section-123";
+ const user = userEvent.setup();
+
+ const mockCompanies = [
+ { id: "company-1", label: "Tech Corp", value: "tech-corp" },
+ { id: "company-2", label: "Startup Inc", value: "startup-inc" },
+ ];
+
+ const mockJobTitles = [
+ { id: "title-1", label: "Software Engineer", value: "software-engineer" },
+ { id: "title-2", label: "Senior Developer", value: "senior-developer" },
+ ];
+
+ const mockLocations = [
+ { id: "location-1", label: "New York, NY", value: "new-york-ny" },
+ { id: "location-2", label: "San Francisco, CA", value: "san-francisco-ca" },
+ ];
+
+ beforeEach(() => {
+ jest.clearAllMocks();
+ (getAllCompanies as jest.Mock).mockResolvedValue(mockCompanies);
+ (getAllJobTitles as jest.Mock).mockResolvedValue(mockJobTitles);
+ (getAllJobLocations as jest.Mock).mockResolvedValue(mockLocations);
+ });
+
+ it("should render Add Experience dialog with correct title", async () => {
+ render(
+ <AddExperience
+ resumeId={mockResumeId}
+ sectionId={mockSectionId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ await waitFor(() => {
+ expect(screen.getByText("Add Experience")).toBeInTheDocument();
+ });
+ });
+
+ it("should render Edit Experience dialog when experienceToEdit is provided", async () => {
+ const mockExperienceToEdit: ResumeSection = {
+ id: "section-1",
+ resumeId: mockResumeId,
+ sectionTitle: "Work Experience",
+ sectionType: "experience" as any,
+ workExperiences: [
+ {
+ id: "exp-1",
+ Company: mockCompanies[0] as any,
+ jobTitle: mockJobTitles[0] as any,
+ location: mockLocations[0] as any,
+ startDate: new Date("2020-01-01"),
+ endDate: new Date("2022-12-31"),
+ currentJob: false,
+ description: "Worked on various projects",
+ },
+ ],
+ };
+
+ render(
+ <AddExperience
+ resumeId={mockResumeId}
+ sectionId={mockSectionId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ experienceToEdit={mockExperienceToEdit}
+ />
+ );
+
+ await waitFor(() => {
+ expect(screen.getByText("Edit Experience")).toBeInTheDocument();
+ });
+ });
+
+ it("should render section title field when sectionId is not provided", async () => {
+ render(
+ <AddExperience
+ resumeId={mockResumeId}
+ sectionId={undefined}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ await waitFor(() => {
+ expect(screen.getByLabelText(/section title/i)).toBeInTheDocument();
+ });
+ });
+
+ it("should not render section title field when sectionId is provided", async () => {
+ render(
+ <AddExperience
+ resumeId={mockResumeId}
+ sectionId={mockSectionId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ await waitFor(() => {
+ expect(screen.queryByLabelText(/section title/i)).not.toBeInTheDocument();
+ });
+ });
+
+ it("should render all form fields correctly", async () => {
+ render(
+ <AddExperience
+ resumeId={mockResumeId}
+ sectionId={mockSectionId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ await waitFor(() => {
+ expect(screen.getByText(/job title/i)).toBeInTheDocument();
+ expect(screen.getByTestId("combobox-title")).toBeInTheDocument();
+ expect(screen.getByText(/company/i)).toBeInTheDocument();
+ expect(screen.getByTestId("combobox-company")).toBeInTheDocument();
+ expect(screen.getByText(/job location/i)).toBeInTheDocument();
+ expect(screen.getByTestId("combobox-location")).toBeInTheDocument();
+ expect(screen.getByText(/start date/i)).toBeInTheDocument();
+ expect(screen.getByTestId("datepicker-startDate")).toBeInTheDocument();
+ expect(screen.getByText(/end date/i)).toBeInTheDocument();
+ expect(screen.getByTestId("datepicker-endDate")).toBeInTheDocument();
+ expect(screen.getByText(/job description/i)).toBeInTheDocument();
+ expect(screen.getByTestId("tiptap-editor")).toBeInTheDocument();
+ expect(
+ screen.getByRole("button", { name: /cancel/i })
+ ).toBeInTheDocument();
+ expect(screen.getByRole("button", { name: /save/i })).toBeInTheDocument();
+ });
+ });
+
+ it("should populate form fields when editing an experience", async () => {
+ const mockExperienceToEdit: ResumeSection = {
+ id: "section-1",
+ resumeId: mockResumeId,
+ sectionTitle: "Work Experience",
+ sectionType: "experience" as any,
+ workExperiences: [
+ {
+ id: "exp-1",
+ Company: mockCompanies[0] as any,
+ jobTitle: mockJobTitles[0] as any,
+ location: mockLocations[0] as any,
+ startDate: new Date("2020-01-01"),
+ endDate: new Date("2022-12-31"),
+ currentJob: false,
+ description: "Worked on various projects",
+ },
+ ],
+ };
+
+ render(
+ <AddExperience
+ resumeId={mockResumeId}
+ sectionId={mockSectionId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ experienceToEdit={mockExperienceToEdit}
+ />
+ );
+
+ await waitFor(() => {
+ const jobTitleSelect = screen.getByTestId(
+ "combobox-title"
+ ) as HTMLSelectElement;
+ const companySelect = screen.getByTestId(
+ "combobox-company"
+ ) as HTMLSelectElement;
+ const locationSelect = screen.getByTestId(
+ "combobox-location"
+ ) as HTMLSelectElement;
+
+ expect(jobTitleSelect.value).toBe("title-1");
+ expect(companySelect.value).toBe("company-1");
+ expect(locationSelect.value).toBe("location-1");
+ });
+ });
+
+ it("should close dialog when Cancel button is clicked", async () => {
+ render(
+ <AddExperience
+ resumeId={mockResumeId}
+ sectionId={mockSectionId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ await waitFor(() => {
+ expect(
+ screen.getByRole("button", { name: /cancel/i })
+ ).toBeInTheDocument();
+ });
+
+ const cancelButton = screen.getByRole("button", { name: /cancel/i });
+ await user.click(cancelButton);
+
+ expect(mockSetDialogOpen).toHaveBeenCalledWith(false);
+ });
+
+ it("should disable Save button when form is not dirty", async () => {
+ render(
+ <AddExperience
+ resumeId={mockResumeId}
+ sectionId={mockSectionId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ await waitFor(() => {
+ const saveButton = screen.getByRole("button", { name: /save/i });
+ expect(saveButton).toBeDisabled();
+ });
+ });
+
+ it("should disable end date when current job is checked", async () => {
+ render(
+ <AddExperience
+ resumeId={mockResumeId}
+ sectionId={mockSectionId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ await waitFor(() => {
+ expect(screen.getByRole("switch")).toBeInTheDocument();
+ });
+
+ const currentJobSwitch = screen.getByRole("switch");
+ const endDateInput = screen.getByTestId(
+ "datepicker-endDate"
+ ) as HTMLInputElement;
+
+ expect(endDateInput).not.toBeDisabled();
+
+ await user.click(currentJobSwitch);
+
+ await waitFor(() => {
+ expect(endDateInput).toBeDisabled();
+ });
+ });
+
+ it("should toggle current job label text", async () => {
+ render(
+ <AddExperience
+ resumeId={mockResumeId}
+ sectionId={mockSectionId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ await waitFor(() => {
+ expect(screen.getByText(/job ended/i)).toBeInTheDocument();
+ });
+
+ const currentJobSwitch = screen.getByRole("switch");
+ await user.click(currentJobSwitch);
+
+ await waitFor(() => {
+ expect(screen.getByText(/current job/i)).toBeInTheDocument();
+ });
+ });
+
+ it("should call addExperience when submitting a new experience", async () => {
+ (addExperience as jest.Mock).mockResolvedValue({
+ success: true,
+ message: "Experience added successfully",
+ });
+
+ render(
+ <AddExperience
+ resumeId={mockResumeId}
+ sectionId={mockSectionId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ // Wait for options to be loaded and rendered in the select elements
+ await waitFor(() => {
+ const jobTitleSelect = screen.getByTestId(
+ "combobox-title"
+ ) as HTMLSelectElement;
+ expect(jobTitleSelect.options.length).toBeGreaterThan(1);
+ });
+
+ const jobTitleSelect = screen.getByTestId(
+ "combobox-title"
+ ) as HTMLSelectElement;
+ const companySelect = screen.getByTestId(
+ "combobox-company"
+ ) as HTMLSelectElement;
+ const locationSelect = screen.getByTestId(
+ "combobox-location"
+ ) as HTMLSelectElement;
+ const startDateInput = screen.getByTestId(
+ "datepicker-startDate"
+ ) as HTMLInputElement;
+ const jobDescriptionEditor = screen.getByTestId(
+ "tiptap-editor"
+ ) as HTMLTextAreaElement;
+
+ await user.selectOptions(jobTitleSelect, "title-1");
+ await user.selectOptions(companySelect, "company-1");
+ await user.selectOptions(locationSelect, "location-1");
+ await user.type(startDateInput, "2023-01-01");
+ await user.type(jobDescriptionEditor, "Developed amazing features");
+
+ // Wait for form to be valid
+ await waitFor(() => {
+ const saveButton = screen.getByRole("button", { name: /save/i });
+ expect(saveButton).not.toBeDisabled();
+ });
+
+ const saveButton = screen.getByRole("button", { name: /save/i });
+ await user.click(saveButton);
+
+ await waitFor(() => {
+ expect(addExperience).toHaveBeenCalledTimes(1);
+ expect(addExperience).toHaveBeenCalledWith(
+ expect.objectContaining({
+ title: "title-1",
+ company: "company-1",
+ location: "location-1",
+ jobDescription: "Developed amazing features",
+ })
+ );
+ });
+ });
+
+ it("should call updateExperience when editing an existing experience", async () => {
+ const mockExperienceToEdit: ResumeSection = {
+ id: "section-1",
+ resumeId: mockResumeId,
+ sectionTitle: "Work Experience",
+ sectionType: "experience" as any,
+ workExperiences: [
+ {
+ id: "exp-1",
+ Company: mockCompanies[0] as any,
+ jobTitle: mockJobTitles[0] as any,
+ location: mockLocations[0] as any,
+ startDate: new Date("2020-01-01"),
+ endDate: new Date("2022-12-31"),
+ currentJob: false,
+ description: "Worked on various projects",
+ },
+ ],
+ };
+
+ (updateExperience as jest.Mock).mockResolvedValue({
+ success: true,
+ message: "Experience updated successfully",
+ });
+
+ render(
+ <AddExperience
+ resumeId={mockResumeId}
+ sectionId={mockSectionId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ experienceToEdit={mockExperienceToEdit}
+ />
+ );
+
+ await waitFor(() => {
+ expect(screen.getByTestId("combobox-title")).toBeInTheDocument();
+ });
+
+ const jobTitleSelect = screen.getByTestId(
+ "combobox-title"
+ ) as HTMLSelectElement;
+ await user.selectOptions(jobTitleSelect, "title-2");
+
+ // Wait for form to be dirty and valid
+ await waitFor(() => {
+ const saveButton = screen.getByRole("button", { name: /save/i });
+ expect(saveButton).not.toBeDisabled();
+ });
+
+ const saveButton = screen.getByRole("button", { name: /save/i });
+ await user.click(saveButton);
+
+ await waitFor(() => {
+ expect(updateExperience).toHaveBeenCalledTimes(1);
+ expect(updateExperience).toHaveBeenCalledWith(
+ expect.objectContaining({
+ id: "exp-1",
+ title: "title-2",
+ })
+ );
+ });
+ });
+
+ it("should close dialog and show success toast on successful submission", async () => {
+ const { toast } = require("@/components/ui/use-toast");
+
+ (addExperience as jest.Mock).mockResolvedValue({
+ success: true,
+ message: "Experience added successfully",
+ });
+
+ render(
+ <AddExperience
+ resumeId={mockResumeId}
+ sectionId={mockSectionId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ await waitFor(() => {
+ expect(screen.getByTestId("combobox-title")).toBeInTheDocument();
+ });
+
+ const jobTitleSelect = screen.getByTestId(
+ "combobox-title"
+ ) as HTMLSelectElement;
+ const companySelect = screen.getByTestId(
+ "combobox-company"
+ ) as HTMLSelectElement;
+ const locationSelect = screen.getByTestId(
+ "combobox-location"
+ ) as HTMLSelectElement;
+ const startDateInput = screen.getByTestId(
+ "datepicker-startDate"
+ ) as HTMLInputElement;
+ const jobDescriptionEditor = screen.getByTestId(
+ "tiptap-editor"
+ ) as HTMLTextAreaElement;
+
+ await user.selectOptions(jobTitleSelect, "title-1");
+ await user.selectOptions(companySelect, "company-1");
+ await user.selectOptions(locationSelect, "location-1");
+ await user.type(startDateInput, "2023-01-01");
+ await user.type(jobDescriptionEditor, "Developed amazing features");
+
+ // Wait for form to be valid
+ await waitFor(() => {
+ const saveButton = screen.getByRole("button", { name: /save/i });
+ expect(saveButton).not.toBeDisabled();
+ });
+
+ const saveButton = screen.getByRole("button", { name: /save/i });
+ await user.click(saveButton);
+
+ await waitFor(() => {
+ expect(mockSetDialogOpen).toHaveBeenCalledWith(false);
+ expect(toast).toHaveBeenCalledWith(
+ expect.objectContaining({
+ variant: "success",
+ description: "Experience has been added successfully",
+ })
+ );
+ });
+ });
+
+ it("should show error toast on failed submission", async () => {
+ const { toast } = require("@/components/ui/use-toast");
+
+ (addExperience as jest.Mock).mockResolvedValue({
+ success: false,
+ message: "Failed to add experience",
+ });
+
+ render(
+ <AddExperience
+ resumeId={mockResumeId}
+ sectionId={mockSectionId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ await waitFor(() => {
+ expect(screen.getByTestId("combobox-title")).toBeInTheDocument();
+ });
+
+ const jobTitleSelect = screen.getByTestId(
+ "combobox-title"
+ ) as HTMLSelectElement;
+ const companySelect = screen.getByTestId(
+ "combobox-company"
+ ) as HTMLSelectElement;
+ const locationSelect = screen.getByTestId(
+ "combobox-location"
+ ) as HTMLSelectElement;
+ const startDateInput = screen.getByTestId(
+ "datepicker-startDate"
+ ) as HTMLInputElement;
+ const jobDescriptionEditor = screen.getByTestId(
+ "tiptap-editor"
+ ) as HTMLTextAreaElement;
+
+ await user.selectOptions(jobTitleSelect, "title-1");
+ await user.selectOptions(companySelect, "company-1");
+ await user.selectOptions(locationSelect, "location-1");
+ await user.type(startDateInput, "2023-01-01");
+ await user.type(jobDescriptionEditor, "Developed amazing features");
+
+ // Wait for form to be valid
+ await waitFor(() => {
+ const saveButton = screen.getByRole("button", { name: /save/i });
+ expect(saveButton).not.toBeDisabled();
+ });
+
+ const saveButton = screen.getByRole("button", { name: /save/i });
+ await user.click(saveButton);
+
+ await waitFor(() => {
+ expect(toast).toHaveBeenCalledWith(
+ expect.objectContaining({
+ variant: "destructive",
+ title: "Error!",
+ description: "Failed to add experience",
+ })
+ );
+ expect(mockSetDialogOpen).not.toHaveBeenCalledWith(false);
+ });
+ });
+
+ it("should show updated success message when editing", async () => {
+ const { toast } = require("@/components/ui/use-toast");
+
+ const mockExperienceToEdit: ResumeSection = {
+ id: "section-1",
+ resumeId: mockResumeId,
+ sectionTitle: "Work Experience",
+ sectionType: "experience" as any,
+ workExperiences: [
+ {
+ id: "exp-1",
+ Company: mockCompanies[0] as any,
+ jobTitle: mockJobTitles[0] as any,
+ location: mockLocations[0] as any,
+ startDate: new Date("2020-01-01"),
+ endDate: new Date("2022-12-31"),
+ currentJob: false,
+ description: "Worked on various projects",
+ },
+ ],
+ };
+
+ (updateExperience as jest.Mock).mockResolvedValue({
+ success: true,
+ message: "Experience updated successfully",
+ });
+
+ render(
+ <AddExperience
+ resumeId={mockResumeId}
+ sectionId={mockSectionId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ experienceToEdit={mockExperienceToEdit}
+ />
+ );
+
+ await waitFor(() => {
+ expect(screen.getByTestId("combobox-title")).toBeInTheDocument();
+ });
+
+ const jobTitleSelect = screen.getByTestId(
+ "combobox-title"
+ ) as HTMLSelectElement;
+ await user.selectOptions(jobTitleSelect, "title-2");
+
+ // Wait for form to be dirty and valid
+ await waitFor(() => {
+ const saveButton = screen.getByRole("button", { name: /save/i });
+ expect(saveButton).not.toBeDisabled();
+ });
+
+ const saveButton = screen.getByRole("button", { name: /save/i });
+ await user.click(saveButton);
+
+ await waitFor(() => {
+ expect(toast).toHaveBeenCalledWith(
+ expect.objectContaining({
+ variant: "success",
+ description: "Experience has been updated successfully",
+ })
+ );
+ });
+ });
+
+ it("should not render dialog when dialogOpen is false", () => {
+ render(
+ <AddExperience
+ resumeId={mockResumeId}
+ sectionId={mockSectionId}
+ dialogOpen={false}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ expect(screen.queryByText("Add Experience")).not.toBeInTheDocument();
+ });
+
+ it("should load companies, job titles, and locations on mount", async () => {
+ render(
+ <AddExperience
+ resumeId={mockResumeId}
+ sectionId={mockSectionId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ await waitFor(() => {
+ expect(getAllCompanies).toHaveBeenCalledTimes(1);
+ expect(getAllJobTitles).toHaveBeenCalledTimes(1);
+ expect(getAllJobLocations).toHaveBeenCalledTimes(1);
+ });
+ });
+
+ it("should display loading indicator when form is submitting", async () => {
+ (addExperience as jest.Mock).mockImplementation(
+ () =>
+ new Promise((resolve) => {
+ setTimeout(() => resolve({ success: true }), 100);
+ })
+ );
+
+ render(
+ <AddExperience
+ resumeId={mockResumeId}
+ sectionId={mockSectionId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ await waitFor(() => {
+ expect(screen.getByTestId("combobox-title")).toBeInTheDocument();
+ });
+
+ const jobTitleSelect = screen.getByTestId("combobox-title");
+ const companySelect = screen.getByTestId("combobox-company");
+ const locationSelect = screen.getByTestId("combobox-location");
+ const startDateInput = screen.getByTestId("datepicker-startDate");
+ const jobDescriptionEditor = screen.getByTestId("tiptap-editor");
+
+ fireEvent.change(jobTitleSelect, { target: { value: "title-1" } });
+ fireEvent.change(companySelect, { target: { value: "company-1" } });
+ fireEvent.change(locationSelect, { target: { value: "location-1" } });
+ fireEvent.change(startDateInput, { target: { value: "2023-01-01" } });
+ fireEvent.change(jobDescriptionEditor, {
+ target: { value: "Developed amazing features" },
+ });
+
+ const saveButton = screen.getByRole("button", { name: /save/i });
+ await user.click(saveButton);
+
+ const loader = screen
+ .getByText(/save/i)
+ .parentElement?.querySelector(".spinner");
+ expect(loader).toBeInTheDocument();
+
+ await waitFor(() => {
+ expect(addExperience).toHaveBeenCalledTimes(1);
+ });
+ });
+});
diff --git a/__tests__/AddResumeSummary.spec.tsx b/__tests__/AddResumeSummary.spec.tsx
new file mode 100644
index 0000000..abca105
--- /dev/null
+++ b/__tests__/AddResumeSummary.spec.tsx
@@ -0,0 +1,431 @@
+import AddResumeSummary from "@/components/profile/AddResumeSummary";
+import {
+ addResumeSummary,
+ updateResumeSummary,
+} from "@/actions/profile.actions";
+import "@testing-library/jest-dom";
+import { screen, render, waitFor, fireEvent } from "@testing-library/react";
+import userEvent from "@testing-library/user-event";
+import { ResumeSection } from "@/models/profile.model";
+
+jest.mock("@/actions/profile.actions", () => ({
+ addResumeSummary: jest.fn(),
+ updateResumeSummary: jest.fn(),
+}));
+
+// Mock TiptapEditor component
+jest.mock("@/components/TiptapEditor", () => {
+ return function TiptapEditor({ field }: any) {
+ return (
+ <textarea
+ data-testid="tiptap-editor"
+ value={field.value || ""}
+ onChange={(e) => field.onChange(e.target.value)}
+ placeholder="Enter summary content"
+ />
+ );
+ };
+});
+
+// Mock toast
+jest.mock("@/components/ui/use-toast", () => ({
+ toast: jest.fn(),
+}));
+
+describe("AddResumeSummary Component", () => {
+ const mockSetDialogOpen = jest.fn();
+ const mockResumeId = "resume-123";
+ const user = userEvent.setup();
+
+ beforeEach(() => {
+ jest.clearAllMocks();
+ });
+
+ it("should render Add Summary dialog with correct title", () => {
+ render(
+ <AddResumeSummary
+ resumeId={mockResumeId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ expect(screen.getByText("Add Summary")).toBeInTheDocument();
+ });
+
+ it("should render Edit Summary dialog when summaryToEdit is provided", () => {
+ const mockSummaryToEdit: ResumeSection = {
+ id: "summary-1",
+ resumeId: mockResumeId,
+ sectionTitle: "Professional Summary",
+ sectionType: "summary" as any,
+ summary: {
+ content: "Experienced software developer",
+ },
+ };
+
+ render(
+ <AddResumeSummary
+ resumeId={mockResumeId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ summaryToEdit={mockSummaryToEdit}
+ />
+ );
+
+ expect(screen.getByText("Edit Summary")).toBeInTheDocument();
+ });
+
+ it("should render all form fields correctly", () => {
+ render(
+ <AddResumeSummary
+ resumeId={mockResumeId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ expect(screen.getByLabelText(/section title/i)).toBeInTheDocument();
+ expect(screen.getByText(/resume summary/i)).toBeInTheDocument();
+ expect(screen.getByTestId("tiptap-editor")).toBeInTheDocument();
+ expect(screen.getByRole("button", { name: /cancel/i })).toBeInTheDocument();
+ expect(screen.getByRole("button", { name: /save/i })).toBeInTheDocument();
+ });
+
+ it("should populate form fields when editing a summary", () => {
+ const mockSummaryToEdit: ResumeSection = {
+ id: "summary-1",
+ resumeId: mockResumeId,
+ sectionTitle: "Professional Summary",
+ sectionType: "summary" as any,
+ summary: {
+ content: "Experienced software developer with 5+ years",
+ },
+ };
+
+ render(
+ <AddResumeSummary
+ resumeId={mockResumeId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ summaryToEdit={mockSummaryToEdit}
+ />
+ );
+
+ const sectionTitleInput = screen.getByLabelText(/section title/i);
+ const contentEditor = screen.getByTestId("tiptap-editor");
+
+ expect(sectionTitleInput).toHaveValue("Professional Summary");
+ expect(contentEditor).toHaveValue(
+ "Experienced software developer with 5+ years"
+ );
+ });
+
+ it("should close dialog when Cancel button is clicked", async () => {
+ render(
+ <AddResumeSummary
+ resumeId={mockResumeId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ const cancelButton = screen.getByRole("button", { name: /cancel/i });
+ await user.click(cancelButton);
+
+ expect(mockSetDialogOpen).toHaveBeenCalledWith(false);
+ });
+
+ it("should disable Save button when form is not dirty", () => {
+ render(
+ <AddResumeSummary
+ resumeId={mockResumeId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ const saveButton = screen.getByRole("button", { name: /save/i });
+ expect(saveButton).toBeDisabled();
+ });
+
+ it("should call addResumeSummary when submitting a new summary", async () => {
+ (addResumeSummary as jest.Mock).mockResolvedValue({
+ success: true,
+ message: "Summary created successfully",
+ });
+
+ render(
+ <AddResumeSummary
+ resumeId={mockResumeId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ const sectionTitleInput = screen.getByLabelText(/section title/i);
+ const contentEditor = screen.getByTestId("tiptap-editor");
+
+ await user.clear(sectionTitleInput);
+ await user.type(sectionTitleInput, "Career Summary");
+
+ fireEvent.change(contentEditor, {
+ target: { value: "Experienced professional with strong skills" },
+ });
+
+ const saveButton = screen.getByRole("button", { name: /save/i });
+ await user.click(saveButton);
+
+ await waitFor(() => {
+ expect(addResumeSummary).toHaveBeenCalledTimes(1);
+ expect(addResumeSummary).toHaveBeenCalledWith(
+ expect.objectContaining({
+ sectionTitle: "Career Summary",
+ content: "Experienced professional with strong skills",
+ })
+ );
+ });
+ });
+
+ it("should call updateResumeSummary when editing an existing summary", async () => {
+ const mockSummaryToEdit: ResumeSection = {
+ id: "summary-1",
+ resumeId: mockResumeId,
+ sectionTitle: "Professional Summary",
+ sectionType: "summary" as any,
+ summary: {
+ content: "Experienced software developer",
+ },
+ };
+
+ (updateResumeSummary as jest.Mock).mockResolvedValue({
+ success: true,
+ message: "Summary updated successfully",
+ });
+
+ render(
+ <AddResumeSummary
+ resumeId={mockResumeId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ summaryToEdit={mockSummaryToEdit}
+ />
+ );
+
+ const sectionTitleInput = screen.getByLabelText(/section title/i);
+ await user.clear(sectionTitleInput);
+ await user.type(sectionTitleInput, "Updated Professional Summary");
+
+ const saveButton = screen.getByRole("button", { name: /save/i });
+ await user.click(saveButton);
+
+ await waitFor(() => {
+ expect(updateResumeSummary).toHaveBeenCalledTimes(1);
+ expect(updateResumeSummary).toHaveBeenCalledWith(
+ expect.objectContaining({
+ id: "summary-1",
+ sectionTitle: "Updated Professional Summary",
+ })
+ );
+ });
+ });
+
+ it("should close dialog and show success toast on successful submission", async () => {
+ const { toast } = require("@/components/ui/use-toast");
+
+ (addResumeSummary as jest.Mock).mockResolvedValue({
+ success: true,
+ message: "Summary created successfully",
+ });
+
+ render(
+ <AddResumeSummary
+ resumeId={mockResumeId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ const sectionTitleInput = screen.getByLabelText(/section title/i);
+ const contentEditor = screen.getByTestId("tiptap-editor");
+
+ await user.clear(sectionTitleInput);
+ await user.type(sectionTitleInput, "Career Summary");
+
+ fireEvent.change(contentEditor, {
+ target: { value: "Experienced professional with strong skills" },
+ });
+
+ const saveButton = screen.getByRole("button", { name: /save/i });
+ await user.click(saveButton);
+
+ await waitFor(() => {
+ expect(mockSetDialogOpen).toHaveBeenCalledWith(false);
+ expect(toast).toHaveBeenCalledWith(
+ expect.objectContaining({
+ variant: "success",
+ description: "Summary has been created successfully",
+ })
+ );
+ });
+ });
+
+ it("should show error toast on failed submission", async () => {
+ const { toast } = require("@/components/ui/use-toast");
+
+ (addResumeSummary as jest.Mock).mockResolvedValue({
+ success: false,
+ message: "Failed to create summary",
+ });
+
+ render(
+ <AddResumeSummary
+ resumeId={mockResumeId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ const sectionTitleInput = screen.getByLabelText(/section title/i);
+ const contentEditor = screen.getByTestId("tiptap-editor");
+
+ await user.clear(sectionTitleInput);
+ await user.type(sectionTitleInput, "Career Summary");
+
+ fireEvent.change(contentEditor, {
+ target: { value: "Experienced professional with strong skills" },
+ });
+
+ const saveButton = screen.getByRole("button", { name: /save/i });
+ await user.click(saveButton);
+
+ await waitFor(() => {
+ expect(toast).toHaveBeenCalledWith(
+ expect.objectContaining({
+ variant: "destructive",
+ title: "Error!",
+ description: "Failed to create summary",
+ })
+ );
+ expect(mockSetDialogOpen).not.toHaveBeenCalledWith(false);
+ });
+ });
+
+ it("should display loading indicator when form is submitting", async () => {
+ (addResumeSummary as jest.Mock).mockImplementation(
+ () =>
+ new Promise((resolve) => {
+ setTimeout(() => resolve({ success: true }), 100);
+ })
+ );
+
+ render(
+ <AddResumeSummary
+ resumeId={mockResumeId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ const sectionTitleInput = screen.getByLabelText(/section title/i);
+ const contentEditor = screen.getByTestId("tiptap-editor");
+
+ await user.clear(sectionTitleInput);
+ await user.type(sectionTitleInput, "Career Summary");
+
+ fireEvent.change(contentEditor, {
+ target: { value: "Experienced professional with strong skills" },
+ });
+
+ const saveButton = screen.getByRole("button", { name: /save/i });
+ await user.click(saveButton);
+
+ // Check if the loader icon is present
+ const loader = screen
+ .getByText(/save/i)
+ .parentElement?.querySelector(".spinner");
+ expect(loader).toBeInTheDocument();
+
+ await waitFor(() => {
+ expect(addResumeSummary).toHaveBeenCalledTimes(1);
+ });
+ });
+
+ it("should show updated success message when editing", async () => {
+ const { toast } = require("@/components/ui/use-toast");
+
+ const mockSummaryToEdit: ResumeSection = {
+ id: "summary-1",
+ resumeId: mockResumeId,
+ sectionTitle: "Professional Summary",
+ sectionType: "summary" as any,
+ summary: {
+ content: "Experienced software developer",
+ },
+ };
+
+ (updateResumeSummary as jest.Mock).mockResolvedValue({
+ success: true,
+ message: "Summary updated successfully",
+ });
+
+ render(
+ <AddResumeSummary
+ resumeId={mockResumeId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ summaryToEdit={mockSummaryToEdit}
+ />
+ );
+
+ const sectionTitleInput = screen.getByLabelText(/section title/i);
+ await user.clear(sectionTitleInput);
+ await user.type(sectionTitleInput, "Updated Professional Summary");
+
+ const saveButton = screen.getByRole("button", { name: /save/i });
+ await user.click(saveButton);
+
+ await waitFor(() => {
+ expect(toast).toHaveBeenCalledWith(
+ expect.objectContaining({
+ variant: "success",
+ description: "Summary has been updated successfully",
+ })
+ );
+ });
+ });
+
+ it("should not render dialog when dialogOpen is false", () => {
+ const { container } = render(
+ <AddResumeSummary
+ resumeId={mockResumeId}
+ dialogOpen={false}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ expect(screen.queryByText("Add Summary")).not.toBeInTheDocument();
+ });
+
+ it("should handle dialog open state change", async () => {
+ const { rerender } = render(
+ <AddResumeSummary
+ resumeId={mockResumeId}
+ dialogOpen={false}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ expect(screen.queryByText("Add Summary")).not.toBeInTheDocument();
+
+ rerender(
+ <AddResumeSummary
+ resumeId={mockResumeId}
+ dialogOpen={true}
+ setDialogOpen={mockSetDialogOpen}
+ />
+ );
+
+ expect(screen.getByText("Add Summary")).toBeInTheDocument();
+ });
+});
diff --git a/jest.setup.ts b/jest.setup.ts
index 8d4db84..dff28e1 100644
--- a/jest.setup.ts
+++ b/jest.setup.ts
@@ -4,3 +4,10 @@ import "@testing-library/jest-dom";
if (typeof global.fetch === "undefined") {
global.fetch = jest.fn();
}
+
+// Mock ResizeObserver for Radix UI components
+global.ResizeObserver = class ResizeObserver {
+ observe() {}
+ unobserve() {}
+ disconnect() {}
+};