|
| 1 | +import { Theme } from "@radix-ui/themes"; |
| 2 | +import { render, screen } from "@testing-library/react"; |
| 3 | +import { describe, expect, it, vi } from "vitest"; |
| 4 | + |
| 5 | +const { useChannelTasks, useParams, usePathname, useTasks } = vi.hoisted( |
| 6 | + () => ({ |
| 7 | + useChannelTasks: vi.fn(), |
| 8 | + useParams: vi.fn(), |
| 9 | + usePathname: vi.fn(), |
| 10 | + useTasks: vi.fn(), |
| 11 | + }), |
| 12 | +); |
| 13 | + |
| 14 | +vi.mock("@tanstack/react-router", () => ({ |
| 15 | + Outlet: () => null, |
| 16 | + useNavigate: () => vi.fn(), |
| 17 | + useParams, |
| 18 | + useRouterState: ({ |
| 19 | + select, |
| 20 | + }: { |
| 21 | + select: (s: { location: { pathname: string } }) => string; |
| 22 | + }) => select({ location: { pathname: usePathname() } }), |
| 23 | +})); |
| 24 | + |
| 25 | +vi.mock( |
| 26 | + "@posthog/ui/features/task-detail/components/TaskHeaderActions", |
| 27 | + () => ({ |
| 28 | + TaskHeaderActions: ({ task }: { task: { id: string } }) => ( |
| 29 | + <div data-testid="task-header-actions">{task.id}</div> |
| 30 | + ), |
| 31 | + }), |
| 32 | +); |
| 33 | + |
| 34 | +vi.mock("@posthog/ui/features/tasks/useTasks", () => ({ useTasks })); |
| 35 | +vi.mock("@posthog/ui/features/canvas/hooks/useChannelTasks", () => ({ |
| 36 | + useChannelTasks, |
| 37 | +})); |
| 38 | +vi.mock("@posthog/ui/features/canvas/hooks/useChannels", () => ({ |
| 39 | + useChannels: () => ({ |
| 40 | + channels: [{ id: "chan-1", name: "project-bluebird" }], |
| 41 | + }), |
| 42 | +})); |
| 43 | +vi.mock("@posthog/ui/features/canvas/hooks/useDashboards", () => ({ |
| 44 | + useDashboard: () => ({ dashboard: undefined }), |
| 45 | + useDashboardMutations: () => ({}), |
| 46 | +})); |
| 47 | +vi.mock("@posthog/ui/features/canvas/stores/dashboardEditStore", () => ({ |
| 48 | + useDashboardEditStore: (sel: (s: unknown) => unknown) => |
| 49 | + sel({ setEditing: vi.fn() }), |
| 50 | + useIsDashboardEditing: () => false, |
| 51 | +})); |
| 52 | +vi.mock("@posthog/ui/features/canvas/stores/freeformChatStore", () => ({ |
| 53 | + useFreeformChatStore: (sel: (s: unknown) => unknown) => |
| 54 | + sel({ revert: vi.fn(), goToLatest: vi.fn() }), |
| 55 | + useFreeformThread: () => ({ |
| 56 | + code: "", |
| 57 | + versions: [], |
| 58 | + currentVersionId: null, |
| 59 | + isSaving: false, |
| 60 | + }), |
| 61 | +})); |
| 62 | +vi.mock("@posthog/ui/features/canvas/components/NewCanvasMenu", () => ({ |
| 63 | + NewCanvasMenu: () => null, |
| 64 | +})); |
| 65 | +vi.mock("@posthog/ui/features/canvas/freeform/CanvasFrameHost", () => ({ |
| 66 | + CanvasFrameHost: () => null, |
| 67 | +})); |
| 68 | + |
| 69 | +import { useHeaderStore } from "@posthog/ui/shell/headerStore"; |
| 70 | +import { WebsiteLayout } from "./WebsiteLayout"; |
| 71 | + |
| 72 | +function renderLayout({ |
| 73 | + pathname, |
| 74 | + params, |
| 75 | + tasks = [{ id: "task-1", title: "Fix the bug" }], |
| 76 | + channelTaskIds = tasks.map((task) => task.id), |
| 77 | +}: { |
| 78 | + pathname: string; |
| 79 | + params: Record<string, string>; |
| 80 | + tasks?: { id: string; title: string }[]; |
| 81 | + channelTaskIds?: string[]; |
| 82 | +}) { |
| 83 | + usePathname.mockReturnValue(pathname); |
| 84 | + useParams.mockReturnValue(params); |
| 85 | + useTasks.mockReturnValue({ data: tasks }); |
| 86 | + useChannelTasks.mockReturnValue({ |
| 87 | + tasks: channelTaskIds.map((taskId) => ({ taskId })), |
| 88 | + isLoading: false, |
| 89 | + }); |
| 90 | + useHeaderStore.setState({ content: <span>crumb</span> }); |
| 91 | + render( |
| 92 | + <Theme> |
| 93 | + <WebsiteLayout /> |
| 94 | + </Theme>, |
| 95 | + ); |
| 96 | +} |
| 97 | + |
| 98 | +describe("WebsiteLayout task header actions", () => { |
| 99 | + it("renders the task action row on a channel task detail", () => { |
| 100 | + renderLayout({ |
| 101 | + pathname: "/website/chan-1/tasks/task-1", |
| 102 | + params: { channelId: "chan-1", taskId: "task-1" }, |
| 103 | + }); |
| 104 | + expect(screen.getByTestId("task-header-actions")).toHaveTextContent( |
| 105 | + "task-1", |
| 106 | + ); |
| 107 | + }); |
| 108 | + |
| 109 | + it("does not render actions for a task filed to another channel", () => { |
| 110 | + renderLayout({ |
| 111 | + pathname: "/website/chan-1/tasks/task-1", |
| 112 | + params: { channelId: "chan-1", taskId: "task-1" }, |
| 113 | + channelTaskIds: ["other-task"], |
| 114 | + }); |
| 115 | + expect(screen.queryByTestId("task-header-actions")).not.toBeInTheDocument(); |
| 116 | + }); |
| 117 | + |
| 118 | + it.each([ |
| 119 | + ["channel home", "/website/chan-1", { channelId: "chan-1" }], |
| 120 | + ["new task", "/website/chan-1/new", { channelId: "chan-1" }], |
| 121 | + ])("does not render the action row on %s", (_label, pathname, params) => { |
| 122 | + renderLayout({ pathname, params }); |
| 123 | + expect(screen.queryByTestId("task-header-actions")).not.toBeInTheDocument(); |
| 124 | + }); |
| 125 | +}); |
0 commit comments