|
1 | 1 | import type { Workspace } from "@coder-studio/core"; |
2 | | -import { fireEvent, render, screen } from "@testing-library/react"; |
| 2 | +import { fireEvent, render, screen, waitFor } from "@testing-library/react"; |
3 | 3 | import { createStore, Provider } from "jotai"; |
4 | 4 | import { beforeEach, describe, expect, it, vi } from "vitest"; |
5 | 5 | import { localeAtom } from "../../atoms/app-ui"; |
| 6 | +import { wsClientAtom } from "../../atoms/connection"; |
6 | 7 | import { |
| 8 | + activeWorkspaceIdAtom, |
7 | 9 | workspaceOrderAtom, |
8 | 10 | workspacesAtom, |
9 | 11 | workspacesLoadStateAtom, |
@@ -39,14 +41,6 @@ vi.mock("../workspace/views/shared/workspace-launch-modal", () => ({ |
39 | 41 | WorkspaceLaunchModal: () => null, |
40 | 42 | })); |
41 | 43 |
|
42 | | -vi.mock("./components/tab", () => ({ |
43 | | - WorkspaceTab: ({ workspace, isActive }: { workspace: Workspace; isActive: boolean }) => ( |
44 | | - <div data-testid="workspace-tab" data-active={String(isActive)}> |
45 | | - {workspace.id} |
46 | | - </div> |
47 | | - ), |
48 | | -})); |
49 | | - |
50 | 44 | function createWorkspace(id: string, path: string): Workspace { |
51 | 45 | return { |
52 | 46 | id, |
@@ -84,14 +78,50 @@ describe("TopBar", () => { |
84 | 78 | </Provider> |
85 | 79 | ); |
86 | 80 |
|
87 | | - const tabs = screen.getAllByTestId("workspace-tab"); |
| 81 | + const tabs = screen.getAllByRole("tab"); |
88 | 82 |
|
89 | | - expect(tabs.map((tab) => tab.textContent)).toEqual(["ws-b", "ws-a"]); |
90 | | - expect(tabs[0]?.getAttribute("data-active")).toBe("true"); |
91 | | - expect(tabs[1]?.getAttribute("data-active")).toBe("false"); |
| 83 | + expect(tabs.map((tab) => tab.textContent)).toEqual(["b", "a"]); |
| 84 | + expect(tabs[0]).toHaveAttribute("aria-selected", "true"); |
| 85 | + expect(tabs[1]).toHaveAttribute("aria-selected", "false"); |
92 | 86 | expect(screen.getByRole("tablist", { name: "Workspace tabs" })).toBeInTheDocument(); |
93 | 87 | }); |
94 | 88 |
|
| 89 | + it("persists the global last-viewed target when keyboard navigation changes workspace tabs", async () => { |
| 90 | + const store = createStore(); |
| 91 | + const sendCommand = vi.fn().mockResolvedValue({ |
| 92 | + workspaceId: "ws-b", |
| 93 | + updatedAt: 10, |
| 94 | + }); |
| 95 | + |
| 96 | + store.set(localeAtom, "en"); |
| 97 | + store.set(wsClientAtom, { sendCommand } as never); |
| 98 | + store.set(workspacesAtom, { |
| 99 | + "ws-a": createWorkspace("ws-a", "/tmp/a"), |
| 100 | + "ws-b": createWorkspace("ws-b", "/tmp/b"), |
| 101 | + }); |
| 102 | + store.set(workspaceOrderAtom, ["ws-a", "ws-b"]); |
| 103 | + store.set(workspacesLoadStateAtom, "ready"); |
| 104 | + store.set(activeWorkspaceIdAtom, "ws-a"); |
| 105 | + |
| 106 | + render( |
| 107 | + <Provider store={store}> |
| 108 | + <TopBar /> |
| 109 | + </Provider> |
| 110 | + ); |
| 111 | + |
| 112 | + const activeTab = screen.getByRole("tab", { name: "a" }); |
| 113 | + activeTab.focus(); |
| 114 | + fireEvent.keyDown(activeTab, { key: "ArrowRight" }); |
| 115 | + |
| 116 | + await waitFor(() => { |
| 117 | + expect(sendCommand).toHaveBeenCalledWith( |
| 118 | + "workspace.lastViewedTarget.set", |
| 119 | + { workspaceId: "ws-b", sessionId: undefined }, |
| 120 | + undefined |
| 121 | + ); |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
95 | 125 | it("uses translated labels when locale is set to en", () => { |
96 | 126 | const store = createStore(); |
97 | 127 | store.set(localeAtom, "en"); |
|
0 commit comments