diff --git a/frontend/src/pages/MarketplacePage.test.tsx b/frontend/src/pages/MarketplacePage.test.tsx index f7586e4..6be69b0 100644 --- a/frontend/src/pages/MarketplacePage.test.tsx +++ b/frontend/src/pages/MarketplacePage.test.tsx @@ -1,6 +1,6 @@ -import { render, screen, within } from "@testing-library/react"; +import { act, fireEvent, render, screen, within } from "@testing-library/react"; import userEvent from "@testing-library/user-event"; -import { MemoryRouter } from "react-router-dom"; +import { MemoryRouter, Route, Routes, useLocation, useNavigate } from "react-router-dom"; import { afterEach, describe, expect, it, vi } from "vitest"; // Block the real Wails runtime: importing it registers a module-level timer in @@ -68,6 +68,35 @@ afterEach(() => { sessionStorage.clear(); }); +function LocationProbe() { + const location = useLocation(); + return {`${location.pathname}${location.search}`}; +} + +// Stands in for the detail page, which goes back to the recorded returnTo. +function DetailBackProbe() { + const navigate = useNavigate(); + const { returnTo } = useLocation().state as { returnTo: string }; + return ; +} + +function renderMarketplaceRoutes() { + render( + + + + + } /> + } /> + + + , + ); +} + +const currentLocation = () => screen.getByTestId("location").textContent; +const searchBox = () => screen.getByRole("textbox", { name: "Search Marketplace" }); + describe("filterMarketplaceItems", () => { it("returns all items when category is 'all' and query is empty", () => { expect(filterMarketplaceItems(ITEMS, "all", "")).toHaveLength(3); @@ -208,6 +237,34 @@ describe("MarketplacePage category URL", () => { }); }); + it("keeps search text typed just before opening a card when going back", () => { + renderMarketplaceRoutes(); + + // No timer runs between the two events, so the 180ms debounce is pending. + fireEvent.change(searchBox(), { target: { value: "ultracode" } }); + expect(currentLocation()).toBe("/marketplace"); + fireEvent.click(screen.getByRole("link", { name: "Ultracode Skill" })); + + expect(currentLocation()).toBe("/marketplace/skill-a"); + expect(readMarketplaceQuerySession()?.returnTo).toBe("/marketplace?q=ultracode"); + + fireEvent.click(screen.getByRole("button", { name: "Back" })); + expect(currentLocation()).toBe("/marketplace?q=ultracode"); + expect(searchBox().value).toBe("ultracode"); + }); + + it("keeps both the pending search text and a filter changed in the same moment", async () => { + renderMarketplaceRoutes(); + fireEvent.click(screen.getByRole("button", { name: "Tool type" })); + + fireEvent.change(searchBox(), { target: { value: "ultracode" } }); + fireEvent.click(screen.getByLabelText("Skill")); + expect(currentLocation()).toBe("/marketplace?q=ultracode&kind=skill"); + + await act(async () => { await new Promise((resolve) => setTimeout(resolve, 250)); }); + expect(currentLocation()).toBe("/marketplace?q=ultracode&kind=skill"); + }); + it("does not expose filter values or top-level resource categories without tool supply", async () => { const user = userEvent.setup(); render( diff --git a/frontend/src/pages/MarketplacePage.tsx b/frontend/src/pages/MarketplacePage.tsx index 8f8fcc3..485428c 100644 --- a/frontend/src/pages/MarketplacePage.tsx +++ b/frontend/src/pages/MarketplacePage.tsx @@ -407,7 +407,13 @@ export function KindBadge({ item }: { item: MarketplaceItem }) { return {kinds.length > 1 ? +{kinds.length - 1} : null}; } -function VirtualMarketplaceGrid({ items, onCopied }: { items: MarketplaceItem[]; onCopied: (item: MarketplaceItem) => void }) { +type MarketplaceCardProps = { + onCopied: (item: MarketplaceItem) => void; + // Returns the list's query string after committing any debounced search text. + currentSearch: () => string; +}; + +function VirtualMarketplaceGrid({ items, onCopied, currentSearch }: { items: MarketplaceItem[] } & MarketplaceCardProps) { const [scrollTop, setScrollTop] = useState(0); const [viewportHeight, setViewportHeight] = useState(() => window.innerHeight); const [viewportWidth, setViewportWidth] = useState(() => window.innerWidth); @@ -436,7 +442,7 @@ function VirtualMarketplaceGrid({ items, onCopied }: { items: MarketplaceItem[]; // as if it had added results when the unfiltered virtual list was already // showing only its viewport. Small result sets remain fully accessible. if (items.length <= 24) { - return ; + return ; } const rowCount = Math.ceil(items.length / columns); const topRow = Math.max(0, Math.floor(scrollTop / rowHeight) - overscan); @@ -447,7 +453,7 @@ function VirtualMarketplaceGrid({ items, onCopied }: { items: MarketplaceItem[]; return <>