Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/components/screens/chat/ChatPublicDiscovery.test.tsx
T

138 lines
5.3 KiB
TypeScript

// @vitest-environment jsdom
import "@testing-library/jest-dom/vitest";
import type { ComponentProps, ReactNode } from "react";
import { fireEvent, render, screen } from "@testing-library/react";
import { buildChatChannelListVM } from "@hivegrid/view-models";
import { describe, expect, it, vi } from "vitest";
import { ChatChannelListPage, type ChatChannelListActions } from "./ChatChannelListPage";
vi.mock("react-i18next", () => ({
useTranslation: () => ({
t: (key: string, values?: Record<string, unknown>) => (values ? `${key}:${Object.values(values).join("/")}` : key),
i18n: { language: "de" },
}),
}));
vi.mock("~/locale/format", () => ({
useLocaleFormatters: () => ({
number: (value: number) => String(value),
relativeDate: (value: string) => value,
}),
}));
vi.mock("@subframe/core", () => ({
FeatherAtSign: () => <span />,
FeatherHash: () => <span />,
FeatherLock: () => <span />,
FeatherMessageCircle: () => <span />,
FeatherMessageSquare: () => <span />,
FeatherPin: () => <span />,
FeatherUsers: () => <span />,
}));
vi.mock("~/ui/components/Avatar", () => ({ Avatar: ({ children }: { children?: ReactNode }) => <span>{children}</span> }));
vi.mock("~/ui/components/BadgeFeedback", () => ({ BadgeFeedback: ({ children }: { children?: ReactNode }) => <span>{children}</span> }));
vi.mock("~/ui/components/Button", () => ({
Button: ({ children, loading: _loading, icon: _icon, ...props }: ComponentProps<"button"> & { loading?: boolean; icon?: ReactNode }) => (
<button {...props}>{children}</button>
),
}));
vi.mock("~/ui/components/ButtonGroup", () => ({ ButtonGroup: ({ children }: { children?: ReactNode }) => <div>{children}</div> }));
vi.mock("~/ui/components/Card", () => ({ Card: ({ children, ...props }: ComponentProps<"div">) => <div {...props}>{children}</div> }));
vi.mock("~/ui/components/EmptyState", () => ({
EmptyState: ({ title, body, action }: { title?: ReactNode; body?: ReactNode; action?: ReactNode }) => (
<div>
{title}
{body}
{action}
</div>
),
}));
vi.mock("~/ui/components/LoadingIndicatorSkeleton", () => ({ LoadingIndicatorSkeleton: (props: ComponentProps<"div">) => <div {...props} /> }));
vi.mock("~/ui/components/SearchInput", () => ({
SearchInput: ({ fillContainer: _fill, hideShortcut: _shortcut, ...props }: ComponentProps<"input"> & { fillContainer?: boolean; hideShortcut?: boolean }) => (
<input type="search" {...props} />
),
}));
vi.mock("~/ui/components/Select", () => ({
Select: Object.assign(
({ children }: { children?: ReactNode }) => <div>{children}</div>,
{ Item: ({ children }: { children?: ReactNode }) => <span>{children}</span> }
),
}));
vi.mock("~/ui/components/Switch", () => ({ Switch: (props: ComponentProps<"input">) => <input type="checkbox" {...props} /> }));
vi.mock("../shared/PageShell", () => ({
PAGE_HEADER_ICON_CLASS: "",
PageShell: ({ children }: { children?: ReactNode }) => <main>{children}</main>,
PageHeader: ({ title, actions }: { title?: ReactNode; actions?: ReactNode }) => (
<header>
{title}
{actions}
</header>
),
PageBody: ({ children }: { children?: ReactNode }) => <section>{children}</section>,
}));
const actions = (onJoinChannel: ChatChannelListActions["onJoinChannel"]): ChatChannelListActions => ({
onOpenChannel: vi.fn(),
onQueryChange: vi.fn(),
onViewChange: vi.fn(),
onShowArchivedChange: vi.fn(),
onRetry: vi.fn(),
onJoinChannel,
});
const vm = buildChatChannelListVM({
rows: [],
discoverRows: [
{
id: "chat_public",
title: "Montage Nord",
description: "Offener Austausch",
avatar: { imageUrl: null, fallbackLabel: "MN" },
memberCount: 7,
isJoined: false,
canJoin: true,
joinHref: "/demo/chat",
detailHref: null,
},
],
filter: { query: "", view: "all", showArchived: false },
permissions: {
canWrite: true,
canCreateGroup: false,
canCreateDm: false,
canDiscover: true,
canViewMentions: false,
canSearch: true,
canViewArchived: false,
canAdmin: false,
},
hrefs: {
newChannel: "/demo/chat/new?kind=group",
newDm: "/demo/chat/new?kind=dm",
search: "/demo/chat/search",
mentions: "/demo/chat/mentions",
archived: "/demo/chat?archived=true",
},
totals: { unread: 0, mentions: 0 },
});
describe("public channel discovery interim UI", () => {
it("shows public groups as join-only rows and delegates the join intent", () => {
const onJoinChannel = vi.fn();
const { rerender } = render(<ChatChannelListPage vm={vm} actions={actions(onJoinChannel)} />);
expect(screen.getByText("chat.channelList.discover.headline")).toBeInTheDocument();
expect(screen.getByText("Offener Austausch")).toBeInTheDocument();
expect(screen.getByText("chat.channelList.discover.memberCount:7")).toBeInTheDocument();
expect(screen.queryByRole("link", { name: /Montage Nord/ })).not.toBeInTheDocument();
const joinButton = screen.getByRole("button", { name: "chat.channelList.discover.joinAria:Montage Nord" });
fireEvent.click(joinButton);
expect(onJoinChannel).toHaveBeenCalledWith("chat_public", "/demo/chat");
rerender(<ChatChannelListPage vm={vm} joiningChannelId="chat_public" actions={actions(onJoinChannel)} />);
expect(screen.getByRole("button", { name: "chat.channelList.discover.joinAria:Montage Nord" })).toBeDisabled();
});
});