|
| 1 | +/* |
| 2 | +Copyright 2025 New Vector Ltd. |
| 3 | +
|
| 4 | +SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial |
| 5 | +Please see LICENSE in the repository root for full details. |
| 6 | +*/ |
| 7 | + |
| 8 | +import { describe, expect, test, vi } from "vitest"; |
| 9 | +import { render, screen } from "@testing-library/react"; |
| 10 | +import { type ReactElement, type ReactNode } from "react"; |
| 11 | +import { BrowserRouter } from "react-router-dom"; |
| 12 | +import userEvent from "@testing-library/user-event"; |
| 13 | + |
| 14 | +import { GroupCallErrorBoundary } from "./GroupCallErrorBoundary.tsx"; |
| 15 | +import { |
| 16 | + ConnectionLostError, |
| 17 | + E2EENotSupportedError, |
| 18 | + type ElementCallError, |
| 19 | + InsufficientCapacityError, |
| 20 | + MatrixRTCFocusMissingError, |
| 21 | + UnknownCallError, |
| 22 | +} from "../utils/errors.ts"; |
| 23 | +import { mockConfig } from "../utils/test.ts"; |
| 24 | + |
| 25 | +test.each([ |
| 26 | + { |
| 27 | + error: new MatrixRTCFocusMissingError("example.com"), |
| 28 | + expectedTitle: "Call is not supported", |
| 29 | + }, |
| 30 | + { |
| 31 | + error: new ConnectionLostError(), |
| 32 | + expectedTitle: "Connection lost", |
| 33 | + expectedDescription: "You were disconnected from the call.", |
| 34 | + }, |
| 35 | + { |
| 36 | + error: new E2EENotSupportedError(), |
| 37 | + expectedTitle: "Incompatible browser", |
| 38 | + expectedDescription: |
| 39 | + "Your web browser does not support encrypted calls. Supported browsers include Chrome, Safari, and Firefox 117+.", |
| 40 | + }, |
| 41 | + { |
| 42 | + error: new InsufficientCapacityError(), |
| 43 | + expectedTitle: "Insufficient capacity", |
| 44 | + expectedDescription: |
| 45 | + "The server has reached its maximum capacity and you cannot join the call at this time. Try again later, or contact your server admin if the problem persists.", |
| 46 | + }, |
| 47 | +])( |
| 48 | + "should report correct error for $expectedTitle", |
| 49 | + async ({ error, expectedTitle, expectedDescription }) => { |
| 50 | + const TestComponent = (): ReactNode => { |
| 51 | + throw error; |
| 52 | + }; |
| 53 | + |
| 54 | + const onErrorMock = vi.fn(); |
| 55 | + const { asFragment } = render( |
| 56 | + <BrowserRouter> |
| 57 | + <GroupCallErrorBoundary onError={onErrorMock}> |
| 58 | + <TestComponent /> |
| 59 | + </GroupCallErrorBoundary> |
| 60 | + </BrowserRouter>, |
| 61 | + ); |
| 62 | + |
| 63 | + await screen.findByText(expectedTitle); |
| 64 | + if (expectedDescription) { |
| 65 | + expect(screen.queryByText(expectedDescription)).toBeInTheDocument(); |
| 66 | + } |
| 67 | + expect(onErrorMock).toHaveBeenCalledWith(error); |
| 68 | + |
| 69 | + expect(asFragment()).toMatchSnapshot(); |
| 70 | + }, |
| 71 | +); |
| 72 | + |
| 73 | +test("should render the error page with link back to home", async () => { |
| 74 | + const error = new MatrixRTCFocusMissingError("example.com"); |
| 75 | + const TestComponent = (): ReactNode => { |
| 76 | + throw error; |
| 77 | + }; |
| 78 | + |
| 79 | + const onErrorMock = vi.fn(); |
| 80 | + const { asFragment } = render( |
| 81 | + <BrowserRouter> |
| 82 | + <GroupCallErrorBoundary onError={onErrorMock}> |
| 83 | + <TestComponent /> |
| 84 | + </GroupCallErrorBoundary> |
| 85 | + </BrowserRouter>, |
| 86 | + ); |
| 87 | + |
| 88 | + await screen.findByText("Call is not supported"); |
| 89 | + expect(screen.getByText(/Domain: example.com/i)).toBeInTheDocument(); |
| 90 | + expect( |
| 91 | + screen.getByText(/Error Code: MISSING_MATRIX_RTC_FOCUS/i), |
| 92 | + ).toBeInTheDocument(); |
| 93 | + |
| 94 | + await screen.findByRole("button", { name: "Return to home screen" }); |
| 95 | + |
| 96 | + expect(onErrorMock).toHaveBeenCalledOnce(); |
| 97 | + expect(onErrorMock).toHaveBeenCalledWith(error); |
| 98 | + |
| 99 | + expect(asFragment()).toMatchSnapshot(); |
| 100 | +}); |
| 101 | + |
| 102 | +test("should have a reconnect button for ConnectionLostError", async () => { |
| 103 | + const user = userEvent.setup(); |
| 104 | + |
| 105 | + const reconnectCallback = vi.fn(); |
| 106 | + |
| 107 | + const TestComponent = (): ReactNode => { |
| 108 | + throw new ConnectionLostError(); |
| 109 | + }; |
| 110 | + |
| 111 | + const { asFragment } = render( |
| 112 | + <BrowserRouter> |
| 113 | + <GroupCallErrorBoundary |
| 114 | + onError={vi.fn()} |
| 115 | + recoveryActionHandler={reconnectCallback} |
| 116 | + > |
| 117 | + <TestComponent /> |
| 118 | + </GroupCallErrorBoundary> |
| 119 | + </BrowserRouter>, |
| 120 | + ); |
| 121 | + |
| 122 | + await screen.findByText("Connection lost"); |
| 123 | + await screen.findByRole("button", { name: "Reconnect" }); |
| 124 | + await screen.findByRole("button", { name: "Return to home screen" }); |
| 125 | + |
| 126 | + expect(asFragment()).toMatchSnapshot(); |
| 127 | + |
| 128 | + await user.click(screen.getByRole("button", { name: "Reconnect" })); |
| 129 | + |
| 130 | + expect(reconnectCallback).toHaveBeenCalledOnce(); |
| 131 | + expect(reconnectCallback).toHaveBeenCalledWith("reconnect"); |
| 132 | +}); |
| 133 | + |
| 134 | +describe("Rageshake button", () => { |
| 135 | + function setupTest(testError: ElementCallError): void { |
| 136 | + mockConfig({ |
| 137 | + rageshake: { |
| 138 | + submit_url: "https://rageshake.example.com.localhost", |
| 139 | + }, |
| 140 | + }); |
| 141 | + |
| 142 | + const TestComponent = (): ReactElement => { |
| 143 | + throw testError; |
| 144 | + }; |
| 145 | + |
| 146 | + render( |
| 147 | + <BrowserRouter> |
| 148 | + <GroupCallErrorBoundary onError={vi.fn()}> |
| 149 | + <TestComponent /> |
| 150 | + </GroupCallErrorBoundary> |
| 151 | + </BrowserRouter>, |
| 152 | + ); |
| 153 | + } |
| 154 | + |
| 155 | + test("should show send rageshake button for unknown errors", () => { |
| 156 | + setupTest(new UnknownCallError(new Error("FOO"))); |
| 157 | + |
| 158 | + expect( |
| 159 | + screen.queryByRole("button", { name: "Send debug logs" }), |
| 160 | + ).toBeInTheDocument(); |
| 161 | + }); |
| 162 | + |
| 163 | + test("should not show send rageshake button for call errors", () => { |
| 164 | + setupTest(new E2EENotSupportedError()); |
| 165 | + |
| 166 | + expect( |
| 167 | + screen.queryByRole("button", { name: "Send debug logs" }), |
| 168 | + ).not.toBeInTheDocument(); |
| 169 | + }); |
| 170 | +}); |
0 commit comments