From 69721e082649f275b8896bbdadebcd654242b5f6 Mon Sep 17 00:00:00 2001 From: Hugh Nimmo-Smith Date: Sat, 23 Nov 2024 09:15:59 +0000 Subject: [PATCH] Refactor config mocking to shared function --- src/room/MuteStates.test.tsx | 18 +++++------------- src/rtcSessionHelper.test.ts | 6 ++---- src/utils/test.ts | 9 +++++++++ 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/room/MuteStates.test.tsx b/src/room/MuteStates.test.tsx index efeff5ba..6cc5815c 100644 --- a/src/room/MuteStates.test.tsx +++ b/src/room/MuteStates.test.tsx @@ -11,14 +11,13 @@ import { beforeEach } from "vitest"; import { render, screen } from "@testing-library/react"; import { MemoryRouter } from "react-router-dom"; -import { Config } from "../config/Config"; -import { DEFAULT_CONFIG } from "../config/ConfigOptions"; import { useMuteStates } from "./MuteStates"; import { MediaDevice, MediaDevices, MediaDevicesContext, } from "../livekit/MediaDevicesContext"; +import { mockConfig } from "../utils/test"; function TestComponent(): ReactNode { const muteStates = useMuteStates(); @@ -106,9 +105,7 @@ describe("useMuteStates", () => { }); it("disabled when no input devices", () => { - vi.spyOn(Config, "get").mockReturnValue({ - ...DEFAULT_CONFIG, - }); + mockConfig(); render( @@ -127,9 +124,7 @@ describe("useMuteStates", () => { }); it("should be enabled by default", () => { - vi.spyOn(Config, "get").mockReturnValue({ - ...DEFAULT_CONFIG, - }); + mockConfig(); render( @@ -143,8 +138,7 @@ describe("useMuteStates", () => { }); it("uses defaults from config", () => { - vi.spyOn(Config, "get").mockReturnValue({ - ...DEFAULT_CONFIG, + mockConfig({ media_devices: { enable_audio: false, enable_video: false, @@ -163,9 +157,7 @@ describe("useMuteStates", () => { }); it("skipLobby mutes inputs", () => { - vi.spyOn(Config, "get").mockReturnValue({ - ...DEFAULT_CONFIG, - }); + mockConfig(); render( diff --git a/src/rtcSessionHelper.test.ts b/src/rtcSessionHelper.test.ts index a7c8781f..61cd990e 100644 --- a/src/rtcSessionHelper.test.ts +++ b/src/rtcSessionHelper.test.ts @@ -9,8 +9,7 @@ import { MatrixRTCSession } from "matrix-js-sdk/src/matrixrtc/MatrixRTCSession"; import { expect, test, vi } from "vitest"; import { enterRTCSession } from "../src/rtcSessionHelpers"; -import { Config } from "../src/config/Config"; -import { DEFAULT_CONFIG } from "./config/ConfigOptions"; +import { mockConfig } from "./utils/test"; test("It joins the correct Session", async () => { const focusFromOlderMembership = { @@ -34,8 +33,7 @@ test("It joins the correct Session", async () => { ], }; - vi.spyOn(Config, "get").mockReturnValue({ - ...DEFAULT_CONFIG, + mockConfig({ livekit: { livekit_service_url: "http://my-default-service-url.com" }, }); const mockedSession = vi.mocked({ diff --git a/src/utils/test.ts b/src/utils/test.ts index 5988dd6f..f583f797 100644 --- a/src/utils/test.ts +++ b/src/utils/test.ts @@ -21,6 +21,8 @@ import { RemoteUserMediaViewModel, } from "../state/MediaViewModel"; import { E2eeType } from "../e2ee/e2eeType"; +import { DEFAULT_CONFIG, ResolvedConfigOptions } from "../config/ConfigOptions"; +import { Config } from "../config/Config"; export function withFakeTimers(continuation: () => void): void { vi.useFakeTimers(); @@ -197,3 +199,10 @@ export async function withRemoteMedia( vm.destroy(); } } + +export function mockConfig(config: Partial = {}): void { + vi.spyOn(Config, "get").mockReturnValue({ + ...DEFAULT_CONFIG, + ...config, + }); +}