|
| 1 | +import { render, screen, waitFor } from "@testing-library/react"; |
| 2 | +import userEvent from "@testing-library/user-event"; |
| 3 | +import { createStore, Provider } from "jotai"; |
| 4 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 5 | +import { MobileWorkspaceDrawer } from "./mobile-workspace-drawer"; |
| 6 | + |
| 7 | +const navigateMock = vi.fn(); |
| 8 | +const closeWorkspaceMock = vi.fn(); |
| 9 | + |
| 10 | +vi.mock("react-router-dom", async () => { |
| 11 | + const actual = await vi.importActual<typeof import("react-router-dom")>("react-router-dom"); |
| 12 | + return { |
| 13 | + ...actual, |
| 14 | + useNavigate: () => navigateMock, |
| 15 | + }; |
| 16 | +}); |
| 17 | + |
| 18 | +vi.mock("../../actions/use-workspace-close-action", () => ({ |
| 19 | + useWorkspaceCloseAction: () => closeWorkspaceMock, |
| 20 | +})); |
| 21 | + |
| 22 | +vi.mock("../../../../lib/i18n", () => ({ |
| 23 | + useTranslation: () => (key: string, params?: Record<string, string | number>) => { |
| 24 | + switch (key) { |
| 25 | + case "mobile.workspace_drawer.close": |
| 26 | + return "Close workspace drawer"; |
| 27 | + case "mobile.workspace_drawer.aria_label": |
| 28 | + return "Workspace drawer"; |
| 29 | + case "label.workspace": |
| 30 | + return "Workspace"; |
| 31 | + case "mobile.workspace_drawer.select_title": |
| 32 | + return "Select Workspace"; |
| 33 | + case "mobile.workspace_drawer.switch_to_workspace": |
| 34 | + return `Switch to ${params?.name ?? ""}`; |
| 35 | + case "mobile.workspace_drawer.close_workspace": |
| 36 | + return `Close ${params?.name ?? ""}`; |
| 37 | + case "tooltip.new_workspace": |
| 38 | + return "New Workspace"; |
| 39 | + default: |
| 40 | + return key; |
| 41 | + } |
| 42 | + }, |
| 43 | +})); |
| 44 | + |
| 45 | +describe("MobileWorkspaceDrawer", () => { |
| 46 | + beforeEach(() => { |
| 47 | + vi.clearAllMocks(); |
| 48 | + closeWorkspaceMock.mockResolvedValue(true); |
| 49 | + }); |
| 50 | + |
| 51 | + it("uses shared IconButton compatibility classes for workspace close actions", async () => { |
| 52 | + const user = userEvent.setup(); |
| 53 | + const onClose = vi.fn(); |
| 54 | + const store = createStore(); |
| 55 | + |
| 56 | + render( |
| 57 | + <Provider store={store}> |
| 58 | + <MobileWorkspaceDrawer |
| 59 | + activeWorkspaceId="ws-1" |
| 60 | + isOpen |
| 61 | + onClose={onClose} |
| 62 | + onOpenWorkspaceLauncher={vi.fn()} |
| 63 | + workspaces={[ |
| 64 | + { |
| 65 | + id: "ws-1", |
| 66 | + path: "/tmp/demo", |
| 67 | + targetRuntime: "native", |
| 68 | + openedAt: 1, |
| 69 | + lastActiveAt: 1, |
| 70 | + uiState: { |
| 71 | + leftPanelWidth: 320, |
| 72 | + bottomPanelHeight: 240, |
| 73 | + focusMode: false, |
| 74 | + }, |
| 75 | + }, |
| 76 | + ]} |
| 77 | + /> |
| 78 | + </Provider> |
| 79 | + ); |
| 80 | + |
| 81 | + const closeButton = screen.getByRole("button", { name: "Close demo" }); |
| 82 | + |
| 83 | + expect(closeButton).toHaveClass( |
| 84 | + "btn", |
| 85 | + "btn-ghost", |
| 86 | + "btn-lg", |
| 87 | + "mobile-workspace-drawer__item-close" |
| 88 | + ); |
| 89 | + |
| 90 | + await user.click(closeButton); |
| 91 | + |
| 92 | + expect(closeWorkspaceMock).toHaveBeenCalledWith("ws-1", { navigateHomeWhenEmpty: true }); |
| 93 | + await waitFor(() => { |
| 94 | + expect(onClose).toHaveBeenCalledTimes(1); |
| 95 | + }); |
| 96 | + }); |
| 97 | +}); |
0 commit comments