Skip to content

Commit

Permalink
[OUR415-314] Improves our test story with test templates and helpers …
Browse files Browse the repository at this point in the history
…for search; adds `BrowserRefinementList` component test (#259)
  • Loading branch information
rosschapman authored Nov 6, 2024
1 parent fe6c1b4 commit 9aee08b
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 4 deletions.
45 changes: 45 additions & 0 deletions app/components/search/Refinements/BrowseRefinementList.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import React from "react";
import { InstantSearch } from "react-instantsearch-core";
import { render, screen, waitFor } from "@testing-library/react";
import BrowseRefinementList from "components/search/Refinements/BrowseRefinementList";
import { createSearchClient } from "../../../../test/helpers/createSearchClient";

describe("BrowseRefinementList", () => {
test("renders the default limit of 10 refinements", async () => {
const searchClient = createSearchClient({
facets: {
categories: {
A: 54,
B: 35,
C: 28,
D: 24,
E: 18,
F: 14,
G: 14,
H: 12,
I: 45,
J: 79,
K: 1,
L: 31,
},
},
});

const expected = 10;

render(
<InstantSearch
searchClient={searchClient}
indexName="fake_test_search_index"
>
<BrowseRefinementList attribute="categories" />
</InstantSearch>
);

await waitFor(() => {
expect(screen.getAllByTestId("browserefinementlist-item")).toHaveLength(
expected
);
});
});
});
2 changes: 1 addition & 1 deletion app/components/search/Refinements/BrowseRefinementList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const BrowseRefinementList = ({ attribute, transform }: Props) => {
return (
<ul>
{items.map((item) => (
<li key={item.label}>
<li key={item.label} data-testid={"browserefinementlist-item"}>
<label className={styles.checkBox}>
{item.label}
<input
Expand Down
4 changes: 1 addition & 3 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,7 @@ const config: Config = {
// ],

// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
// testPathIgnorePatterns: [
// "/node_modules/"
// ],
testPathIgnorePatterns: ["/templates"],

// The regexp pattern or array of patterns that Jest uses to detect test files
// testRegex: [],
Expand Down
50 changes: 50 additions & 0 deletions test/helpers/createSearchClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
interface Options {
[key: string]: any;
}

/**
* Generates search client fixture for testing components that rely on hooks
* from the Algolia instantsearch library.
*
* Example usage:
* ```
* const searchClient = createSearchClient({
* facets: {
* categories: {
* "Arts, Culture & Identity": 54,
* Education: 28,
* },
* },
* });
*
* render(
* <InstantSearch
* searchClient={searchClient}
* indexName="fake_test_search_index"
* >
* <BrowseRefinementList attribute="categories" />
* </InstantSearch>
* );
* ```
*
* @param options Additional customizations of the search response
* @returns
*/
export function createSearchClient(options: Options) {
return {
search: (requests: any) =>
Promise.resolve({
results: requests.map(() => ({
hits: [],
page: 0,
nbHits: 0,
nbPages: 0,
hitsPerPage: 0,
processingTimeMS: 1,
params: "",
query: "",
...options,
})),
}),
};
}
25 changes: 25 additions & 0 deletions test/templates/Template.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Template for a simple component test
*
* Usage: Duplicate this file and rename it using your component name. Happy
* coding from there!
*/
import React from "react";
import { render, screen } from "@testing-library/react";
import { BrowserRouter } from "react-router-dom";

// Normally imported from components directory
const MyFakeComponent = ({ text }: { text: string }) => <>{text}</>;

describe("<MyFakeComponent />", () => {
const parameters = {
text: "expected content",
};

it("renders", () => {
render(<MyFakeComponent {...parameters} />, { wrapper: BrowserRouter });
expect(screen.getByTestId("my-component-test-id")).toHaveTextContent(
parameters.text
);
});
});

0 comments on commit 9aee08b

Please sign in to comment.