Skip to content

Commit b5c944a

Browse files
fix: filtering list must reset pagination (#364)
1 parent 29bb32f commit b5c944a

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

pwa/components/book/Filters.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const Filters: FunctionComponent<Props> = ({ filters, mutation }) => (
1919
enableReinitialize={true}
2020
onSubmit={(values, { setSubmitting, setStatus, setErrors }) => {
2121
mutation.mutate(
22-
values,
22+
{ ...values, page: 1 },
2323
{
2424
onSuccess: () => {
2525
setStatus({

pwa/components/book/List.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export const List: NextPage<Props> = ({ data, hubURL, filters, page }) => {
6262
value={filters.order?.title ?? ""}
6363
displayEmpty
6464
onChange={(event) => {
65-
filtersMutation.mutate({ ...filters, order: event.target.value ? { title: event.target.value } : undefined });
65+
filtersMutation.mutate({ ...filters, page: 1, order: event.target.value ? { title: event.target.value } : undefined });
6666
}}
6767
>
6868
<MenuItem value="">Relevance</MenuItem>

pwa/tests/BooksList.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,25 @@ test.describe("Books list", () => {
106106
await expect(page.getByTestId("nb-books")).toHaveText(`${totalBooks} book(s) found`);
107107
await expect(page.getByTestId("book").or(page.getByTestId("loading"))).toHaveCount(30);
108108

109+
// filtering must reset the pagination
110+
await page.getByLabel("Go to next page").click();
111+
await expect(page).toHaveURL(/\/books\?page=2$/);
112+
await expect(page.getByTestId("book").or(page.getByTestId("loading"))).toHaveCount(30);
113+
await expect(await bookPage.getDefaultBook()).not.toBeVisible();
114+
await bookPage.filter({ author: "Dan Simmons" });
115+
await expect(page).toHaveURL(/\/books\?author=Dan\+Simmons/);
116+
await expect(page.getByTestId("nb-books")).toHaveText("1 book(s) found");
117+
await expect(page.getByTestId("book").or(page.getByTestId("loading"))).toHaveCount(1);
118+
await expect(page.getByTestId("pagination")).toHaveCount(0);
119+
await expect(await bookPage.getDefaultBook()).toBeVisible();
120+
121+
// clear author field
122+
await page.getByTestId("filter-author").clear();
123+
await expect(page.getByTestId("filter-author")).toHaveValue("");
124+
await expect(page).toHaveURL(/\/books$/);
125+
await expect(page.getByTestId("nb-books")).toHaveText(`${totalBooks} book(s) found`);
126+
await expect(page.getByTestId("book").or(page.getByTestId("loading"))).toHaveCount(30);
127+
109128
// filter by title, author and condition
110129
await bookPage.filter({ author: "Dan Simmons", title: "Hyperion", condition: "Used" });
111130
await expect(page).toHaveURL(/\/books\?author=Dan\+Simmons&title=Hyperion&condition%5B%5D=https%3A%2F%2Fschema\.org%2FUsedCondition$/);

0 commit comments

Comments
 (0)