Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OUR415-314] Improves our test story with test templates and helpers for search; adds BrowserRefinementList component test #259

Merged
merged 7 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 correct the number of categories", async () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question (non-blocking): There are 12 categories in searchClient but expected is 10. Is there anything in the repo that explains this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. I updated the test name to indicate more explicitly what this is testing.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick (non-blocking): Could you remove the second "the"?

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
);
});
});
});
4 changes: 3 additions & 1 deletion app/components/search/Refinements/BrowseRefinementList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ const BrowseRefinementList = ({ attribute, transform }: Props) => {
setChecked(checked);
};

// console.log(items);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chore: Let's get rid of this


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;

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

View workflow job for this annotation

GitHub Actions / build_and_test_app

Unexpected any. Specify a different type
}

/**
* 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) =>

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question (if-minor): In the interest of minimizing lint warnings, can we change these 2 'any' types in this file or can we lint ignore this file?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops yeah, forgot to add ignore statements. I was going to clean up all the warnings in a separate PR so I can do it there.

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: Good idea

*
* 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
);
});
});
Loading