-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.js
47 lines (42 loc) · 1.22 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import React from "react";
import { render, fireEvent, screen, waitFor } from "@testing-library/react";
import { useSearch } from "./index";
describe("useSearch", () => {
const Search = () => {
const [searchTerm, setSearchTerm, results, loading, error] = useSearch(
"blog"
);
return (
<div>
<input
value={searchTerm}
onChange={(e) => setSearchTerm(e.target.value)}
placeholder="Search"
/>
{loading && "Loading..."}
{error && `Oh no! Something went wrong.`}
{!(loading || error) && (
<ul>
{results.map((result) => (
// Result data here matches what is provided in the
// `response` object when building the index.
<li key={result.id}>{result.name}</li>
))}
</ul>
)}
</div>
);
};
it("returns search results", async () => {
global.fetch.once(
JSON.stringify({
results: [{ id: "123", name: "Lorem ipsum" }],
})
);
render(<Search />);
fireEvent.change(screen.getByPlaceholderText("Search"), {
target: { value: "Lorem" },
});
await waitFor(() => expect(screen.getByText("Lorem ipsum")));
});
});