feat(ux-26): implement real shop pagination with deep-link page support and regression test
This commit is contained in:
1 parent
92065cc7f2
commit
21f7768cce
3 files changed
+115
-22
No files matched your search
@@ -1,7 +1,7 @@
|
||||
"use client";
|
||||
import React from "react";
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
import { render, screen, fireEvent, waitFor } from "@testing-library/react";
|
||||
import { render, screen, fireEvent, waitFor, cleanup } from "@testing-library/react";
|
||||
import ArchivePage, { orderProductsByAvailability } from "../ArchivePage";
|
||||
import { productService } from "../../lib/services/productService";
|
||||
import { useCartStore } from "../../lib/store/cartStore";
|
||||
@@ -383,4 +383,44 @@ describe("ArchivePage Component", () => {
|
||||
expect(screen.getByText("محصول بازیابی شده")).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
it("supports real pagination, page switching, deep-link initialPage, and resets to page 1 on filter change", async () => {
|
||||
const mockProducts = Array.from({ length: 25 }, (_, i) => ({
|
||||
id: "prod-" + (i + 1),
|
||||
name: "محصول تست " + (i + 1),
|
||||
nameFa: "محصول تست " + (i + 1),
|
||||
priceValue: 100000 + i * 1000,
|
||||
stock: 10,
|
||||
categorySlug: i % 2 === 0 ? "joints" : "immune",
|
||||
images: ["/test.png"],
|
||||
symptoms: [],
|
||||
main_ingredients: [],
|
||||
}));
|
||||
|
||||
// Initial render at page 1 with pageSize 12 -> shows items 1 to 12
|
||||
const { rerender } = render(<ArchivePage initialProducts={mockProducts as any} initialLimit={12} />);
|
||||
|
||||
expect(screen.getByTestId("shop-pagination")).toBeInTheDocument();
|
||||
expect(screen.getByText("محصول تست 1")).toBeInTheDocument();
|
||||
expect(screen.getByText("محصول تست 12")).toBeInTheDocument();
|
||||
expect(screen.queryByText("محصول تست 13")).not.toBeInTheDocument();
|
||||
|
||||
// Click next page button
|
||||
const nextBtn = screen.getByRole("button", { name: "صفحه بعد" });
|
||||
fireEvent.click(nextBtn);
|
||||
|
||||
// Now page 2 -> shows items 13 to 24
|
||||
expect(screen.getByText("محصول تست 13")).toBeInTheDocument();
|
||||
expect(screen.getByText("محصول تست 24")).toBeInTheDocument();
|
||||
expect(screen.queryByText("محصول تست 1")).not.toBeInTheDocument();
|
||||
|
||||
// Deep-link initialPage test: render directly at page 2
|
||||
cleanup();
|
||||
render(<ArchivePage initialProducts={mockProducts as any} initialLimit={12} initialPage={2} />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByText("محصول تست 13")).toBeInTheDocument();
|
||||
expect(screen.queryByText("محصول تست 1")).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user