|
1 | | -import { describe, expect, it } from "vitest"; |
| 1 | +/* @vitest-environment jsdom */ |
| 2 | +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; |
| 3 | +import { renderHook, waitFor } from "@testing-library/react"; |
| 4 | +import { createElement, type ReactNode } from "react"; |
| 5 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
2 | 6 |
|
3 | | -import { timeEntriesQueryKey } from "./web-shell-time-entries.ts"; |
| 7 | +const mockGetTimeEntries = vi.fn(); |
| 8 | +const mockUpdateWebSession = vi.fn(); |
| 9 | + |
| 10 | +vi.mock("../api/public/track/index.ts", () => ({ |
| 11 | + deleteWorkspaceTimeEntries: vi.fn(), |
| 12 | + getCurrentTimeEntry: vi.fn(), |
| 13 | + getTimeEntries: (...args: unknown[]) => mockGetTimeEntries(...args), |
| 14 | + patchWorkspaceStopTimeEntryHandler: vi.fn(), |
| 15 | + postWorkspaceTimeEntries: vi.fn(), |
| 16 | + putWorkspaceTimeEntryHandler: vi.fn(), |
| 17 | +})); |
| 18 | + |
| 19 | +vi.mock("../api/web/index.ts", () => ({ |
| 20 | + listRecentWorkspaceTimeEntrySuggestions: vi.fn(), |
| 21 | + updateWebSession: (...args: unknown[]) => mockUpdateWebSession(...args), |
| 22 | +})); |
| 23 | + |
| 24 | +vi.mock("../api/web-client.ts", () => ({ |
| 25 | + unwrapWebApiResult: (p: Promise<unknown>) => |
| 26 | + p.then((result) => |
| 27 | + typeof result === "object" && result !== null && "data" in result |
| 28 | + ? (result as { data: unknown }).data |
| 29 | + : result, |
| 30 | + ), |
| 31 | +})); |
| 32 | + |
| 33 | +const { sessionQueryKey, timeEntriesQueryKey, useTimeEntriesQuery } = |
| 34 | + await import("./web-shell.ts"); |
| 35 | + |
| 36 | +function createTestQueryClient(): QueryClient { |
| 37 | + return new QueryClient({ |
| 38 | + defaultOptions: { |
| 39 | + queries: { retry: false }, |
| 40 | + }, |
| 41 | + }); |
| 42 | +} |
| 43 | + |
| 44 | +function createWrapper(queryClient: QueryClient) { |
| 45 | + return function Wrapper({ children }: { children: ReactNode }) { |
| 46 | + return createElement(QueryClientProvider, { client: queryClient }, children); |
| 47 | + }; |
| 48 | +} |
4 | 49 |
|
5 | 50 | describe("timeEntriesQueryKey", () => { |
| 51 | + beforeEach(() => { |
| 52 | + vi.clearAllMocks(); |
| 53 | + }); |
| 54 | + |
6 | 55 | it("scopes time entry lists by workspace", () => { |
7 | 56 | expect(timeEntriesQueryKey(1, "2026-05-01", "2026-05-07", false)).not.toEqual( |
8 | 57 | timeEntriesQueryKey(2, "2026-05-01", "2026-05-07", false), |
9 | 58 | ); |
10 | 59 | }); |
| 60 | + |
| 61 | + it("aligns the server session workspace before reading /me time entries", async () => { |
| 62 | + const queryClient = createTestQueryClient(); |
| 63 | + queryClient.setQueryData(sessionQueryKey, { |
| 64 | + current_workspace_id: 1, |
| 65 | + }); |
| 66 | + mockUpdateWebSession.mockResolvedValue({ |
| 67 | + data: { |
| 68 | + current_workspace_id: 2, |
| 69 | + }, |
| 70 | + }); |
| 71 | + mockGetTimeEntries.mockResolvedValue({ data: [] }); |
| 72 | + |
| 73 | + renderHook( |
| 74 | + () => |
| 75 | + useTimeEntriesQuery({ |
| 76 | + endDate: "2026-05-07", |
| 77 | + startDate: "2026-05-01", |
| 78 | + workspaceId: 2, |
| 79 | + }), |
| 80 | + { |
| 81 | + wrapper: createWrapper(queryClient), |
| 82 | + }, |
| 83 | + ); |
| 84 | + |
| 85 | + await waitFor(() => expect(mockGetTimeEntries).toHaveBeenCalledTimes(1)); |
| 86 | + expect(mockUpdateWebSession).toHaveBeenCalledWith({ |
| 87 | + body: { |
| 88 | + workspace_id: 2, |
| 89 | + }, |
| 90 | + }); |
| 91 | + expect(mockGetTimeEntries.mock.invocationCallOrder[0]).toBeGreaterThan( |
| 92 | + mockUpdateWebSession.mock.invocationCallOrder[0], |
| 93 | + ); |
| 94 | + expect(queryClient.getQueryData(sessionQueryKey)).toEqual({ |
| 95 | + current_workspace_id: 2, |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + it("does not update the server session when the workspace is already current", async () => { |
| 100 | + const queryClient = createTestQueryClient(); |
| 101 | + queryClient.setQueryData(sessionQueryKey, { |
| 102 | + current_workspace_id: 2, |
| 103 | + }); |
| 104 | + mockGetTimeEntries.mockResolvedValue({ data: [] }); |
| 105 | + |
| 106 | + renderHook( |
| 107 | + () => |
| 108 | + useTimeEntriesQuery({ |
| 109 | + endDate: "2026-05-07", |
| 110 | + startDate: "2026-05-01", |
| 111 | + workspaceId: 2, |
| 112 | + }), |
| 113 | + { |
| 114 | + wrapper: createWrapper(queryClient), |
| 115 | + }, |
| 116 | + ); |
| 117 | + |
| 118 | + await waitFor(() => expect(mockGetTimeEntries).toHaveBeenCalledTimes(1)); |
| 119 | + expect(mockUpdateWebSession).not.toHaveBeenCalled(); |
| 120 | + }); |
11 | 121 | }); |
0 commit comments