2022-11-18 07:36:20 +00:00
|
|
|
/*
|
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from "react";
|
2024-02-02 11:43:59 +00:00
|
|
|
import { act, render, RenderResult, screen, waitFor } from "@testing-library/react";
|
|
|
|
import { MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
|
|
|
|
import { mocked } from "jest-mock";
|
2022-11-18 07:36:20 +00:00
|
|
|
|
2022-12-27 07:39:26 +00:00
|
|
|
import UnwrappedUserMenu from "../../../src/components/structures/UserMenu";
|
|
|
|
import { stubClient, wrapInSdkContext } from "../../test-utils";
|
2022-11-18 07:36:20 +00:00
|
|
|
import {
|
|
|
|
VoiceBroadcastInfoState,
|
|
|
|
VoiceBroadcastRecording,
|
|
|
|
VoiceBroadcastRecordingsStore,
|
|
|
|
} from "../../../src/voice-broadcast";
|
|
|
|
import { mkVoiceBroadcastInfoStateEvent } from "../../voice-broadcast/utils/test-utils";
|
2022-12-27 07:39:26 +00:00
|
|
|
import { TestSdkContext } from "../../TestSdkContext";
|
2024-02-02 11:43:59 +00:00
|
|
|
import defaultDispatcher from "../../../src/dispatcher/dispatcher";
|
|
|
|
import LogoutDialog from "../../../src/components/views/dialogs/LogoutDialog";
|
|
|
|
import Modal from "../../../src/Modal";
|
2022-11-18 07:36:20 +00:00
|
|
|
|
|
|
|
describe("<UserMenu>", () => {
|
|
|
|
let client: MatrixClient;
|
|
|
|
let renderResult: RenderResult;
|
2022-12-27 07:39:26 +00:00
|
|
|
let sdkContext: TestSdkContext;
|
2022-11-18 07:36:20 +00:00
|
|
|
|
|
|
|
beforeEach(() => {
|
2022-12-27 07:39:26 +00:00
|
|
|
sdkContext = new TestSdkContext();
|
2022-11-18 07:36:20 +00:00
|
|
|
});
|
|
|
|
|
2024-02-02 11:43:59 +00:00
|
|
|
describe("<UserMenu> when video broadcast", () => {
|
|
|
|
let voiceBroadcastInfoEvent: MatrixEvent;
|
|
|
|
let voiceBroadcastRecording: VoiceBroadcastRecording;
|
|
|
|
let voiceBroadcastRecordingsStore: VoiceBroadcastRecordingsStore;
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
client = stubClient();
|
|
|
|
voiceBroadcastInfoEvent = mkVoiceBroadcastInfoStateEvent(
|
|
|
|
"!room:example.com",
|
|
|
|
VoiceBroadcastInfoState.Started,
|
|
|
|
client.getUserId() || "",
|
|
|
|
client.getDeviceId() || "",
|
|
|
|
);
|
2022-11-18 07:36:20 +00:00
|
|
|
});
|
|
|
|
|
2024-02-02 11:43:59 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
voiceBroadcastRecordingsStore = new VoiceBroadcastRecordingsStore();
|
|
|
|
sdkContext._VoiceBroadcastRecordingsStore = voiceBroadcastRecordingsStore;
|
|
|
|
|
|
|
|
voiceBroadcastRecording = new VoiceBroadcastRecording(voiceBroadcastInfoEvent, client);
|
2022-11-18 07:36:20 +00:00
|
|
|
});
|
|
|
|
|
2024-02-02 11:43:59 +00:00
|
|
|
describe("when rendered", () => {
|
2022-11-18 07:36:20 +00:00
|
|
|
beforeEach(() => {
|
2024-02-02 11:43:59 +00:00
|
|
|
const UserMenu = wrapInSdkContext(UnwrappedUserMenu, sdkContext);
|
|
|
|
renderResult = render(<UserMenu isPanelCollapsed={true} />);
|
2022-11-18 07:36:20 +00:00
|
|
|
});
|
|
|
|
|
2024-02-02 11:43:59 +00:00
|
|
|
it("should render as expected", () => {
|
|
|
|
expect(renderResult.container).toMatchSnapshot();
|
2022-11-18 07:36:20 +00:00
|
|
|
});
|
|
|
|
|
2024-02-02 11:43:59 +00:00
|
|
|
describe("and a live voice broadcast starts", () => {
|
2022-11-18 07:36:20 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
act(() => {
|
2024-02-02 11:43:59 +00:00
|
|
|
voiceBroadcastRecordingsStore.setCurrent(voiceBroadcastRecording);
|
2022-11-18 07:36:20 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-02-02 11:43:59 +00:00
|
|
|
it("should render the live voice broadcast avatar addon", () => {
|
|
|
|
expect(renderResult.queryByTestId("user-menu-live-vb")).toBeInTheDocument();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("and the broadcast ends", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
act(() => {
|
|
|
|
voiceBroadcastRecordingsStore.clearCurrent();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not render the live voice broadcast avatar addon", () => {
|
|
|
|
expect(renderResult.queryByTestId("user-menu-live-vb")).not.toBeInTheDocument();
|
|
|
|
});
|
2022-11-18 07:36:20 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2024-02-02 11:43:59 +00:00
|
|
|
|
|
|
|
describe("<UserMenu> logout", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
client = stubClient();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should logout directly if no crypto", async () => {
|
|
|
|
const UserMenu = wrapInSdkContext(UnwrappedUserMenu, sdkContext);
|
|
|
|
renderResult = render(<UserMenu isPanelCollapsed={true} />);
|
|
|
|
|
|
|
|
mocked(client.getRooms).mockReturnValue([
|
|
|
|
{
|
|
|
|
roomId: "!room0",
|
|
|
|
} as unknown as Room,
|
|
|
|
{
|
|
|
|
roomId: "!room1",
|
|
|
|
} as unknown as Room,
|
|
|
|
]);
|
|
|
|
jest.spyOn(client, "getCrypto").mockReturnValue(undefined);
|
|
|
|
|
|
|
|
const spy = jest.spyOn(defaultDispatcher, "dispatch");
|
|
|
|
screen.getByRole("button", { name: /User menu/i }).click();
|
|
|
|
screen.getByRole("menuitem", { name: /Sign out/i }).click();
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(spy).toHaveBeenCalledWith({ action: "logout" });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should logout directly if no encrypted rooms", async () => {
|
|
|
|
const UserMenu = wrapInSdkContext(UnwrappedUserMenu, sdkContext);
|
|
|
|
renderResult = render(<UserMenu isPanelCollapsed={true} />);
|
|
|
|
|
|
|
|
mocked(client.getRooms).mockReturnValue([
|
|
|
|
{
|
|
|
|
roomId: "!room0",
|
|
|
|
} as unknown as Room,
|
|
|
|
{
|
|
|
|
roomId: "!room1",
|
|
|
|
} as unknown as Room,
|
|
|
|
]);
|
|
|
|
const crypto = client.getCrypto()!;
|
|
|
|
|
|
|
|
jest.spyOn(crypto, "isEncryptionEnabledInRoom").mockResolvedValue(false);
|
|
|
|
|
|
|
|
const spy = jest.spyOn(defaultDispatcher, "dispatch");
|
|
|
|
screen.getByRole("button", { name: /User menu/i }).click();
|
|
|
|
screen.getByRole("menuitem", { name: /Sign out/i }).click();
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(spy).toHaveBeenCalledWith({ action: "logout" });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should show dialog if some encrypted rooms", async () => {
|
|
|
|
const UserMenu = wrapInSdkContext(UnwrappedUserMenu, sdkContext);
|
|
|
|
renderResult = render(<UserMenu isPanelCollapsed={true} />);
|
|
|
|
|
|
|
|
mocked(client.getRooms).mockReturnValue([
|
|
|
|
{
|
|
|
|
roomId: "!room0",
|
|
|
|
} as unknown as Room,
|
|
|
|
{
|
|
|
|
roomId: "!room1",
|
|
|
|
} as unknown as Room,
|
|
|
|
]);
|
|
|
|
const crypto = client.getCrypto()!;
|
|
|
|
|
|
|
|
jest.spyOn(crypto, "isEncryptionEnabledInRoom").mockImplementation(async (roomId: string) => {
|
|
|
|
return roomId === "!room0";
|
|
|
|
});
|
|
|
|
|
|
|
|
const spy = jest.spyOn(Modal, "createDialog");
|
|
|
|
screen.getByRole("button", { name: /User menu/i }).click();
|
|
|
|
screen.getByRole("menuitem", { name: /Sign out/i }).click();
|
|
|
|
|
|
|
|
await waitFor(() => {
|
|
|
|
expect(spy).toHaveBeenCalledWith(LogoutDialog);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-11-18 07:36:20 +00:00
|
|
|
});
|