Move mediaDevices
mock out of setupManualMocks
(#11413)
* Move `mediaDevices` mock out of `setupManualMocks` This doesn't work well in test suites with multiple tests, because the `mockReturnValue` is reset for subsequent tests. In any case, having it mocked out automagically is *magical*. Let's make it opt-in. * clarify ts-ignore
This commit is contained in:
parent
d569ba0cfe
commit
3fda0299a5
9 changed files with 34 additions and 15 deletions
|
@ -21,6 +21,7 @@ import Recorder from "opus-recorder/dist/recorder.min.js";
|
||||||
import { VoiceRecording, voiceRecorderOptions, highQualityRecorderOptions } from "../../src/audio/VoiceRecording";
|
import { VoiceRecording, voiceRecorderOptions, highQualityRecorderOptions } from "../../src/audio/VoiceRecording";
|
||||||
import { createAudioContext } from "../..//src/audio/compat";
|
import { createAudioContext } from "../..//src/audio/compat";
|
||||||
import MediaDeviceHandler from "../../src/MediaDeviceHandler";
|
import MediaDeviceHandler from "../../src/MediaDeviceHandler";
|
||||||
|
import { useMockMediaDevices } from "../test-utils";
|
||||||
|
|
||||||
jest.mock("opus-recorder/dist/recorder.min.js");
|
jest.mock("opus-recorder/dist/recorder.min.js");
|
||||||
const RecorderMock = mocked(Recorder);
|
const RecorderMock = mocked(Recorder);
|
||||||
|
@ -56,6 +57,7 @@ describe("VoiceRecording", () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
useMockMediaDevices();
|
||||||
recording = new VoiceRecording();
|
recording = new VoiceRecording();
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
recording.observable = {
|
recording.observable = {
|
||||||
|
|
|
@ -35,6 +35,7 @@ import {
|
||||||
mkRoomCreateEvent,
|
mkRoomCreateEvent,
|
||||||
mockPlatformPeg,
|
mockPlatformPeg,
|
||||||
flushPromises,
|
flushPromises,
|
||||||
|
useMockMediaDevices,
|
||||||
} from "../../test-utils";
|
} from "../../test-utils";
|
||||||
import { MatrixClientPeg } from "../../../src/MatrixClientPeg";
|
import { MatrixClientPeg } from "../../../src/MatrixClientPeg";
|
||||||
import { CallStore } from "../../../src/stores/CallStore";
|
import { CallStore } from "../../../src/stores/CallStore";
|
||||||
|
@ -84,6 +85,8 @@ describe("PipContainer", () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
|
useMockMediaDevices();
|
||||||
|
|
||||||
user = userEvent.setup();
|
user = userEvent.setup();
|
||||||
|
|
||||||
stubClient();
|
stubClient();
|
||||||
|
|
|
@ -29,6 +29,7 @@ import {
|
||||||
setupAsyncStoreWithClient,
|
setupAsyncStoreWithClient,
|
||||||
resetAsyncStoreWithClient,
|
resetAsyncStoreWithClient,
|
||||||
wrapInMatrixClientContext,
|
wrapInMatrixClientContext,
|
||||||
|
useMockMediaDevices,
|
||||||
} from "../../../test-utils";
|
} from "../../../test-utils";
|
||||||
import defaultDispatcher from "../../../../src/dispatcher/dispatcher";
|
import defaultDispatcher from "../../../../src/dispatcher/dispatcher";
|
||||||
import { Action } from "../../../../src/dispatcher/actions";
|
import { Action } from "../../../../src/dispatcher/actions";
|
||||||
|
@ -41,9 +42,6 @@ import { ConnectionState } from "../../../../src/models/Call";
|
||||||
const CallEvent = wrapInMatrixClientContext(UnwrappedCallEvent);
|
const CallEvent = wrapInMatrixClientContext(UnwrappedCallEvent);
|
||||||
|
|
||||||
describe("CallEvent", () => {
|
describe("CallEvent", () => {
|
||||||
useMockedCalls();
|
|
||||||
jest.spyOn(HTMLMediaElement.prototype, "play").mockImplementation(async () => {});
|
|
||||||
|
|
||||||
let client: Mocked<MatrixClient>;
|
let client: Mocked<MatrixClient>;
|
||||||
let room: Room;
|
let room: Room;
|
||||||
let alice: RoomMember;
|
let alice: RoomMember;
|
||||||
|
@ -55,6 +53,10 @@ describe("CallEvent", () => {
|
||||||
jest.useFakeTimers();
|
jest.useFakeTimers();
|
||||||
jest.setSystemTime(0);
|
jest.setSystemTime(0);
|
||||||
|
|
||||||
|
useMockMediaDevices();
|
||||||
|
useMockedCalls();
|
||||||
|
jest.spyOn(HTMLMediaElement.prototype, "play").mockImplementation(async () => {});
|
||||||
|
|
||||||
stubClient();
|
stubClient();
|
||||||
client = mocked(MatrixClientPeg.safeGet());
|
client = mocked(MatrixClientPeg.safeGet());
|
||||||
client.getUserId.mockReturnValue("@alice:example.org");
|
client.getUserId.mockReturnValue("@alice:example.org");
|
||||||
|
|
|
@ -38,6 +38,7 @@ import {
|
||||||
filterConsole,
|
filterConsole,
|
||||||
flushPromises,
|
flushPromises,
|
||||||
mkMessage,
|
mkMessage,
|
||||||
|
useMockMediaDevices,
|
||||||
} from "../../../test-utils";
|
} from "../../../test-utils";
|
||||||
import { CallStore } from "../../../../src/stores/CallStore";
|
import { CallStore } from "../../../../src/stores/CallStore";
|
||||||
import RoomTile from "../../../../src/components/views/rooms/RoomTile";
|
import RoomTile from "../../../../src/components/views/rooms/RoomTile";
|
||||||
|
@ -134,6 +135,7 @@ describe("RoomTile", () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
useMockMediaDevices();
|
||||||
sdkContext = new TestSdkContext();
|
sdkContext = new TestSdkContext();
|
||||||
|
|
||||||
client = mocked(stubClient());
|
client = mocked(stubClient());
|
||||||
|
|
|
@ -30,6 +30,7 @@ import {
|
||||||
useMockedCalls,
|
useMockedCalls,
|
||||||
MockedCall,
|
MockedCall,
|
||||||
setupAsyncStoreWithClient,
|
setupAsyncStoreWithClient,
|
||||||
|
useMockMediaDevices,
|
||||||
} from "../../../test-utils";
|
} from "../../../test-utils";
|
||||||
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
||||||
import { CallView as _CallView } from "../../../../src/components/views/voip/CallView";
|
import { CallView as _CallView } from "../../../../src/components/views/voip/CallView";
|
||||||
|
@ -41,7 +42,7 @@ import MediaDeviceHandler from "../../../../src/MediaDeviceHandler";
|
||||||
|
|
||||||
const CallView = wrapInMatrixClientContext(_CallView);
|
const CallView = wrapInMatrixClientContext(_CallView);
|
||||||
|
|
||||||
describe("CallLobby", () => {
|
describe("CallView", () => {
|
||||||
useMockedCalls();
|
useMockedCalls();
|
||||||
jest.spyOn(HTMLMediaElement.prototype, "play").mockImplementation(async () => {});
|
jest.spyOn(HTMLMediaElement.prototype, "play").mockImplementation(async () => {});
|
||||||
|
|
||||||
|
@ -50,7 +51,7 @@ describe("CallLobby", () => {
|
||||||
let alice: RoomMember;
|
let alice: RoomMember;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
mocked(navigator.mediaDevices.enumerateDevices).mockResolvedValue([]);
|
useMockMediaDevices();
|
||||||
|
|
||||||
stubClient();
|
stubClient();
|
||||||
client = mocked(MatrixClientPeg.safeGet());
|
client = mocked(MatrixClientPeg.safeGet());
|
||||||
|
|
|
@ -91,11 +91,3 @@ window.fetch = fetchMock.sandbox();
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
window.Response = Response;
|
window.Response = Response;
|
||||||
|
|
||||||
// set up mediaDevices mock
|
|
||||||
Object.defineProperty(navigator, "mediaDevices", {
|
|
||||||
value: {
|
|
||||||
enumerateDevices: jest.fn().mockResolvedValue([]),
|
|
||||||
getUserMedia: jest.fn(),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
|
@ -20,7 +20,13 @@ import { Widget } from "matrix-widget-api";
|
||||||
|
|
||||||
import type { MatrixClient } from "matrix-js-sdk/src/matrix";
|
import type { MatrixClient } from "matrix-js-sdk/src/matrix";
|
||||||
import type { ClientWidgetApi } from "matrix-widget-api";
|
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 { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
||||||
import DMRoomMap from "../../../../src/utils/DMRoomMap";
|
import DMRoomMap from "../../../../src/utils/DMRoomMap";
|
||||||
import { DefaultTagID } from "../../../../src/stores/room-list/models";
|
import { DefaultTagID } from "../../../../src/stores/room-list/models";
|
||||||
|
@ -37,6 +43,7 @@ describe("Algorithm", () => {
|
||||||
let algorithm: Algorithm;
|
let algorithm: Algorithm;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
useMockMediaDevices();
|
||||||
stubClient();
|
stubClient();
|
||||||
client = mocked(MatrixClientPeg.safeGet());
|
client = mocked(MatrixClientPeg.safeGet());
|
||||||
DMRoomMap.makeShared(client);
|
DMRoomMap.makeShared(client);
|
||||||
|
|
|
@ -225,3 +225,12 @@ export const clearAllModals = async (): Promise<void> => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** Install a stub object at `navigator.mediaDevices` */
|
||||||
|
export function useMockMediaDevices(): void {
|
||||||
|
// @ts-ignore assignment of a thing that isn't a `MediaDevices` to read-only property
|
||||||
|
navigator["mediaDevices"] = {
|
||||||
|
enumerateDevices: jest.fn().mockResolvedValue([]),
|
||||||
|
getUserMedia: jest.fn(),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
|
@ -19,7 +19,7 @@ import { logger } from "matrix-js-sdk/src/logger";
|
||||||
import { screen } from "@testing-library/react";
|
import { screen } from "@testing-library/react";
|
||||||
|
|
||||||
import { requestMediaPermissions } from "../../../src/utils/media/requestMediaPermissions";
|
import { requestMediaPermissions } from "../../../src/utils/media/requestMediaPermissions";
|
||||||
import { flushPromises } from "../../test-utils";
|
import { flushPromises, useMockMediaDevices } from "../../test-utils";
|
||||||
|
|
||||||
describe("requestMediaPermissions", () => {
|
describe("requestMediaPermissions", () => {
|
||||||
let error: Error;
|
let error: Error;
|
||||||
|
@ -34,6 +34,7 @@ describe("requestMediaPermissions", () => {
|
||||||
};
|
};
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
useMockMediaDevices();
|
||||||
error = new Error();
|
error = new Error();
|
||||||
jest.spyOn(logger, "log");
|
jest.spyOn(logger, "log");
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue