Files
element-call/src/button/ReactionToggleButton.test.tsx
T

178 lines
5.5 KiB
TypeScript
Raw Normal View History

2024-11-08 17:36:40 +00:00
/*
Copyright 2024 New Vector Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
2024-11-08 17:36:40 +00:00
Please see LICENSE in the repository root for full details.
*/
import { act, render } from "@testing-library/react";
2025-10-22 22:57:29 -04:00
import { expect, test, vi } from "vitest";
2024-11-08 17:36:40 +00:00
import { TooltipProvider } from "@vector-im/compound-web";
import { userEvent } from "@testing-library/user-event";
import { type ReactNode } from "react";
2024-11-08 17:36:40 +00:00
import { ReactionToggleButton } from "./ReactionToggleButton";
import { ElementCallReactionEventType } from "../reactions";
import { type CallViewModel } from "../state/CallViewModel/CallViewModel";
import { getBasicCallViewModelEnvironment } from "../utils/test-viewmodel";
import { alice, local, localRtcMember } from "../utils/test-fixtures";
import { type MockRTCSession } from "../utils/test";
import { ReactionsSenderProvider } from "../reactions/useReactionsSender";
2026-01-22 19:38:39 +01:00
import { initializeWidget } from "../widget";
initializeWidget();
2025-10-22 22:57:29 -04:00
vi.mock("livekit-client/e2ee-worker?worker");
const localIdent = `${localRtcMember.userId}:${localRtcMember.deviceId}`;
2024-11-08 17:36:40 +00:00
function TestComponent({
rtcSession,
vm,
2024-11-08 17:36:40 +00:00
}: {
rtcSession: MockRTCSession;
vm: CallViewModel;
2024-11-08 17:36:40 +00:00
}): ReactNode {
return (
<TooltipProvider>
<ReactionsSenderProvider
vm={vm}
2025-10-10 11:09:41 +02:00
rtcSession={rtcSession.asMockedSession()}
>
<ReactionToggleButton vm={vm} identifier={localIdent} />
</ReactionsSenderProvider>
2024-11-08 17:36:40 +00:00
</TooltipProvider>
);
}
test("Can open menu", async () => {
const user = userEvent.setup();
const { vm, rtcSession } = getBasicCallViewModelEnvironment([alice]);
2024-11-08 17:36:40 +00:00
const { getByLabelText, container } = render(
<TestComponent vm={vm} rtcSession={rtcSession} />,
2024-11-08 17:36:40 +00:00
);
2025-03-03 14:37:29 +01:00
await user.click(getByLabelText("Reactions"));
2024-11-08 17:36:40 +00:00
expect(container).toMatchSnapshot();
});
test("Can raise hand", async () => {
const user = userEvent.setup();
const { vm, rtcSession, handRaisedSubject$ } =
getBasicCallViewModelEnvironment([local, alice]);
2024-11-08 17:36:40 +00:00
const { getByLabelText, container } = render(
<TestComponent vm={vm} rtcSession={rtcSession} />,
2024-11-08 17:36:40 +00:00
);
2025-03-03 14:37:29 +01:00
await user.click(getByLabelText("Reactions"));
await user.click(getByLabelText("Raise hand"));
expect(rtcSession.room.client.sendEvent).toHaveBeenCalledWith(
rtcSession.room.roomId,
"m.reaction",
{
"m.relates_to": {
event_id: localRtcMember.eventId,
key: "🖐️",
rel_type: "m.annotation",
},
},
);
act(() => {
// Mock receiving a reaction.
handRaisedSubject$.next({
[localIdent]: {
time: new Date(),
reactionEventId: "",
membershipEventId: localRtcMember.eventId!,
2024-11-08 17:36:40 +00:00
},
});
});
2024-11-08 17:36:40 +00:00
expect(container).toMatchSnapshot();
});
test("Can lower hand", async () => {
const reactionEventId = "$my-reaction-event:example.org";
2024-11-08 17:36:40 +00:00
const user = userEvent.setup();
const { vm, rtcSession, handRaisedSubject$ } =
getBasicCallViewModelEnvironment([local, alice]);
2024-11-08 17:36:40 +00:00
const { getByLabelText, container } = render(
<TestComponent vm={vm} rtcSession={rtcSession} />,
2024-11-08 17:36:40 +00:00
);
2025-03-03 14:37:29 +01:00
await user.click(getByLabelText("Reactions"));
await user.click(getByLabelText("Raise hand"));
act(() => {
handRaisedSubject$.next({
[localIdent]: {
time: new Date(),
reactionEventId,
membershipEventId: localRtcMember.eventId!,
},
});
});
2025-03-03 14:37:29 +01:00
await user.click(getByLabelText("Reactions"));
await user.click(getByLabelText("Lower hand"));
expect(rtcSession.room.client.redactEvent).toHaveBeenCalledWith(
rtcSession.room.roomId,
reactionEventId,
);
act(() => {
// Mock receiving a redacted reaction.
handRaisedSubject$.next({});
});
2024-11-08 17:36:40 +00:00
expect(container).toMatchSnapshot();
});
test("Can react with emoji", async () => {
const user = userEvent.setup();
const { vm, rtcSession } = getBasicCallViewModelEnvironment([local, alice]);
2024-11-08 17:36:40 +00:00
const { getByLabelText, getByText } = render(
<TestComponent vm={vm} rtcSession={rtcSession} />,
2024-11-08 17:36:40 +00:00
);
2025-03-03 14:37:29 +01:00
await user.click(getByLabelText("Reactions"));
2024-11-08 17:36:40 +00:00
await user.click(getByText("🐶"));
expect(rtcSession.room.client.sendEvent).toHaveBeenCalledWith(
rtcSession.room.roomId,
ElementCallReactionEventType,
{
"m.relates_to": {
event_id: localRtcMember.eventId,
rel_type: "m.reference",
2024-11-08 17:36:40 +00:00
},
name: "dog",
emoji: "🐶",
},
);
2024-11-08 17:36:40 +00:00
});
2024-11-15 16:02:06 +00:00
test("Can fully expand emoji picker", async () => {
2024-11-08 17:36:40 +00:00
const user = userEvent.setup();
const { vm, rtcSession } = getBasicCallViewModelEnvironment([local, alice]);
const { getByLabelText, container, getByText } = render(
<TestComponent vm={vm} rtcSession={rtcSession} />,
2024-11-08 17:36:40 +00:00
);
2025-03-03 14:37:29 +01:00
await user.click(getByLabelText("Reactions"));
await user.click(getByLabelText("Show more"));
2024-11-08 17:36:40 +00:00
expect(container).toMatchSnapshot();
await user.click(getByText("🦗"));
expect(rtcSession.room.client.sendEvent).toHaveBeenCalledWith(
rtcSession.room.roomId,
ElementCallReactionEventType,
{
"m.relates_to": {
event_id: localRtcMember.eventId,
rel_type: "m.reference",
2024-11-08 17:36:40 +00:00
},
name: "crickets",
emoji: "🦗",
},
);
2024-11-08 17:36:40 +00:00
});
test("Can close reaction dialog", async () => {
2024-11-08 17:36:40 +00:00
const user = userEvent.setup();
const { vm, rtcSession } = getBasicCallViewModelEnvironment([local, alice]);
2024-11-08 17:36:40 +00:00
const { getByLabelText, container } = render(
<TestComponent vm={vm} rtcSession={rtcSession} />,
2024-11-08 17:36:40 +00:00
);
2025-03-03 14:37:29 +01:00
await user.click(getByLabelText("Reactions"));
await user.click(getByLabelText("Show more"));
await user.click(getByLabelText("Show less"));
2024-11-08 17:36:40 +00:00
expect(container).toMatchSnapshot();
});