|
| 1 | +/* |
| 2 | +Copyright 2024 New Vector Ltd. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: AGPL-3.0-only |
| 5 | +Please see LICENSE in the repository root for full details. |
| 6 | +*/ |
| 7 | + |
| 8 | +import { render } from "@testing-library/react"; |
| 9 | +import { expect, test } from "vitest"; |
| 10 | +import { TooltipProvider } from "@vector-im/compound-web"; |
| 11 | +import { act, ReactNode } from "react"; |
| 12 | +import { afterEach } from "node:test"; |
| 13 | + |
| 14 | +import { |
| 15 | + MockRoom, |
| 16 | + MockRTCSession, |
| 17 | + TestReactionsWrapper, |
| 18 | +} from "../utils/testReactions"; |
| 19 | +import { showReactions } from "../settings/settings"; |
| 20 | +import { ReactionsOverlay } from "./ReactionsOverlay"; |
| 21 | +import { ReactionSet } from "../reactions"; |
| 22 | + |
| 23 | +const memberUserIdAlice = "@alice:example.org"; |
| 24 | +const memberUserIdBob = "@bob:example.org"; |
| 25 | +const memberUserIdCharlie = "@charlie:example.org"; |
| 26 | +const memberEventAlice = "$membership-alice:example.org"; |
| 27 | +const memberEventBob = "$membership-bob:example.org"; |
| 28 | +const memberEventCharlie = "$membership-charlie:example.org"; |
| 29 | + |
| 30 | +const membership: Record<string, string> = { |
| 31 | + [memberEventAlice]: memberUserIdAlice, |
| 32 | + [memberEventBob]: memberUserIdBob, |
| 33 | + [memberEventCharlie]: memberUserIdCharlie, |
| 34 | +}; |
| 35 | + |
| 36 | +function TestComponent({ |
| 37 | + rtcSession, |
| 38 | +}: { |
| 39 | + rtcSession: MockRTCSession; |
| 40 | +}): ReactNode { |
| 41 | + return ( |
| 42 | + <TooltipProvider> |
| 43 | + <TestReactionsWrapper rtcSession={rtcSession}> |
| 44 | + <ReactionsOverlay /> |
| 45 | + </TestReactionsWrapper> |
| 46 | + </TooltipProvider> |
| 47 | + ); |
| 48 | +} |
| 49 | + |
| 50 | +afterEach(() => { |
| 51 | + showReactions.setValue(showReactions.defaultValue); |
| 52 | +}); |
| 53 | + |
| 54 | +test("defaults to showing no reactions", () => { |
| 55 | + showReactions.setValue(true); |
| 56 | + const rtcSession = new MockRTCSession( |
| 57 | + new MockRoom(memberUserIdAlice), |
| 58 | + membership, |
| 59 | + ); |
| 60 | + const { container } = render(<TestComponent rtcSession={rtcSession} />); |
| 61 | + expect(container.getElementsByTagName("span")).toHaveLength(0); |
| 62 | +}); |
| 63 | + |
| 64 | +test("shows a reaction when sent", () => { |
| 65 | + showReactions.setValue(true); |
| 66 | + const reaction = ReactionSet[0]; |
| 67 | + const room = new MockRoom(memberUserIdAlice); |
| 68 | + const rtcSession = new MockRTCSession(room, membership); |
| 69 | + const { getByRole } = render(<TestComponent rtcSession={rtcSession} />); |
| 70 | + act(() => { |
| 71 | + room.testSendReaction(memberEventAlice, reaction, membership); |
| 72 | + }); |
| 73 | + const span = getByRole("presentation"); |
| 74 | + expect(getByRole("presentation")).toBeTruthy(); |
| 75 | + expect(span.innerHTML).toEqual(reaction.emoji); |
| 76 | +}); |
| 77 | + |
| 78 | +test("shows two of the same reaction when sent", () => { |
| 79 | + showReactions.setValue(true); |
| 80 | + const reaction = ReactionSet[0]; |
| 81 | + const room = new MockRoom(memberUserIdAlice); |
| 82 | + const rtcSession = new MockRTCSession(room, membership); |
| 83 | + const { getAllByRole } = render(<TestComponent rtcSession={rtcSession} />); |
| 84 | + act(() => { |
| 85 | + room.testSendReaction(memberEventAlice, reaction, membership); |
| 86 | + }); |
| 87 | + act(() => { |
| 88 | + room.testSendReaction(memberEventBob, reaction, membership); |
| 89 | + }); |
| 90 | + expect(getAllByRole("presentation")).toHaveLength(2); |
| 91 | +}); |
| 92 | + |
| 93 | +test("shows two different reactions when sent", () => { |
| 94 | + showReactions.setValue(true); |
| 95 | + const room = new MockRoom(memberUserIdAlice); |
| 96 | + const rtcSession = new MockRTCSession(room, membership); |
| 97 | + const [reactionA, reactionB] = ReactionSet; |
| 98 | + const { getAllByRole } = render(<TestComponent rtcSession={rtcSession} />); |
| 99 | + act(() => { |
| 100 | + room.testSendReaction(memberEventAlice, reactionA, membership); |
| 101 | + }); |
| 102 | + act(() => { |
| 103 | + room.testSendReaction(memberEventBob, reactionB, membership); |
| 104 | + }); |
| 105 | + const [reactionElementA, reactionElementB] = getAllByRole("presentation"); |
| 106 | + expect(reactionElementA.innerHTML).toEqual(reactionA.emoji); |
| 107 | + expect(reactionElementB.innerHTML).toEqual(reactionB.emoji); |
| 108 | +}); |
| 109 | + |
| 110 | +test("hides reactions when reaction animations are disabled", () => { |
| 111 | + showReactions.setValue(false); |
| 112 | + const reaction = ReactionSet[0]; |
| 113 | + const room = new MockRoom(memberUserIdAlice); |
| 114 | + const rtcSession = new MockRTCSession(room, membership); |
| 115 | + act(() => { |
| 116 | + room.testSendReaction(memberEventAlice, reaction, membership); |
| 117 | + }); |
| 118 | + const { container } = render(<TestComponent rtcSession={rtcSession} />); |
| 119 | + expect(container.getElementsByTagName("span")).toHaveLength(0); |
| 120 | +}); |
0 commit comments