2022-01-28 16:44:03 +01:00
|
|
|
/*
|
2024-09-09 15:57:16 +02:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-01-28 16:44:03 +01:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 15:57:16 +02:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-01-28 16:44:03 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
import React from "react";
|
2024-05-02 16:53:35 +02:00
|
|
|
import { render, screen, waitFor } from "@testing-library/react";
|
2022-01-28 16:44:03 +01:00
|
|
|
|
|
|
|
import MatrixClientContext from "../../../../src/contexts/MatrixClientContext";
|
2022-12-23 11:46:14 +01:00
|
|
|
import RoomContext from "../../../../src/contexts/RoomContext";
|
|
|
|
import { createTestClient, getRoomContext, mkStubRoom } from "../../../test-utils";
|
2022-01-28 16:44:03 +01:00
|
|
|
import { IRoomState } from "../../../../src/components/structures/RoomView";
|
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
2022-03-02 22:09:24 +01:00
|
|
|
import MessageComposerButtons from "../../../../src/components/views/rooms/MessageComposerButtons";
|
2022-01-28 16:44:03 +01:00
|
|
|
|
|
|
|
describe("MessageComposerButtons", () => {
|
2022-12-23 11:46:14 +01:00
|
|
|
// @ts-ignore - we're deliberately not implementing the whole interface here, but
|
|
|
|
// can't use Partial<> for types because it'll annoy TS more than it helps.
|
|
|
|
const mockProps: React.ComponentProps<typeof MessageComposerButtons> = {
|
|
|
|
addEmoji: () => false,
|
|
|
|
haveRecording: false,
|
|
|
|
isStickerPickerOpen: false,
|
|
|
|
menuPosition: undefined,
|
|
|
|
onRecordStartEndClick: () => {},
|
|
|
|
setStickerPickerOpen: () => {},
|
|
|
|
toggleButtonMenu: () => {},
|
|
|
|
};
|
|
|
|
|
|
|
|
const mockClient = createTestClient();
|
|
|
|
jest.spyOn(MatrixClientPeg, "get").mockReturnValue(mockClient);
|
|
|
|
|
|
|
|
function getButtonLabels() {
|
|
|
|
const getLabels = (elements: HTMLElement[]): string[] =>
|
|
|
|
elements
|
|
|
|
.map((element) => element.getAttribute("aria-label"))
|
|
|
|
.filter((label): label is string => label !== null);
|
|
|
|
|
|
|
|
const mainLabels: Array<string | string[]> = getLabels(screen.queryAllByRole("button"));
|
|
|
|
const menuLabels = getLabels(screen.queryAllByRole("menuitem"));
|
|
|
|
|
|
|
|
if (menuLabels.length) {
|
|
|
|
mainLabels.push(getLabels(screen.queryAllByRole("menuitem")));
|
|
|
|
}
|
|
|
|
|
|
|
|
return mainLabels;
|
|
|
|
}
|
|
|
|
|
|
|
|
function wrapAndRender(component: React.ReactElement, narrow: boolean) {
|
|
|
|
const mockRoom = mkStubRoom("myfakeroom", "myfakeroom", mockClient) as any;
|
|
|
|
const defaultRoomContext: IRoomState = getRoomContext(mockRoom, { narrow });
|
|
|
|
|
|
|
|
return render(
|
|
|
|
<MatrixClientContext.Provider value={mockClient}>
|
|
|
|
<RoomContext.Provider value={defaultRoomContext}>{component}</RoomContext.Provider>
|
|
|
|
</MatrixClientContext.Provider>,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-02 10:30:53 +01:00
|
|
|
it("Renders emoji and upload buttons in wide mode", () => {
|
2022-12-23 11:46:14 +01:00
|
|
|
wrapAndRender(
|
2022-01-28 16:44:03 +01:00
|
|
|
<MessageComposerButtons
|
2022-09-16 11:10:33 +02:00
|
|
|
{...mockProps}
|
2022-01-28 16:44:03 +01:00
|
|
|
isMenuOpen={false}
|
|
|
|
showLocationButton={true}
|
2022-03-04 19:04:30 +01:00
|
|
|
showPollsButton={true}
|
2022-01-28 16:44:03 +01:00
|
|
|
showStickersButton={true}
|
2022-02-02 10:30:53 +01:00
|
|
|
/>,
|
2022-02-23 15:03:46 +01:00
|
|
|
false,
|
2022-02-02 10:30:53 +01:00
|
|
|
);
|
|
|
|
|
2022-12-23 11:46:14 +01:00
|
|
|
expect(getButtonLabels()).toEqual(["Emoji", "Attachment", "More options"]);
|
2022-02-02 10:30:53 +01:00
|
|
|
});
|
|
|
|
|
2024-05-02 16:53:35 +02:00
|
|
|
it("Renders other buttons in menu in wide mode", async () => {
|
2022-12-23 11:46:14 +01:00
|
|
|
wrapAndRender(
|
2022-02-02 10:30:53 +01:00
|
|
|
<MessageComposerButtons
|
2022-09-16 11:10:33 +02:00
|
|
|
{...mockProps}
|
2022-02-02 10:30:53 +01:00
|
|
|
isMenuOpen={true}
|
|
|
|
showLocationButton={true}
|
2022-03-04 19:04:30 +01:00
|
|
|
showPollsButton={true}
|
2022-02-02 10:30:53 +01:00
|
|
|
showStickersButton={true}
|
2022-01-28 16:44:03 +01:00
|
|
|
/>,
|
2022-02-23 15:03:46 +01:00
|
|
|
false,
|
2022-01-28 16:44:03 +01:00
|
|
|
);
|
|
|
|
|
2024-05-02 16:53:35 +02:00
|
|
|
// The location code is lazy loaded, so the button will take a little while
|
|
|
|
// to appear, so we need to wait.
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(getButtonLabels()).toEqual([
|
|
|
|
"Emoji",
|
|
|
|
"Attachment",
|
|
|
|
"More options",
|
|
|
|
["Sticker", "Voice Message", "Poll", "Location"],
|
|
|
|
]);
|
|
|
|
});
|
2022-01-28 16:44:03 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Renders only some buttons in narrow mode", () => {
|
2022-12-23 11:46:14 +01:00
|
|
|
wrapAndRender(
|
2022-01-28 16:44:03 +01:00
|
|
|
<MessageComposerButtons
|
2022-09-16 11:10:33 +02:00
|
|
|
{...mockProps}
|
2022-01-28 16:44:03 +01:00
|
|
|
isMenuOpen={false}
|
|
|
|
showLocationButton={true}
|
2022-03-04 19:04:30 +01:00
|
|
|
showPollsButton={true}
|
2022-01-28 16:44:03 +01:00
|
|
|
showStickersButton={true}
|
|
|
|
/>,
|
2022-02-23 15:03:46 +01:00
|
|
|
true,
|
2022-01-28 16:44:03 +01:00
|
|
|
);
|
|
|
|
|
2022-12-23 11:46:14 +01:00
|
|
|
expect(getButtonLabels()).toEqual(["Emoji", "More options"]);
|
2022-01-28 16:44:03 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Renders other buttons in menu (except voice messages) in narrow mode", () => {
|
2022-12-23 11:46:14 +01:00
|
|
|
wrapAndRender(
|
2022-01-28 16:44:03 +01:00
|
|
|
<MessageComposerButtons
|
2022-09-16 11:10:33 +02:00
|
|
|
{...mockProps}
|
2022-01-28 16:44:03 +01:00
|
|
|
isMenuOpen={true}
|
|
|
|
showLocationButton={true}
|
2022-03-04 19:04:30 +01:00
|
|
|
showPollsButton={true}
|
2022-01-28 16:44:03 +01:00
|
|
|
showStickersButton={true}
|
|
|
|
/>,
|
2022-02-23 15:03:46 +01:00
|
|
|
true,
|
2022-01-28 16:44:03 +01:00
|
|
|
);
|
|
|
|
|
2022-12-23 11:46:14 +01:00
|
|
|
expect(getButtonLabels()).toEqual(["Emoji", "More options", ["Attachment", "Sticker", "Poll", "Location"]]);
|
2022-01-28 16:44:03 +01:00
|
|
|
});
|
2022-03-04 19:04:30 +01:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("polls button", () => {
|
|
|
|
it("should render when asked to", () => {
|
2022-12-23 11:46:14 +01:00
|
|
|
wrapAndRender(
|
2022-03-04 19:04:30 +01:00
|
|
|
<MessageComposerButtons
|
2022-09-16 11:10:33 +02:00
|
|
|
{...mockProps}
|
2022-03-04 19:04:30 +01:00
|
|
|
isMenuOpen={true}
|
|
|
|
showLocationButton={true}
|
|
|
|
showPollsButton={true}
|
|
|
|
showStickersButton={true}
|
|
|
|
/>,
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
|
2022-12-23 11:46:14 +01:00
|
|
|
expect(getButtonLabels()).toEqual(["Emoji", "More options", ["Attachment", "Sticker", "Poll", "Location"]]);
|
2022-03-04 19:04:30 +01:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should not render when asked not to", () => {
|
2022-12-23 11:46:14 +01:00
|
|
|
wrapAndRender(
|
2022-03-04 19:04:30 +01:00
|
|
|
<MessageComposerButtons
|
2022-09-16 11:10:33 +02:00
|
|
|
{...mockProps}
|
2022-03-04 19:04:30 +01:00
|
|
|
isMenuOpen={true}
|
|
|
|
showLocationButton={true}
|
|
|
|
showPollsButton={false} // !! the change from the alternate test
|
|
|
|
showStickersButton={true}
|
|
|
|
/>,
|
|
|
|
true,
|
|
|
|
);
|
|
|
|
|
2022-12-23 11:46:14 +01:00
|
|
|
expect(getButtonLabels()).toEqual([
|
2022-03-04 19:04:30 +01:00
|
|
|
"Emoji",
|
|
|
|
"More options",
|
|
|
|
[
|
|
|
|
"Attachment",
|
|
|
|
"Sticker",
|
|
|
|
// "Poll", // should be hidden
|
|
|
|
"Location",
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
2022-09-16 11:10:33 +02:00
|
|
|
|
|
|
|
describe("with showVoiceBroadcastButton = true", () => {
|
|
|
|
it("should render the »Voice broadcast« button", () => {
|
2022-12-23 11:46:14 +01:00
|
|
|
wrapAndRender(
|
2022-09-16 11:10:33 +02:00
|
|
|
<MessageComposerButtons
|
|
|
|
{...mockProps}
|
|
|
|
isMenuOpen={true}
|
|
|
|
showLocationButton={true}
|
|
|
|
showPollsButton={true}
|
|
|
|
showStickersButton={true}
|
|
|
|
showVoiceBroadcastButton={true}
|
|
|
|
/>,
|
|
|
|
false,
|
|
|
|
);
|
|
|
|
|
2022-12-23 11:46:14 +01:00
|
|
|
expect(getButtonLabels()).toEqual([
|
2022-09-16 11:10:33 +02:00
|
|
|
"Emoji",
|
|
|
|
"Attachment",
|
|
|
|
"More options",
|
2022-12-12 12:24:14 +01:00
|
|
|
["Sticker", "Voice Message", "Voice broadcast", "Poll", "Location"],
|
2022-09-16 11:10:33 +02:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
2022-01-28 16:44:03 +01:00
|
|
|
});
|