Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
32 changes: 7 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,42 +41,24 @@ Shared helpers and fixtures live in `app/test/`:
- `setup.ts` adds the [jest-dom](https://github.com/testing-library/jest-dom)
matchers (e.g. `toBeInTheDocument`) and clears the DOM and `localStorage`
after every test.
- `renderWithQueryClient.tsx` renders components inside a
`QueryClientProvider`, which components that use `useBooks` need.
- `fixtures/` holds saved Open Library responses (a search and a work) and a
sample book.
- `fixtures/` holds a sample book.

### What is tested

We test where there is logic, state or several parts working together. Static
markup (like `Header` and `Footer`) is not tested: such tests break on every
intended change without catching real bugs.

- **Unit tests** for the API layer (`app/api/openLibrary.test.ts`): `toBook`
and `toDescription` map Open Library data to our types (with fallbacks for
missing fields), and `searchBooks` and `fetchWork` build the right request
and throw on HTTP errors.
- **Component tests** for `BookCard`, `BookDetails`, `FavoriteButton`,
`Pagination`, `SearchBar` and `BookGrid`: rendering based on props, user
interaction (clicks, typing) and state (favorites in `localStorage`; loading,
error and empty states; paging; loading the description).
- **Component tests** for `BookCard`, `FavoriteButton`, `Pagination` and
`SearchBar`: rendering based on props, user interaction (clicks, typing)
and state (favorites in `localStorage`, paging).
- **Snapshot test** for `BookCard`. The rendered HTML is stored in
`__snapshots__/` next to the test and compared on every run. If a markup
change is intended, update the snapshot with `pnpm test -u`.
- **Integration test** for `App`: selecting a book shows its details and
description, and going back shows the grid again without a new API request.

### Mocking

No test sends requests to openlibrary.org:

- `openLibrary.test.ts` replaces the global `fetch` with a mock (`vi.spyOn`)
that answers with the saved responses in `app/test/fixtures/`.
- The `App` and `BookDetails` tests replace `searchBooks` and `fetchWork`
with mocks (`vi.mock`), so the components receive the fixture data
(`searchResponse.json`, `workResponse.json`) right away.
- `BookGrid` does not fetch anything itself; its tests pass a fake query
result in as a prop.
The page tests (home and selectedBook) were removed when the frontend moved from Open Library to our GraphQL API. They will be rewritten with Apollo's MockedProvider in #33. The remaining component tests get their data through props, so they do not need any mocking.

### Browsers and devices

Expand Down Expand Up @@ -145,5 +127,5 @@ short, it was used for:
the `description` field of a work.

Where AI contributed directly to a piece of code, there is a comment saying
so, for example in [app/api/types.ts](app/api/types.ts) and
[app/root.tsx](app/root.tsx).
so, for example in [frontend/app/api/types/index.ts](frontend/app/api/types/index.ts) and
[frontend/app/root.tsx](frontend/app/root.tsx).
73 changes: 0 additions & 73 deletions frontend/app/routes/book/selectedBook.test.tsx

This file was deleted.

59 changes: 0 additions & 59 deletions frontend/app/routes/home.test.tsx

This file was deleted.

4 changes: 1 addition & 3 deletions frontend/app/test/fixtures/books.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { Book } from "~/api/types";

// Sample book for component tests. Matches the first doc in searchResponse.json
// and the work in workResponse.json. The id has no "/works/" prefix, the same
// way toBook() maps an Open Library doc (app/api/openLibrary.ts).
// Sample book for component tests.
const bookWithCover: Book = {
id: "OL8400950W",
title: "The Blade Itself",
Expand Down
46 changes: 0 additions & 46 deletions frontend/app/test/fixtures/searchResponse.json

This file was deleted.

33 changes: 0 additions & 33 deletions frontend/app/test/fixtures/workResponse.json

This file was deleted.

32 changes: 0 additions & 32 deletions frontend/app/test/renderWithQueryClient.tsx

This file was deleted.