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

176 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
Please see LICENSE in the repository root for full details.
*/
import { act, render } from "@testing-library/react";
2024-11-08 17:36:40 +00:00
import { expect, test } from "vitest";
import { TooltipProvider } from "@vector-im/compound-web";
import { userEvent } from "@testing-library/user-event";
import { type ReactNode } from "react";
import { type MatrixRTCSession } from "matrix-js-sdk/src/matrixrtc/MatrixRTCSession";
2024-11-08 17:36:40 +00:00
import { ReactionToggleButton } from "./ReactionToggleButton";
import { ElementCallReactionEventType } from "../reactions";
import { type CallViewModel } from "../state/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";
2024-11-08 17:36:40 +00:00
const localIdent = `${localRtcMember.sender}:${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}
rtcSession={rtcSession as unknown as MatrixRTCSession}
>
<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
);
2024-11-19 10:43:42 -05:00
await user.click(getByLabelText("common.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
);
2024-11-19 10:43:42 -05:00
await user.click(getByLabelText("common.reactions"));
2024-11-08 17:36:40 +00:00
await user.click(getByLabelText("action.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
);
await user.click(getByLabelText("common.reactions"));
await user.click(getByLabelText("action.raise_hand"));
act(() => {
handRaisedSubject$.next({
[localIdent]: {
time: new Date(),
reactionEventId,
membershipEventId: localRtcMember.eventId!,
},
});
});
2024-11-19 10:43:42 -05:00
await user.click(getByLabelText("common.reactions"));
2024-11-08 17:36:40 +00:00
await user.click(getByLabelText("action.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
);
2024-11-19 10:43:42 -05:00
await user.click(getByLabelText("common.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
);
2024-11-19 10:43:42 -05:00
await user.click(getByLabelText("common.reactions"));
2024-11-15 16:02:06 +00:00
await user.click(getByLabelText("action.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
);
2024-11-19 10:43:42 -05:00
await user.click(getByLabelText("common.reactions"));
2024-11-15 16:02:06 +00:00
await user.click(getByLabelText("action.show_more"));
await user.click(getByLabelText("action.show_less"));
2024-11-08 17:36:40 +00:00
expect(container).toMatchSnapshot();
});