-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from 5 commits
e7d924a
4be2bd8
8f8a966
970b00a
3cba11c
40f3ce7
bf2dfa4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,10 +39,12 @@ const BrowseRefinementList = ({ attribute, transform }: Props) => { | |
setChecked(checked); | ||
}; | ||
|
||
// console.log(items); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
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) => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
})), | ||
}), | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/** | ||
* Template for a simple component test | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
); | ||
}); | ||
}); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.