Skip to content

Commit

Permalink
Parameterizes the clear search button
Browse files Browse the repository at this point in the history
  • Loading branch information
rosschapman committed Nov 7, 2024
1 parent 0d6d094 commit 55d8554
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/components/search/Refinements/ClearSearchButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const ClearSearchButton = () => {
mobileFullWidth={false}
onClick={handleOnClick}
>
Clear Search
<span data-testid={"clear-search-button"}>Clear Search</span>
</Button>
);
};
Expand Down
1 change: 0 additions & 1 deletion app/components/search/SearchResults/SearchResult.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React, { forwardRef } from "react";
import { TransformedSearchHit } from "models";
import { Link } from "react-router-dom";
import { LabelTag } from "components/ui/LabelTag";
import { Tooltip } from "react-tippy";
import { formatPhoneNumber, renderAddressMetadata } from "utils";
import { removeAsterisksAndHashes } from "utils/strings";
import ReactMarkdown from "react-markdown";
Expand Down
33 changes: 33 additions & 0 deletions app/components/search/SearchResults/SearchResults.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from "react";
import { InstantSearch } from "react-instantsearch-core";
import { render, screen, waitFor } from "@testing-library/react";
import SearchResults from "components/search/SearchResults/SearchResults";
import { createSearchClient } from "../../../../test/helpers/createSearchClient";
import ClearSearchButton from "components/search/Refinements/ClearSearchButton";

describe("SearchResults", () => {
test("renders a Clear Search button if passed as a param", async () => {
const searchClient = createSearchClient();

render(
<InstantSearch
searchClient={searchClient}
indexName="fake_test_search_index"
initialUiState={{
fake_test_search_index: {
query: "fake query",
},
}}
>
<SearchResults
mobileMapIsCollapsed={false}
clearSearchButton={<ClearSearchButton />}
/>
</InstantSearch>
);

await waitFor(() => {
expect(screen.getByTestId("clear-search-button")).toBeInTheDocument();
});
});
});
17 changes: 12 additions & 5 deletions app/components/search/SearchResults/SearchResults.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback } from "react";
import React, { ReactNode, useCallback } from "react";
import { SearchMap } from "components/search/SearchMap/SearchMap";
import { SearchResult } from "components/search/SearchResults/SearchResult";
import {
Expand All @@ -11,18 +11,25 @@ import {
useSearchBox,
} from "react-instantsearch";
import styles from "./SearchResults.module.scss";
import ClearSearchButton from "../Refinements/ClearSearchButton";
import { Loader } from "components/ui";
import { Loader } from "components/ui/Loader";
import ResultsPagination from "components/search/Pagination/ResultsPagination";

export enum SearchMapActions {
SearchThisArea,
}

/**
* Renders the search results list and the map
*
* @property clearSearchButton Must be a ClearSearchButton component
* @returns
*/
const SearchResults = ({
mobileMapIsCollapsed,
clearSearchButton,
}: {
mobileMapIsCollapsed: boolean;
clearSearchButton?: ReactNode;
}) => {
const { refine: refinePagination } = usePagination();
const {
Expand Down Expand Up @@ -51,15 +58,15 @@ const SearchResults = ({
<br /> Try a different location, filter, or search term.
</div>

{query && <ClearSearchButton />}
{query && clearSearchButton}
</div>
);

const searchResultsHeader = () => {
return (
<div className={styles.searchResultsHeader}>
<h2>{searchResults.nbHits} results</h2>
<ClearSearchButton />
{clearSearchButton}
</div>
);
};
Expand Down
6 changes: 5 additions & 1 deletion app/pages/SearchResultsPage/SearchResultsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import styles from "./SearchResultsPage.module.scss";
import { DEFAULT_AROUND_PRECISION, useAppContext } from "utils";
import { Configure } from "react-instantsearch-core";
import classNames from "classnames";
import ClearSearchButton from "components/search/Refinements/ClearSearchButton";

// NOTE: The .searchResultsPage is added plain so that it can be targeted by print-specific css
export const SearchResultsPage = () => {
Expand All @@ -28,7 +29,10 @@ export const SearchResultsPage = () => {
/>

<div className={styles.results}>
<SearchResults mobileMapIsCollapsed={isMapCollapsed} />
<SearchResults
mobileMapIsCollapsed={isMapCollapsed}
clearSearchButton={<ClearSearchButton />}
/>
</div>
</div>
</div>
Expand Down
3 changes: 3 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ const config: Config = {
moduleNameMapper: {
// Ensures style imports don't break tests
"\\.(css|scss)$": "<rootDir>/test/jest/__mocks__/styleMock.ts",
"react-markdown": "<rootDir>/test/jest/__mocks__/react-markdown.tsx",
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$":
"<rootDir>/test/jest/__mocks__/fileMock.js",
},

// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/createSearchClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ interface Options {
* @param options Additional customizations of the search response
* @returns
*/
export function createSearchClient(options: Options) {
export function createSearchClient(options?: Options) {
return {
search: (requests: any) =>

Check warning on line 35 in test/helpers/createSearchClient.ts

View workflow job for this annotation

GitHub Actions / build_and_test_app

Unexpected any. Specify a different type
Promise.resolve({
Expand Down
3 changes: 3 additions & 0 deletions test/jest/__mocks__/fileMock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Webpack is not available in tests to handle the css import parsing and handling. Our test config tells jest to
// replace any style imports it sees with this empty object.
export default "";

Check warning on line 3 in test/jest/__mocks__/fileMock.ts

View workflow job for this annotation

GitHub Actions / build_and_test_app

Assign literal to a variable before exporting as module default
11 changes: 11 additions & 0 deletions test/jest/__mocks__/react-markdown.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from "react";

const ReactMarkdown = ({
children,
}: {
children: string | JSX.Element | JSX.Element[] | (() => JSX.Element);
}) => {
return <>{children}</>;
};

export default ReactMarkdown;

0 comments on commit 55d8554

Please sign in to comment.