|
| 1 | +import type { ChannelItemModel } from "@posthog/core/canvas/channelItems"; |
| 2 | +import { Theme } from "@radix-ui/themes"; |
| 3 | +import { render, screen } from "@testing-library/react"; |
| 4 | +import userEvent from "@testing-library/user-event"; |
| 5 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 6 | + |
| 7 | +const mocks = vi.hoisted(() => ({ |
| 8 | + items: [] as ChannelItemModel[], |
| 9 | + isLoading: false, |
| 10 | + channelMissing: false, |
| 11 | +})); |
| 12 | + |
| 13 | +vi.mock("@posthog/ui/features/canvas/hooks/useChannelItems", () => ({ |
| 14 | + useChannelItems: () => ({ |
| 15 | + items: mocks.items, |
| 16 | + actions: { open: vi.fn(), togglePin: vi.fn(), archive: vi.fn() }, |
| 17 | + me: { uuid: "me-uuid" }, |
| 18 | + isLoading: mocks.isLoading, |
| 19 | + channelMissing: mocks.channelMissing, |
| 20 | + }), |
| 21 | +})); |
| 22 | +vi.mock("@posthog/ui/features/feature-flags/useFeatureFlag", () => ({ |
| 23 | + useFeatureFlag: () => false, |
| 24 | +})); |
| 25 | +vi.mock("@tanstack/react-router", () => ({ |
| 26 | + useNavigate: () => vi.fn(), |
| 27 | + useRouterState: () => "/website/channel-1", |
| 28 | +})); |
| 29 | + |
| 30 | +// Both mount their own query stacks; this suite is about the list's own |
| 31 | +// loading-vs-empty decisions. |
| 32 | +vi.mock("@posthog/ui/features/canvas/components/ChannelBackRow", () => ({ |
| 33 | + ChannelBackRow: () => null, |
| 34 | +})); |
| 35 | +vi.mock("@posthog/ui/features/canvas/components/ChannelsFab", () => ({ |
| 36 | + ChannelsFab: () => null, |
| 37 | +})); |
| 38 | + |
| 39 | +// The row context menu's hooks reach for a QueryClient and the DI container, |
| 40 | +// neither of which a unit test has. Stubbed at the module boundary, as |
| 41 | +// WebsiteLayout.test.tsx does for the same reason. |
| 42 | +vi.mock("@posthog/ui/features/tasks/useTaskContextMenu", () => ({ |
| 43 | + useTaskContextMenu: () => ({ |
| 44 | + showContextMenu: vi.fn(), |
| 45 | + editingTaskId: null, |
| 46 | + setEditingTaskId: vi.fn(), |
| 47 | + }), |
| 48 | +})); |
| 49 | +vi.mock("@posthog/ui/features/tasks/useTaskMutations", () => ({ |
| 50 | + useRenameTask: () => ({ renameTask: vi.fn() }), |
| 51 | +})); |
| 52 | +vi.mock("@posthog/ui/features/tasks/useTasks", () => ({ |
| 53 | + useTasks: () => ({ data: [] }), |
| 54 | +})); |
| 55 | + |
| 56 | +import { ChannelSidebar } from "./ChannelSidebar"; |
| 57 | + |
| 58 | +function item(overrides: Partial<ChannelItemModel> = {}): ChannelItemModel { |
| 59 | + return { |
| 60 | + key: "task:task-1", |
| 61 | + kind: "task", |
| 62 | + id: "task-1", |
| 63 | + title: "Investigate signup drop-off", |
| 64 | + ts: Date.parse("2026-07-17T12:00:00.000Z"), |
| 65 | + pinned: false, |
| 66 | + rawStatus: null, |
| 67 | + authorUser: null, |
| 68 | + authorName: "Someone else", |
| 69 | + // Not the viewer, so filtering to "Me" leaves nothing. |
| 70 | + authorUuid: "someone-else-uuid", |
| 71 | + templateId: null, |
| 72 | + ...overrides, |
| 73 | + }; |
| 74 | +} |
| 75 | + |
| 76 | +// A fresh element per render: React bails out of re-rendering an element it has |
| 77 | +// already seen by reference, and these tests change the hook's answer between |
| 78 | +// renders rather than the props. |
| 79 | +const sidebar = () => ( |
| 80 | + <Theme> |
| 81 | + <ChannelSidebar channelId="channel-1" /> |
| 82 | + </Theme> |
| 83 | +); |
| 84 | + |
| 85 | +function renderSidebar() { |
| 86 | + return render(sidebar()); |
| 87 | +} |
| 88 | + |
| 89 | +describe("ChannelSidebar", () => { |
| 90 | + beforeEach(() => { |
| 91 | + mocks.items = []; |
| 92 | + mocks.isLoading = false; |
| 93 | + mocks.channelMissing = false; |
| 94 | + }); |
| 95 | + |
| 96 | + it.each([ |
| 97 | + { |
| 98 | + what: "nothing has arrived yet", |
| 99 | + state: { items: [], isLoading: true }, |
| 100 | + shown: [] as string[], |
| 101 | + hidden: ["Recent", "No matches", "Nothing here yet"], |
| 102 | + }, |
| 103 | + { |
| 104 | + what: "the space is settled and genuinely empty", |
| 105 | + state: { items: [], isLoading: false }, |
| 106 | + shown: ["Nothing here yet"], |
| 107 | + hidden: ["Recent", "No matches"], |
| 108 | + }, |
| 109 | + { |
| 110 | + what: "the space is settled with items", |
| 111 | + state: { items: [item()], isLoading: false }, |
| 112 | + shown: ["Recent", "Investigate signup drop-off"], |
| 113 | + hidden: ["No matches", "Nothing here yet"], |
| 114 | + }, |
| 115 | + ])("shows one state when $what", ({ state, shown, hidden }) => { |
| 116 | + mocks.items = state.items; |
| 117 | + mocks.isLoading = state.isLoading; |
| 118 | + |
| 119 | + const { container } = renderSidebar(); |
| 120 | + |
| 121 | + for (const text of shown) { |
| 122 | + expect(screen.getByText(text)).toBeInTheDocument(); |
| 123 | + } |
| 124 | + for (const text of hidden) { |
| 125 | + expect(screen.queryByText(text)).not.toBeInTheDocument(); |
| 126 | + } |
| 127 | + expect( |
| 128 | + container.querySelector("[aria-busy]")?.getAttribute("aria-busy"), |
| 129 | + ).toBe(String(state.isLoading)); |
| 130 | + }); |
| 131 | + |
| 132 | + it("doesn't call a cold load 'no matches' while a filter is active", async () => { |
| 133 | + const user = userEvent.setup(); |
| 134 | + mocks.items = [item()]; |
| 135 | + const { rerender } = renderSidebar(); |
| 136 | + |
| 137 | + // Filter down to the viewer's own items; this one is someone else's, so the |
| 138 | + // settled list really has no matches. |
| 139 | + await user.click(screen.getByRole("button", { name: "Filter" })); |
| 140 | + await user.click(await screen.findByRole("menuitemradio", { name: "Me" })); |
| 141 | + expect(screen.getByText("No matches")).toBeInTheDocument(); |
| 142 | + |
| 143 | + // Reloading the space empties the list again — that isn't a verdict. |
| 144 | + mocks.items = []; |
| 145 | + mocks.isLoading = true; |
| 146 | + rerender(sidebar()); |
| 147 | + |
| 148 | + expect(screen.queryByText("No matches")).not.toBeInTheDocument(); |
| 149 | + expect(screen.queryByText("Nothing here yet")).not.toBeInTheDocument(); |
| 150 | + }); |
| 151 | + |
| 152 | + it("shows a single empty state when the last item goes away under a search", async () => { |
| 153 | + const user = userEvent.setup(); |
| 154 | + mocks.items = [item()]; |
| 155 | + const { rerender } = renderSidebar(); |
| 156 | + |
| 157 | + await user.click(screen.getByRole("button", { name: "Search" })); |
| 158 | + mocks.items = []; |
| 159 | + rerender(sidebar()); |
| 160 | + |
| 161 | + expect(screen.getByText("No matches")).toBeInTheDocument(); |
| 162 | + expect(screen.queryByText("Nothing here yet")).not.toBeInTheDocument(); |
| 163 | + }); |
| 164 | +}); |
0 commit comments