Skip to content

Commit

Permalink
add header test
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-s-nava committed Nov 6, 2024
1 parent 5b30855 commit a1ac516
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
7 changes: 5 additions & 2 deletions frontend/src/components/search/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,21 @@ export default function SearchBar({ query }: SearchBarProps) {
updateQueryParams("", "query", queryTerm, false);
};

// if we have "refresh=true" query param, clear the input
// this supports the expected refresh of the input if the user clicks the search link while on the search page
useEffect(() => {
if (searchParams.get("refresh") && inputRef.current) {
updateQueryTerm("");
inputRef.current.value = "";
}
}, [searchParams, updateQueryTerm]);

// removes the "refresh" param once a user has dirtied the input
useEffect(() => {
if (searchParams.get("refresh") && queryTerm) {
if (searchParams.get("refresh") && inputRef.current?.value) {
updateQueryParams("", "refresh");
}
}, [queryTerm, searchParams, updateQueryParams]);
}, [searchParams, updateQueryParams]);

return (
<div className="margin-top-5 margin-bottom-2">
Expand Down
30 changes: 19 additions & 11 deletions frontend/tests/components/Header.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,10 @@ import Header from "src/components/Header";

const props = {
logoPath: "/img/logo.svg",
primaryLinks: [
{
i18nKey: "nav_link_home",
href: "/",
},
{
i18nKey: "nav_link_health",
href: "/health",
},
],
locale: "en",
};

const mockedPath = "/fakepath";
let mockedPath = "/fakepath";

const getMockedPath = () => mockedPath;

Expand Down Expand Up @@ -66,4 +57,21 @@ describe("Header", () => {

expect(govBanner).toHaveAttribute("aria-expanded", "true");
});

it("displays a search link without refresh param if not currently on search page", () => {
render(<Header />);

const searchLink = screen.getByRole("link", { name: "Search" });
expect(searchLink).toBeInTheDocument();
expect(searchLink).toHaveAttribute("href", "/search");
});

it("displays a search link with refresh param if currently on search page", () => {
mockedPath = "/search";
render(<Header />);

const searchLink = screen.getByRole("link", { name: "Search" });
expect(searchLink).toBeInTheDocument();
expect(searchLink).toHaveAttribute("href", "/search?refresh=true");
});
});

0 comments on commit a1ac516

Please sign in to comment.