From 0395e5fdc973eb35a6658a910365c56e291bf2a0 Mon Sep 17 00:00:00 2001 From: Hugh Nimmo-Smith Date: Mon, 2 Dec 2024 11:59:20 +0000 Subject: [PATCH] Refactor the way we check for if widget or not to make it easier to mock --- src/analytics/PosthogAnalytics.ts | 4 ++-- src/auth/useInteractiveRegistration.ts | 4 ++-- src/room/MuteStates.test.tsx | 22 ++++------------------ src/room/MuteStates.ts | 4 ++-- src/room/useLoadGroupCall.ts | 4 ++-- src/settings/SettingsModal.tsx | 4 ++-- src/widget.ts | 7 +++++++ 7 files changed, 21 insertions(+), 28 deletions(-) diff --git a/src/analytics/PosthogAnalytics.ts b/src/analytics/PosthogAnalytics.ts index 0df0ee32..c93d0450 100644 --- a/src/analytics/PosthogAnalytics.ts +++ b/src/analytics/PosthogAnalytics.ts @@ -10,7 +10,7 @@ import { logger } from "matrix-js-sdk/src/logger"; import { MatrixClient } from "matrix-js-sdk/src/matrix"; import { Buffer } from "buffer"; -import { widget } from "../widget"; +import { isRunningAsWidget } from "../widget"; import { CallEndedTracker, CallStartedTracker, @@ -269,7 +269,7 @@ export class PosthogAnalytics { private async getAnalyticsId(): Promise { const client: MatrixClient = window.matrixclient; let accountAnalyticsId; - if (widget) { + if (isRunningAsWidget) { accountAnalyticsId = getUrlParams().analyticsID; } else { const accountData = await client.getAccountDataFromServer( diff --git a/src/auth/useInteractiveRegistration.ts b/src/auth/useInteractiveRegistration.ts index 2c272cb1..96cba24d 100644 --- a/src/auth/useInteractiveRegistration.ts +++ b/src/auth/useInteractiveRegistration.ts @@ -17,7 +17,7 @@ import { logger } from "matrix-js-sdk/src/logger"; import { initClient } from "../utils/matrix"; import { Session } from "../ClientContext"; import { Config } from "../config/Config"; -import { widget } from "../widget"; +import { isRunningAsWidget } from "../widget"; export const useInteractiveRegistration = ( oldClient?: MatrixClient, @@ -47,7 +47,7 @@ export const useInteractiveRegistration = ( } useEffect(() => { - if (widget) return; + if (isRunningAsWidget) return; // An empty registerRequest is used to get the privacy policy and recaptcha key. authClient.current!.registerRequest({}).catch((error) => { setPrivacyPolicyUrl( diff --git a/src/room/MuteStates.test.tsx b/src/room/MuteStates.test.tsx index ba48997f..3800fc42 100644 --- a/src/room/MuteStates.test.tsx +++ b/src/room/MuteStates.test.tsx @@ -5,7 +5,7 @@ SPDX-License-Identifier: AGPL-3.0-only Please see LICENSE in the repository root for full details. */ -import { afterAll, afterEach, describe, expect, it, vi } from "vitest"; +import { afterEach, describe, expect, it, vi } from "vitest"; import React, { ReactNode } from "react"; import { beforeEach } from "vitest"; import { render, screen } from "@testing-library/react"; @@ -18,6 +18,7 @@ import { MediaDevicesContext, } from "../livekit/MediaDevicesContext"; import { mockConfig } from "../utils/test"; +import * as widget from "../widget"; function TestComponent(): ReactNode { const muteStates = useMuteStates(); @@ -98,10 +99,7 @@ describe("useMuteStates", () => { afterEach(() => { vi.restoreAllMocks(); - }); - - afterAll(() => { - vi.clearAllMocks(); + vi.unmock("../widget"); }); it("disabled when no input devices", () => { @@ -172,19 +170,7 @@ describe("useMuteStates", () => { it("skipLobby does not mute inputs in widget mode", () => { mockConfig(); - vi.mock("../widget", () => { - return { - widget: { - api: { - transport: { - send: async (): Promise => new Promise((r) => r()), - }, - }, - lazyActions: { on: vi.fn(), off: vi.fn() }, - }, - ElementWidgetActions: {}, - }; - }); + vi.spyOn(widget, "isRunningAsWidget", "get").mockImplementation(() => true); render( diff --git a/src/room/MuteStates.ts b/src/room/MuteStates.ts index 02370b7b..1ea47cc0 100644 --- a/src/room/MuteStates.ts +++ b/src/room/MuteStates.ts @@ -17,7 +17,7 @@ import { logger } from "matrix-js-sdk/src/logger"; import { MediaDevice, useMediaDevices } from "../livekit/MediaDevicesContext"; import { useReactiveState } from "../useReactiveState"; -import { ElementWidgetActions, widget } from "../widget"; +import { ElementWidgetActions, isRunningAsWidget, widget } from "../widget"; import { Config } from "../config/Config"; import { useUrlParams } from "../UrlParams"; @@ -75,7 +75,7 @@ export function useMuteStates(): MuteStates { const { skipLobby } = useUrlParams(); // In SPA without lobby we need to protect from unmuted joins for privacy. - const allowStartUnmuted = !skipLobby || !!widget; + const allowStartUnmuted = !skipLobby || isRunningAsWidget; const audio = useMuteState(devices.audioInput, () => { return Config.get().media_devices.enable_audio && allowStartUnmuted; }); diff --git a/src/room/useLoadGroupCall.ts b/src/room/useLoadGroupCall.ts index 163571c8..a68818e3 100644 --- a/src/room/useLoadGroupCall.ts +++ b/src/room/useLoadGroupCall.ts @@ -20,7 +20,7 @@ import { KnownMembership } from "matrix-js-sdk/src/types"; import { JoinRule, MatrixError } from "matrix-js-sdk/src/matrix"; import { useTranslation } from "react-i18next"; -import { widget } from "../widget"; +import { isRunningAsWidget } from "../widget"; export type GroupCallLoaded = { kind: "loaded"; @@ -238,7 +238,7 @@ export const useLoadGroupCall = ( // room already joined so we are done here already. return room!; } - if (widget) + if (isRunningAsWidget) // in widget mode we never should reach this point. (getRoom should return the room.) throw new Error( "Room not found. The widget-api did not pass over the relevant room events/information.", diff --git a/src/settings/SettingsModal.tsx b/src/settings/SettingsModal.tsx index 78afc2c5..01478a72 100644 --- a/src/settings/SettingsModal.tsx +++ b/src/settings/SettingsModal.tsx @@ -21,7 +21,7 @@ import { useMediaDevices, useMediaDeviceNames, } from "../livekit/MediaDevicesContext"; -import { widget } from "../widget"; +import { isRunningAsWidget } from "../widget"; import { useSetting, developerSettingsTab as developerSettingsTabSetting, @@ -236,7 +236,7 @@ export const SettingsModal: FC = ({ }; const tabs = [audioTab, videoTab]; - if (widget === null) tabs.push(profileTab); + if (!isRunningAsWidget) tabs.push(profileTab); tabs.push(preferencesTab, feedbackTab, moreTab); if (developerSettingsTab) tabs.push(developerTab); diff --git a/src/widget.ts b/src/widget.ts index fb1b1cfd..d05ac4e3 100644 --- a/src/widget.ts +++ b/src/widget.ts @@ -181,3 +181,10 @@ export const widget = ((): WidgetHelpers | null => { return null; } })(); + +/** + * Whether or not we are running as a widget. + * + * @returns true if widget, false if SPA + */ +export const isRunningAsWidget: boolean = !!widget;