diff --git a/src/App.test.tsx b/src/App.test.tsx index 9821139..7b7911c 100644 --- a/src/App.test.tsx +++ b/src/App.test.tsx @@ -15,6 +15,7 @@ import { describe("App", () => { beforeEach(() => { sessionStorage.clear(); + localStorage.clear(); resetSearchRequestCount(); }); @@ -121,9 +122,24 @@ describe("App", () => { expect(screen.getByRole("combobox", { name: "Subject" })).toHaveValue("fantasy"); }); + it("saves, restores, and removes a favorite", async () => { + const firstRender = withProviders(); + await screen.findByRole("heading", { name: "The Hobbit" }); + + fireEvent.click(screen.getByRole("button", { name: "Add The Hobbit to favorites" })); + expect(localStorage.getItem("t19.favorites")).toBe(JSON.stringify(["/works/OL1"])); + + firstRender.unmount(); + withProviders(); + await screen.findByRole("button", { name: "Remove The Hobbit from favorites list" }); + + fireEvent.click(screen.getByRole("button", { name: "Remove The Hobbit from favorites list" })); + expect(localStorage.getItem("t19.favorites")).toBe(JSON.stringify([])); + }); + it("matches the snapshot", async () => { const { container } = withProviders(); - await screen.findByRole("heading", { level: 2 }); + await screen.findByRole("heading", { level: 2, name: "The Hobbit" }); expect(container).toMatchSnapshot(); }); }); diff --git a/src/App.tsx b/src/App.tsx index a7c2db0..2198822 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -2,18 +2,20 @@ import { useState } from "react"; import { BookCard } from "./components/BookCard"; import { BookJumpList } from "./components/BookJumpList"; import { NavigationControls } from "./components/NavigationControls"; +import { FavoritesView } from "./components/FavoritesView"; import { SubjectFilter } from "./components/SubjectFilter"; import { useBooks } from "./hooks/useBooks"; +import { useFavorites } from "./hooks/useFavorites"; import { usePreferences } from "./hooks/usePreferences"; import "./App.css"; function App() { const { subject, setSubject } = usePreferences(); const { books, isLoading, isError } = useBooks(subject); - const [index, setIndex] = useState(0); const safeIndex = books.length > 0 ? Math.min(index, books.length - 1) : 0; const current = books[safeIndex]; + const { favoriteKeys, toggleFavorite, removeFavorite } = useFavorites(); return (
@@ -30,7 +32,11 @@ function App() { )} {current && (
- + toggleFavorite(current.key)} + />
)} + {!isLoading && !isError && ( + + )}

Data from the OpenLibrary API.

diff --git a/src/__snapshots__/App.test.tsx.snap b/src/__snapshots__/App.test.tsx.snap index 10bad78..77d621e 100644 --- a/src/__snapshots__/App.test.tsx.snap +++ b/src/__snapshots__/App.test.tsx.snap @@ -99,6 +99,14 @@ exports[`App > matches the snapshot 1`] = ` dragons +