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.

Write tests for add company component

Khuram Niaz30b4520f9a63
diff --git a/__tests__/AddCompany.spec.tsx b/__tests__/AddCompany.spec.tsx
new file mode 100644
index 0000000..c2638b3
--- /dev/null
+++ b/__tests__/AddCompany.spec.tsx
@@ -0,0 +1,154 @@
+import { addCompany, updateCompany } from "@/actions/company.actions";
+import AddCompany from "@/components/admin/AddCompany";
+import "@testing-library/jest-dom";
+import {
+ screen,
+ render,
+ waitFor,
+ fireEvent,
+ act,
+} from "@testing-library/react";
+
+jest.mock("@/actions/company.actions", () => ({
+ addCompany: jest.fn(),
+ updateCompany: jest.fn(),
+}));
+
+describe("AddCompany Component", () => {
+ const mockReloadCompanies = jest.fn();
+ const mockResetEditCompany = jest.fn();
+
+ beforeEach(() => {
+ jest.clearAllMocks();
+ });
+
+ it("should render Add Company button", async () => {
+ render(
+ <AddCompany
+ reloadCompanies={mockReloadCompanies}
+ resetEditCompany={mockResetEditCompany}
+ />
+ );
+ expect(
+ screen.getByRole("button", { name: /add company/i })
+ ).toBeInTheDocument();
+ });
+
+ it("should open dialog when Add Company button is clicked", async () => {
+ render(
+ <AddCompany
+ reloadCompanies={mockReloadCompanies}
+ resetEditCompany={mockResetEditCompany}
+ />
+ );
+ const addCompanyButton = screen.getByRole("button", {
+ name: /add company/i,
+ });
+ fireEvent.click(addCompanyButton);
+
+ const dialog = await screen.findByRole("dialog");
+ expect(dialog).toBeInTheDocument();
+ });
+
+ it("should populate form fields when editing a company", async () => {
+ const mockEditCompany = {
+ id: "company-id",
+ label: "Test Company",
+ value: "test company",
+ createdBy: "user-id",
+ logoUrl: "http://example.com/logo.png",
+ };
+
+ render(
+ <AddCompany
+ reloadCompanies={mockReloadCompanies}
+ resetEditCompany={mockResetEditCompany}
+ editCompany={mockEditCompany}
+ />
+ );
+
+ const addCompanyButton = await screen.findByTestId("add-company-btn");
+ fireEvent.click(addCompanyButton);
+
+ const dialog = await screen.findByText(/edit company/i);
+ expect(dialog).toBeInTheDocument();
+ expect(screen.getByLabelText(/company name/i)).toHaveValue("Test Company");
+ expect(screen.getByLabelText(/company logo url/i)).toHaveValue(
+ "http://example.com/logo.png"
+ );
+ });
+
+ it("should call addCompany function when submitting the form for new company", async () => {
+ render(
+ <AddCompany
+ reloadCompanies={mockReloadCompanies}
+ resetEditCompany={mockResetEditCompany}
+ />
+ );
+
+ const addCompanyButton = await screen.findByTestId("add-company-btn");
+ fireEvent.click(addCompanyButton);
+
+ const companyNameInput = screen.getByLabelText(/company name/i);
+ fireEvent.change(companyNameInput, {
+ target: { value: "New Test Company" },
+ });
+ const companyLogoUrlInput = screen.getByLabelText(/company logo url/i);
+ fireEvent.change(companyLogoUrlInput, {
+ target: { value: "http://example.com/new-logo.png" },
+ });
+ const saveButton = screen.getByRole("button", { name: /save/i });
+ fireEvent.click(saveButton);
+
+ await waitFor(() => {
+ expect(addCompany).toHaveBeenCalledTimes(1);
+ expect(addCompany).toHaveBeenCalledWith({
+ company: "New Test Company",
+ logoUrl: "http://example.com/new-logo.png",
+ });
+ });
+ });
+
+ it("should call updateCompany function when submitting the form for editing existing company", async () => {
+ const mockEditCompany = {
+ id: "company-id",
+ label: "Test Company",
+ value: "test-company",
+ createdBy: "user-id",
+ logoUrl: "http://example.com/logo.png",
+ };
+
+ render(
+ <AddCompany
+ reloadCompanies={mockReloadCompanies}
+ resetEditCompany={mockResetEditCompany}
+ editCompany={mockEditCompany}
+ />
+ );
+
+ const addCompanyButton = await screen.findByTestId("add-company-btn");
+
+ fireEvent.click(addCompanyButton);
+
+ const companyNameInput = screen.getByLabelText(/company name/i);
+ fireEvent.change(companyNameInput, {
+ target: { value: "Edited Test Company" },
+ });
+
+ const companyLogoUrlInput = screen.getByLabelText(/company logo url/i);
+ fireEvent.change(companyLogoUrlInput, {
+ target: { value: "http://example.com/edited-logo.png" },
+ });
+
+ const saveButton = screen.getByRole("button", { name: /save/i });
+ fireEvent.click(saveButton);
+
+ await waitFor(() => {
+ expect(updateCompany).toHaveBeenCalledTimes(1);
+ expect(updateCompany).toHaveBeenCalledWith({
+ company: "Edited Test Company",
+ logoUrl: "http://example.com/edited-logo.png",
+ });
+ });
+ });
+});
diff --git a/__tests__/SigninForm.spec.tsx b/__tests__/SigninForm.spec.tsx
index 6c3d10d..6e17317 100644
--- a/__tests__/SigninForm.spec.tsx
+++ b/__tests__/SigninForm.spec.tsx
@@ -3,7 +3,6 @@ import SigninForm from "@/components/auth/SigninForm";
import "@testing-library/jest-dom";
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
import { useRouter } from "next/navigation";
-import React from "react";
// Mock the external dependencies
jest.mock("@/actions/auth.actions", () => ({
diff --git a/src/components/admin/AddCompany.tsx b/src/components/admin/AddCompany.tsx
index f3e5489..6c65ff7 100644
--- a/src/components/admin/AddCompany.tsx
+++ b/src/components/admin/AddCompany.tsx
@@ -75,11 +75,11 @@ function AddCompany({
const res = editCompany
? await updateCompany(data)
: await addCompany(data);
- if (!res.success) {
+ if (!res?.success) {
toast({
variant: "destructive",
title: "Error!",
- description: res.message,
+ description: res?.message,
});
} else {
reset();
@@ -97,7 +97,12 @@ function AddCompany({
return (
<>
- <Button size="sm" className="h-8 gap-1" onClick={addCompanyForm}>
+ <Button
+ size="sm"
+ className="h-8 gap-1"
+ onClick={addCompanyForm}
+ data-testid="add-company-btn"
+ >
<PlusCircle className="h-3.5 w-3.5" />
<span className="sr-only sm:not-sr-only sm:whitespace-nowrap">
Add Company