Refactor config mocking to shared function

This commit is contained in:
Hugh Nimmo-Smith
2024-11-23 09:15:59 +00:00
parent d32fdb9cbd
commit 69721e0826
3 changed files with 16 additions and 17 deletions

View File

@@ -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(
<MemoryRouter>
@@ -127,9 +124,7 @@ describe("useMuteStates", () => {
});
it("should be enabled by default", () => {
vi.spyOn(Config, "get").mockReturnValue({
...DEFAULT_CONFIG,
});
mockConfig();
render(
<MemoryRouter>
@@ -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(
<MemoryRouter initialEntries={["/room/?skipLobby=true"]}>

View File

@@ -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({

View File

@@ -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<ResolvedConfigOptions> = {}): void {
vi.spyOn(Config, "get").mockReturnValue({
...DEFAULT_CONFIG,
...config,
});
}