Fix tests

This commit is contained in:
Half-Shot
2024-11-08 13:26:15 +00:00
parent 90fa6d02d7
commit caf2d6bb5e
4 changed files with 89 additions and 100 deletions

View File

@@ -5,7 +5,7 @@ SPDX-License-Identifier: AGPL-3.0-only
Please see LICENSE in the repository root for full details. Please see LICENSE in the repository root for full details.
*/ */
import { fireEvent, render } from "@testing-library/react"; import { fireEvent, getByLabelText, render } from "@testing-library/react";
import { act } from "react"; import { act } from "react";
import { expect, test } from "vitest"; import { expect, test } from "vitest";
import { MatrixRTCSession } from "matrix-js-sdk/src/matrixrtc"; import { MatrixRTCSession } from "matrix-js-sdk/src/matrixrtc";
@@ -47,28 +47,26 @@ function TestComponent({
); );
} }
test("Can open menu", () => { test("Can open menu", async () => {
const user = userEvent.setup();
const room = new MockRoom(memberUserIdAlice); const room = new MockRoom(memberUserIdAlice);
const rtcSession = new MockRTCSession(room, membership); const rtcSession = new MockRTCSession(room, membership);
const { getByRole, container } = render( const { getByLabelText, container } = render(
<TestComponent rtcSession={rtcSession} room={room} />, <TestComponent rtcSession={rtcSession} room={room} />,
); );
act(() => getByRole("button").click()); await user.click(getByLabelText("action.raise_hand_or_send_reaction"));
expect(container).toMatchSnapshot(); expect(container).toMatchSnapshot();
}); });
test("Can raise hand", () => { test("Can raise hand", async () => {
const user = userEvent.setup();
const room = new MockRoom(memberUserIdAlice); const room = new MockRoom(memberUserIdAlice);
const rtcSession = new MockRTCSession(room, membership); const rtcSession = new MockRTCSession(room, membership);
const { getByRole, getByLabelText, container } = render( const { getByLabelText, container } = render(
<TestComponent rtcSession={rtcSession} room={room} />, <TestComponent rtcSession={rtcSession} room={room} />,
); );
act(() => { await user.click(getByLabelText("action.raise_hand_or_send_reaction"));
getByRole("button").click(); await user.click(getByLabelText("common.raise_hand"));
});
act(() => {
getByLabelText("Toggle hand raised").click();
});
expect(room.testSentEvents).toEqual([ expect(room.testSentEvents).toEqual([
[ [
undefined, undefined,
@@ -85,35 +83,29 @@ test("Can raise hand", () => {
expect(container).toMatchSnapshot(); expect(container).toMatchSnapshot();
}); });
test("Can lower hand", () => { test("Can lower hand", async () => {
const user = userEvent.setup();
const room = new MockRoom(memberUserIdAlice); const room = new MockRoom(memberUserIdAlice);
const rtcSession = new MockRTCSession(room, membership); const rtcSession = new MockRTCSession(room, membership);
const { getByRole, getByLabelText, container } = render( const { getByLabelText, container } = render(
<TestComponent rtcSession={rtcSession} room={room} />, <TestComponent rtcSession={rtcSession} room={room} />,
); );
const reactionEvent = room.testSendHandRaise(memberEventAlice, membership); const reactionEvent = room.testSendHandRaise(memberEventAlice, membership);
act(() => { await user.click(getByLabelText("action.raise_hand_or_send_reaction"));
getByRole("button").click(); await user.click(getByLabelText("common.lower_hand"));
});
act(() => {
getByLabelText("Toggle hand raised").click();
});
expect(room.testRedactedEvents).toEqual([[undefined, reactionEvent]]); expect(room.testRedactedEvents).toEqual([[undefined, reactionEvent]]);
expect(container).toMatchSnapshot(); expect(container).toMatchSnapshot();
}); });
test("Can react with emoji", () => { test("Can react with emoji", async () => {
const user = userEvent.setup();
const room = new MockRoom(memberUserIdAlice); const room = new MockRoom(memberUserIdAlice);
const rtcSession = new MockRTCSession(room, membership); const rtcSession = new MockRTCSession(room, membership);
const { getByRole, getByText } = render( const { getByLabelText, getByText } = render(
<TestComponent rtcSession={rtcSession} room={room} />, <TestComponent rtcSession={rtcSession} room={room} />,
); );
act(() => { await user.click(getByLabelText("action.raise_hand_or_send_reaction"));
getByRole("button").click(); await user.click(getByText("🐶"));
});
act(() => {
getByText("🐶").click();
});
expect(room.testSentEvents).toEqual([ expect(room.testSentEvents).toEqual([
[ [
undefined, undefined,
@@ -134,25 +126,16 @@ test("Can search for and send emoji", async () => {
const user = userEvent.setup(); const user = userEvent.setup();
const room = new MockRoom(memberUserIdAlice); const room = new MockRoom(memberUserIdAlice);
const rtcSession = new MockRTCSession(room, membership); const rtcSession = new MockRTCSession(room, membership);
const { getByText, getByRole, getByPlaceholderText, container } = render( const { getByText, container, getByLabelText } = render(
<TestComponent rtcSession={rtcSession} room={room} />, <TestComponent rtcSession={rtcSession} room={room} />,
); );
act(() => { await user.click(getByLabelText("action.raise_hand_or_send_reaction"));
getByRole("button").click(); await user.click(getByLabelText("action.open_search"));
}); // Search should autofocus.
act(() => { await user.keyboard("crickets");
getByRole("button", {
name: "Search",
}).click();
});
await act(async () => {
getByPlaceholderText("Search reactions…").focus();
await user.keyboard("crickets");
});
expect(container).toMatchSnapshot(); expect(container).toMatchSnapshot();
act(() => { await user.click(getByText("🦗"));
getByText("🦗").click();
});
expect(room.testSentEvents).toEqual([ expect(room.testSentEvents).toEqual([
[ [
undefined, undefined,
@@ -173,22 +156,14 @@ test("Can search for and send emoji with the keyboard", async () => {
const user = userEvent.setup(); const user = userEvent.setup();
const room = new MockRoom(memberUserIdAlice); const room = new MockRoom(memberUserIdAlice);
const rtcSession = new MockRTCSession(room, membership); const rtcSession = new MockRTCSession(room, membership);
const { getByRole, getByPlaceholderText, container } = render( const { getByLabelText, getByPlaceholderText, container } = render(
<TestComponent rtcSession={rtcSession} room={room} />, <TestComponent rtcSession={rtcSession} room={room} />,
); );
act(() => { await user.click(getByLabelText("action.raise_hand_or_send_reaction"));
getByRole("button").click(); await user.click(getByLabelText("action.open_search"));
}); const searchField = getByPlaceholderText("reaction_search");
act(() => { // Search should autofocus.
getByRole("button", { await user.keyboard("crickets");
name: "Search",
}).click();
});
const searchField = getByPlaceholderText("Search reactions…");
await act(async () => {
searchField.focus();
await user.keyboard("crickets");
});
expect(container).toMatchSnapshot(); expect(container).toMatchSnapshot();
act(() => { act(() => {
fireEvent.keyDown(searchField, { key: "Enter" }); fireEvent.keyDown(searchField, { key: "Enter" });
@@ -209,43 +184,29 @@ test("Can search for and send emoji with the keyboard", async () => {
]); ]);
}); });
test("Can close search", () => { test("Can close search", async () => {
const user = userEvent.setup();
const room = new MockRoom(memberUserIdAlice); const room = new MockRoom(memberUserIdAlice);
const rtcSession = new MockRTCSession(room, membership); const rtcSession = new MockRTCSession(room, membership);
const { getByRole, container } = render( const { getByLabelText, container } = render(
<TestComponent rtcSession={rtcSession} room={room} />, <TestComponent rtcSession={rtcSession} room={room} />,
); );
act(() => { await user.click(getByLabelText("action.raise_hand_or_send_reaction"));
getByRole("button").click(); await user.click(getByLabelText("action.open_search"));
}); await user.click(getByLabelText("action.close_search"));
act(() => {
getByRole("button", {
name: "Search",
}).click();
});
act(() => {
getByRole("button", {
name: "close search",
}).click();
});
expect(container).toMatchSnapshot(); expect(container).toMatchSnapshot();
}); });
test("Can close search with the escape key", () => { test("Can close search with the escape key", async () => {
const user = userEvent.setup();
const room = new MockRoom(memberUserIdAlice); const room = new MockRoom(memberUserIdAlice);
const rtcSession = new MockRTCSession(room, membership); const rtcSession = new MockRTCSession(room, membership);
const { getByRole, container, getByPlaceholderText } = render( const { getByLabelText, container, getByPlaceholderText } = render(
<TestComponent rtcSession={rtcSession} room={room} />, <TestComponent rtcSession={rtcSession} room={room} />,
); );
act(() => { await user.click(getByLabelText("action.raise_hand_or_send_reaction"));
getByRole("button").click(); await user.click(getByLabelText("action.open_search"));
}); const searchField = getByPlaceholderText("reaction_search");
act(() => {
getByRole("button", {
name: "Search",
}).click();
});
const searchField = getByPlaceholderText("Search reactions…");
act(() => { act(() => {
fireEvent.keyDown(searchField, { key: "Escape" }); fireEvent.keyDown(searchField, { key: "Escape" });
}); });

View File

@@ -60,6 +60,7 @@ const InnerButton: FC<InnerButtonProps> = ({ raised, open, ...props }) => {
<CpdButton <CpdButton
className={classNames(raised && styles.raisedButton)} className={classNames(raised && styles.raisedButton)}
aria-expanded={open} aria-expanded={open}
aria-label={t("action.raise_hand_or_send_reaction")}
kind={raised || open ? "primary" : "secondary"} kind={raised || open ? "primary" : "secondary"}
iconOnly iconOnly
Icon={raised ? RaisedHandSolidIcon : ReactionIcon} Icon={raised ? RaisedHandSolidIcon : ReactionIcon}
@@ -121,7 +122,7 @@ export function ReactionPopupMenu({
}, },
[sendReaction, filteredReactionSet, canReact, setIsSearching], [sendReaction, filteredReactionSet, canReact, setIsSearching],
); );
const label = isHandRaised ? t("common.raise_hand") : t("common.lower_hand"); const label = isHandRaised ? t("common.lower_hand") : t("common.raise_hand");
return ( return (
<> <>
{errorText && ( {errorText && (

View File

@@ -8,6 +8,7 @@ exports[`Can close search 1`] = `
<button <button
aria-disabled="false" aria-disabled="false"
aria-expanded="true" aria-expanded="true"
aria-label="action.raise_hand_or_send_reaction"
aria-labelledby=":rec:" aria-labelledby=":rec:"
class="_button_i91xf_17 _has-icon_i91xf_66 _icon-only_i91xf_59" class="_button_i91xf_17 _has-icon_i91xf_66 _icon-only_i91xf_59"
data-kind="primary" data-kind="primary"
@@ -39,6 +40,7 @@ exports[`Can close search with the escape key 1`] = `
<button <button
aria-disabled="false" aria-disabled="false"
aria-expanded="false" aria-expanded="false"
aria-label="action.raise_hand_or_send_reaction"
aria-labelledby=":rhh:" aria-labelledby=":rhh:"
class="_button_i91xf_17 _has-icon_i91xf_66 _icon-only_i91xf_59" class="_button_i91xf_17 _has-icon_i91xf_66 _icon-only_i91xf_59"
data-kind="secondary" data-kind="secondary"
@@ -66,13 +68,11 @@ exports[`Can close search with the escape key 1`] = `
`; `;
exports[`Can lower hand 1`] = ` exports[`Can lower hand 1`] = `
<div <div>
aria-hidden="true"
data-aria-hidden="true"
>
<button <button
aria-disabled="true" aria-disabled="false"
aria-expanded="true" aria-expanded="false"
aria-label="action.raise_hand_or_send_reaction"
aria-labelledby=":r3i:" aria-labelledby=":r3i:"
class="_button_i91xf_17 raisedButton _has-icon_i91xf_66 _icon-only_i91xf_59" class="_button_i91xf_17 raisedButton _has-icon_i91xf_66 _icon-only_i91xf_59"
data-kind="primary" data-kind="primary"
@@ -104,6 +104,7 @@ exports[`Can open menu 1`] = `
<button <button
aria-disabled="false" aria-disabled="false"
aria-expanded="true" aria-expanded="true"
aria-label="action.raise_hand_or_send_reaction"
aria-labelledby=":r0:" aria-labelledby=":r0:"
class="_button_i91xf_17 _has-icon_i91xf_66 _icon-only_i91xf_59" class="_button_i91xf_17 _has-icon_i91xf_66 _icon-only_i91xf_59"
data-kind="primary" data-kind="primary"
@@ -131,16 +132,14 @@ exports[`Can open menu 1`] = `
`; `;
exports[`Can raise hand 1`] = ` exports[`Can raise hand 1`] = `
<div <div>
aria-hidden="true"
data-aria-hidden="true"
>
<button <button
aria-disabled="true" aria-disabled="false"
aria-expanded="true" aria-expanded="false"
aria-label="action.raise_hand_or_send_reaction"
aria-labelledby=":r1p:" aria-labelledby=":r1p:"
class="_button_i91xf_17 _has-icon_i91xf_66 _icon-only_i91xf_59" class="_button_i91xf_17 _has-icon_i91xf_66 _icon-only_i91xf_59"
data-kind="primary" data-kind="secondary"
data-size="lg" data-size="lg"
role="button" role="button"
tabindex="0" tabindex="0"
@@ -172,6 +171,7 @@ exports[`Can search for and send emoji 1`] = `
<button <button
aria-disabled="false" aria-disabled="false"
aria-expanded="true" aria-expanded="true"
aria-label="action.raise_hand_or_send_reaction"
aria-labelledby=":r74:" aria-labelledby=":r74:"
class="_button_i91xf_17 _has-icon_i91xf_66 _icon-only_i91xf_59" class="_button_i91xf_17 _has-icon_i91xf_66 _icon-only_i91xf_59"
data-kind="primary" data-kind="primary"
@@ -206,6 +206,7 @@ exports[`Can search for and send emoji with the keyboard 1`] = `
<button <button
aria-disabled="false" aria-disabled="false"
aria-expanded="true" aria-expanded="true"
aria-label="action.raise_hand_or_send_reaction"
aria-labelledby=":ra3:" aria-labelledby=":ra3:"
class="_button_i91xf_17 _has-icon_i91xf_66 _icon-only_i91xf_59" class="_button_i91xf_17 _has-icon_i91xf_66 _icon-only_i91xf_59"
data-kind="primary" data-kind="primary"

View File

@@ -16,7 +16,7 @@ import {
TestReactionsWrapper, TestReactionsWrapper,
} from "../utils/testReactions"; } from "../utils/testReactions";
import { ReactionsAudioRenderer } from "./ReactionAudioRenderer"; import { ReactionsAudioRenderer } from "./ReactionAudioRenderer";
import { ReactionSet } from "../reactions"; import { GenericReaction, ReactionSet } from "../reactions";
import { playReactionsSound } from "../settings/settings"; import { playReactionsSound } from "../settings/settings";
const memberUserIdAlice = "@alice:example.org"; const memberUserIdAlice = "@alice:example.org";
@@ -60,7 +60,8 @@ test("preloads all audio elements", () => {
); );
const { container } = render(<TestComponent rtcSession={rtcSession} />); const { container } = render(<TestComponent rtcSession={rtcSession} />);
expect(container.getElementsByTagName("audio")).toHaveLength( expect(container.getElementsByTagName("audio")).toHaveLength(
ReactionSet.filter((r) => r.sound).length, // All reactions plus the generic sound
ReactionSet.filter((r) => r.sound).length + 1,
); );
}); });
@@ -99,6 +100,31 @@ test("will play an audio sound when there is a reaction", () => {
expect(audioIsPlaying[0]).toContain(chosenReaction.sound?.ogg); expect(audioIsPlaying[0]).toContain(chosenReaction.sound?.ogg);
}); });
test("will play the generic audio sound when there is soundless reaction", () => {
const audioIsPlaying: string[] = [];
window.HTMLMediaElement.prototype.play = async function (): Promise<void> {
audioIsPlaying.push((this.children[0] as HTMLSourceElement).src);
return Promise.resolve();
};
playReactionsSound.setValue(true);
const room = new MockRoom(memberUserIdAlice);
const rtcSession = new MockRTCSession(room, membership);
render(<TestComponent rtcSession={rtcSession} />);
// Find the first reaction with a sound effect
const chosenReaction = ReactionSet.find((r) => !r.sound);
if (!chosenReaction) {
throw Error(
"No reactions have sounds configured, this test cannot succeed",
);
}
act(() => {
room.testSendReaction(memberEventAlice, chosenReaction, membership);
});
expect(audioIsPlaying).toHaveLength(1);
expect(audioIsPlaying[0]).toContain(GenericReaction.sound?.ogg);
});
test("will play multiple audio sounds when there are multiple different reactions", () => { test("will play multiple audio sounds when there are multiple different reactions", () => {
const audioIsPlaying: string[] = []; const audioIsPlaying: string[] = [];
window.HTMLMediaElement.prototype.play = async function (): Promise<void> { window.HTMLMediaElement.prototype.play = async function (): Promise<void> {