|
| 1 | +import { DEFAULT_CLIENT_SETTINGS } from "@t3tools/contracts/settings"; |
| 2 | +import { describe, expect, it, vi } from "vite-plus/test"; |
| 3 | + |
| 4 | +import { act, render } from "../../test/render"; |
| 5 | +import { |
| 6 | + __resetClientSettingsPersistenceForTests, |
| 7 | + __setClientSettingsForTests, |
| 8 | + updateClientSettings, |
| 9 | +} from "../hooks/useSettings"; |
| 10 | +import { AppSidebarLayout } from "./AppSidebarLayout"; |
| 11 | + |
| 12 | +vi.mock("../env", () => ({ isElectron: true })); |
| 13 | +vi.mock("./Sidebar", () => ({ default: () => null })); |
| 14 | +vi.mock("./BackgroundThreadAnnouncer", () => ({ BackgroundThreadAnnouncer: () => null })); |
| 15 | +vi.mock("./sidebar/SidebarChrome", () => ({ SidebarBrandWidthProbe: () => null })); |
| 16 | +vi.mock("../localApi", () => ({ |
| 17 | + ensureLocalApi: () => ({ |
| 18 | + persistence: { setClientSettings: async () => undefined }, |
| 19 | + }), |
| 20 | +})); |
| 21 | + |
| 22 | +function setup(historySwipeNavigation: boolean) { |
| 23 | + __resetClientSettingsPersistenceForTests(); |
| 24 | + __setClientSettingsForTests({ ...DEFAULT_CLIENT_SETTINGS, historySwipeNavigation }); |
| 25 | +} |
| 26 | + |
| 27 | +function wheel(deltaX: number) { |
| 28 | + window.dispatchEvent(new WheelEvent("wheel", { deltaX, clientY: 200 })); |
| 29 | +} |
| 30 | + |
| 31 | +describe("desktop history swipe navigation", () => { |
| 32 | + it.each(["back", "forward"] as const)( |
| 33 | + "disables %s swipes and can re-enable them without reloading", |
| 34 | + async (direction) => { |
| 35 | + setup(false); |
| 36 | + using _available = vi |
| 37 | + .spyOn(window.navigation, direction === "back" ? "canGoBack" : "canGoForward", "get") |
| 38 | + .mockReturnValue(true); |
| 39 | + await using screen = await render(<AppSidebarLayout>Thread</AppSidebarLayout>); |
| 40 | + using navigate = vi.spyOn(window.history, direction).mockImplementation(() => undefined); |
| 41 | + const deltaX = direction === "back" ? -140 : 140; |
| 42 | + |
| 43 | + await act(() => { |
| 44 | + wheel(deltaX); |
| 45 | + wheel(0); |
| 46 | + }); |
| 47 | + expect(navigate).not.toHaveBeenCalled(); |
| 48 | + await expect.element(screen.getByTestId("history-swipe-indicator")).not.toBeInTheDocument(); |
| 49 | + |
| 50 | + await act(() => updateClientSettings({ historySwipeNavigation: true })); |
| 51 | + await act(() => wheel(deltaX)); |
| 52 | + await expect.element(screen.getByTestId("history-swipe-indicator")).toBeInTheDocument(); |
| 53 | + |
| 54 | + await act(() => updateClientSettings({ historySwipeNavigation: false })); |
| 55 | + await expect.element(screen.getByTestId("history-swipe-indicator")).not.toBeInTheDocument(); |
| 56 | + await act(() => wheel(0)); |
| 57 | + expect(navigate).not.toHaveBeenCalled(); |
| 58 | + |
| 59 | + await act(() => updateClientSettings({ historySwipeNavigation: true })); |
| 60 | + await act(() => { |
| 61 | + wheel(deltaX); |
| 62 | + wheel(0); |
| 63 | + }); |
| 64 | + expect(navigate).toHaveBeenCalledOnce(); |
| 65 | + }, |
| 66 | + ); |
| 67 | +}); |
0 commit comments