diff --git a/react-app/src/components/BreadCrumb/BreadCrumb.test.tsx b/react-app/src/components/BreadCrumb/BreadCrumb.test.tsx index 06d257211c..691aa7c2de 100644 --- a/react-app/src/components/BreadCrumb/BreadCrumb.test.tsx +++ b/react-app/src/components/BreadCrumb/BreadCrumb.test.tsx @@ -1,9 +1,12 @@ import { describe, test, expect, beforeEach } from "vitest"; import { render, screen } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; -import { BreadCrumb, BreadCrumbBar } from "./BreadCrumb"; +import { BreadCrumb, BreadCrumbBar, BreadCrumbs } from "./BreadCrumb"; import { BrowserRouter, Route, Routes, useLocation } from "react-router-dom"; +import { optionCrumbsFromPath } from "./create-breadcrumbs"; +import { Authority } from "shared-types"; + export const LocationDisplay = () => { const location = useLocation(); @@ -66,5 +69,36 @@ describe("Bread Crumb Tests", () => { }); }); - // TODO: Write a test to test the functionality of the BreadCrumbs component with a test config passed in + describe("Create Bread Crumbs From Path", async () => { + test("return the dashboard crum as the first breadcrumb", () => { + const result = optionCrumbsFromPath("/details/package-id"); + + expect(result[0].displayText).toEqual("Dashboard"); + expect(result[0].to).toEqual("/dashboard"); + }); + + test("when a path is a new submission; newSubmissionPageRouteMapper is used", () => { + const result = optionCrumbsFromPath("/new-submission/spa/medicaid/create?origin=spas"); + + const medicaid = { + to: "/new-submission/spa/medicaid", + displayText: "Medicaid SPA Type", + }; + + expect(result.length).toBe(4); // dashboard > submission type > SPA > medicaid + expect(result[3].to).toBe(medicaid.to); + expect(result[3].displayText).toBe(medicaid.displayText); + }); + + test("optionCrumbsFromPath creates config passed into component & displays correct bread crumbs ", () => { + const path = "/details/Medicaid%20SPA/MD-24-0114-P"; + const testConfig = optionCrumbsFromPath(path, "Medicaid SPA" as Authority); + + render(, { wrapper: BrowserRouter }); + + const dashboardBreadCrum = screen.getByRole("link", { name: "Dashboard" }); + expect(dashboardBreadCrum).toBeInTheDocument(); + expect(dashboardBreadCrum).toHaveAttribute("href", "/dashboard?tab=spas"); + }); + }); }); diff --git a/react-app/src/components/SearchForm/SearchForm.test.tsx b/react-app/src/components/SearchForm/SearchForm.test.tsx new file mode 100644 index 0000000000..050dec5bb3 --- /dev/null +++ b/react-app/src/components/SearchForm/SearchForm.test.tsx @@ -0,0 +1,82 @@ +// Search.test.js +import { render, screen, fireEvent, waitFor } from "@testing-library/react"; +import { describe, expect, test, vi, afterEach } from "vitest"; +import { SearchForm } from "."; +import userEvent from "@testing-library/user-event"; + +const mockHandleSearch = vi.fn(); + +describe("Search Component", () => { + afterEach(() => { + mockHandleSearch.mockClear(); + }); + + test("renders input and svg", () => { + render(); + + expect( + screen.getByLabelText("Search by Package ID, CPOC Name, or Submitter Name"), + ).toBeInTheDocument(); + }); + + test("can type in input field", () => { + render(); + + const input = screen.getByLabelText("Search by Package ID, CPOC Name, or Submitter Name"); + fireEvent.change(input, { target: { value: "Test query" } }); + + expect(input).toHaveValue("Test query"); + }); + + test("calls handleSearch onChange", async () => { + render(); + + const input = screen.getByLabelText("Search by Package ID, CPOC Name, or Submitter Name"); + + fireEvent.change(input, { target: { value: "Test query" } }); + + await waitFor(() => { + expect(mockHandleSearch).toHaveBeenCalledWith("Test query"); + }); + }); + + test("calls handleSearch onSubmit", async () => { + render(); + + const input = screen.getByLabelText("Search by Package ID, CPOC Name, or Submitter Name"); + + fireEvent.change(input, { target: { value: "Test query" } }); + // this triggers the user clicking the 'enter' key + userEvent.type(input, "{enter}"); + + await waitFor(() => { + expect(mockHandleSearch).toHaveBeenCalledWith("Test query"); + }); + }); + + test("x button appears after text is inputed", () => { + render(); + + const input = screen.getByLabelText("Search by Package ID, CPOC Name, or Submitter Name"); + fireEvent.change(input, { target: { value: "Test query" } }); + + const xButton = screen.getByTestId("close-icon"); + expect(xButton).toBeInTheDocument(); + }); + + test("clicking x clears the text input", async () => { + render(); + const input = screen.getByLabelText("Search by Package ID, CPOC Name, or Submitter Name"); + fireEvent.change(input, { target: { value: "Test query" } }); + + const xButton = screen.getByTestId("close-icon"); + expect(xButton).toBeInTheDocument(); + + fireEvent.click(xButton); + + await waitFor(() => { + expect(input).toHaveValue(""); + expect(mockHandleSearch).toHaveBeenCalledWith(""); + }); + }); +}); diff --git a/react-app/src/components/SearchForm/index.tsx b/react-app/src/components/SearchForm/index.tsx index 17da007f56..24c8a98aa7 100644 --- a/react-app/src/components/SearchForm/index.tsx +++ b/react-app/src/components/SearchForm/index.tsx @@ -67,6 +67,7 @@ export const SearchForm: FC<{ {!!searchText && ( { setSearchText(""); handleSearch("");