Skip to content

Commit

Permalink
Merge branch 'rav/mediaDevices_mock' into rav/update_jest
Browse files Browse the repository at this point in the history
  • Loading branch information
richvdh committed Aug 15, 2023
2 parents 20a4703 + 67b0a9f commit fd9aa0e
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 15 deletions.
2 changes: 2 additions & 0 deletions test/audio/VoiceRecording-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Recorder from "opus-recorder/dist/recorder.min.js";
import { VoiceRecording, voiceRecorderOptions, highQualityRecorderOptions } from "../../src/audio/VoiceRecording";
import { createAudioContext } from "../..//src/audio/compat";
import MediaDeviceHandler from "../../src/MediaDeviceHandler";
import { useMockMediaDevices } from "../test-utils";

jest.mock("opus-recorder/dist/recorder.min.js");
const RecorderMock = mocked(Recorder);
Expand Down Expand Up @@ -56,6 +57,7 @@ describe("VoiceRecording", () => {
};

beforeEach(() => {
useMockMediaDevices();
recording = new VoiceRecording();
// @ts-ignore
recording.observable = {
Expand Down
3 changes: 3 additions & 0 deletions test/components/structures/PipContainer-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
mkRoomCreateEvent,
mockPlatformPeg,
flushPromises,
useMockMediaDevices,
} from "../../test-utils";
import { MatrixClientPeg } from "../../../src/MatrixClientPeg";
import { CallStore } from "../../../src/stores/CallStore";
Expand Down Expand Up @@ -84,6 +85,8 @@ describe("PipContainer", () => {
};

beforeEach(async () => {
useMockMediaDevices();

user = userEvent.setup();

stubClient();
Expand Down
8 changes: 5 additions & 3 deletions test/components/views/messages/CallEvent-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
setupAsyncStoreWithClient,
resetAsyncStoreWithClient,
wrapInMatrixClientContext,
useMockMediaDevices,
} from "../../../test-utils";
import defaultDispatcher from "../../../../src/dispatcher/dispatcher";
import { Action } from "../../../../src/dispatcher/actions";
Expand All @@ -41,9 +42,6 @@ import { ConnectionState } from "../../../../src/models/Call";
const CallEvent = wrapInMatrixClientContext(UnwrappedCallEvent);

describe("CallEvent", () => {
useMockedCalls();
jest.spyOn(HTMLMediaElement.prototype, "play").mockImplementation(async () => {});

let client: Mocked<MatrixClient>;
let room: Room;
let alice: RoomMember;
Expand All @@ -55,6 +53,10 @@ describe("CallEvent", () => {
jest.useFakeTimers();
jest.setSystemTime(0);

useMockMediaDevices();
useMockedCalls();
jest.spyOn(HTMLMediaElement.prototype, "play").mockImplementation(async () => {});

stubClient();
client = mocked(MatrixClientPeg.safeGet());
client.getUserId.mockReturnValue("@alice:example.org");
Expand Down
2 changes: 2 additions & 0 deletions test/components/views/rooms/RoomTile-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
filterConsole,
flushPromises,
mkMessage,
useMockMediaDevices,
} from "../../../test-utils";
import { CallStore } from "../../../../src/stores/CallStore";
import RoomTile from "../../../../src/components/views/rooms/RoomTile";
Expand Down Expand Up @@ -134,6 +135,7 @@ describe("RoomTile", () => {
};

beforeEach(() => {
useMockMediaDevices();
sdkContext = new TestSdkContext();

client = mocked(stubClient());
Expand Down
5 changes: 3 additions & 2 deletions test/components/views/voip/CallView-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
useMockedCalls,
MockedCall,
setupAsyncStoreWithClient,
useMockMediaDevices,
} from "../../../test-utils";
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
import { CallView as _CallView } from "../../../../src/components/views/voip/CallView";
Expand All @@ -41,7 +42,7 @@ import MediaDeviceHandler from "../../../../src/MediaDeviceHandler";

const CallView = wrapInMatrixClientContext(_CallView);

describe("CallLobby", () => {
describe("CallView", () => {
useMockedCalls();
jest.spyOn(HTMLMediaElement.prototype, "play").mockImplementation(async () => {});

Expand All @@ -50,7 +51,7 @@ describe("CallLobby", () => {
let alice: RoomMember;

beforeEach(() => {
mocked(navigator.mediaDevices.enumerateDevices).mockResolvedValue([]);
useMockMediaDevices();

stubClient();
client = mocked(MatrixClientPeg.safeGet());
Expand Down
8 changes: 0 additions & 8 deletions test/setup/setupManualMocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,3 @@ window.fetch = fetchMock.sandbox();

// @ts-ignore
window.Response = Response;

// set up mediaDevices mock
Object.defineProperty(navigator, "mediaDevices", {
value: {
enumerateDevices: jest.fn().mockResolvedValue([]),
getUserMedia: jest.fn(),
},
});
9 changes: 8 additions & 1 deletion test/stores/room-list/algorithms/Algorithm-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ import { Widget } from "matrix-widget-api";

import type { MatrixClient } from "matrix-js-sdk/src/matrix";
import type { ClientWidgetApi } from "matrix-widget-api";
import { stubClient, setupAsyncStoreWithClient, useMockedCalls, MockedCall } from "../../../test-utils";
import {
stubClient,
setupAsyncStoreWithClient,
useMockedCalls,
MockedCall,
useMockMediaDevices,
} from "../../../test-utils";
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
import DMRoomMap from "../../../../src/utils/DMRoomMap";
import { DefaultTagID } from "../../../../src/stores/room-list/models";
Expand All @@ -37,6 +43,7 @@ describe("Algorithm", () => {
let algorithm: Algorithm;

beforeEach(() => {
useMockMediaDevices();
stubClient();
client = mocked(MatrixClientPeg.safeGet());
DMRoomMap.makeShared(client);
Expand Down
9 changes: 9 additions & 0 deletions test/test-utils/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,12 @@ export const clearAllModals = async (): Promise<void> => {
}
}
};

/** Install a stub object at `navigator.mediaDevices` */
export function useMockMediaDevices(): void {
// @ts-ignore
navigator["mediaDevices"] = {
enumerateDevices: jest.fn().mockResolvedValue([]),
getUserMedia: jest.fn(),
};
}
3 changes: 2 additions & 1 deletion test/utils/media/requestMediaPermissions-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { logger } from "matrix-js-sdk/src/logger";
import { screen } from "@testing-library/react";

import { requestMediaPermissions } from "../../../src/utils/media/requestMediaPermissions";
import { flushPromises } from "../../test-utils";
import { flushPromises, useMockMediaDevices } from "../../test-utils";

describe("requestMediaPermissions", () => {
let error: Error;
Expand All @@ -34,6 +34,7 @@ describe("requestMediaPermissions", () => {
};

beforeEach(() => {
useMockMediaDevices();
error = new Error();
jest.spyOn(logger, "log");
});
Expand Down

0 comments on commit fd9aa0e

Please sign in to comment.