Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Tweak filterConsole
Browse files Browse the repository at this point in the history
  • Loading branch information
weeman1337 committed Jan 3, 2023
1 parent d6aa67c commit 87b1fad
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 27 deletions.
16 changes: 7 additions & 9 deletions test/components/structures/auth/ForgotPassword-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ describe("<ForgotPassword>", () => {
let onComplete: () => void;
let onLoginClick: () => void;
let renderResult: RenderResult;
let restoreConsole: () => void;

const typeIntoField = async (label: string, value: string): Promise<void> => {
await act(async () => {
Expand All @@ -63,14 +62,14 @@ describe("<ForgotPassword>", () => {
});
};

beforeEach(() => {
restoreConsole = filterConsole(
// not implemented by js-dom /~https://github.com/jsdom/jsdom/issues/1937
"Not implemented: HTMLFormElement.prototype.requestSubmit",
// not of interested for this test
"Starting load of AsyncWrapper for modal",
);
filterConsole(
// not implemented by js-dom /~https://github.com/jsdom/jsdom/issues/1937
"Not implemented: HTMLFormElement.prototype.requestSubmit",
// not of interested for this test
"Starting load of AsyncWrapper for modal",
);

beforeEach(() => {
client = stubClient();
mocked(createClient).mockReturnValue(client);

Expand All @@ -87,7 +86,6 @@ describe("<ForgotPassword>", () => {
afterEach(() => {
// clean up modals
Modal.closeCurrentModal("force");
restoreConsole?.();
});

beforeAll(() => {
Expand Down
7 changes: 6 additions & 1 deletion test/components/views/avatars/RoomAvatar-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { MatrixClient, Room } from "matrix-js-sdk/src/matrix";
import { mocked } from "jest-mock";

import RoomAvatar from "../../../../src/components/views/avatars/RoomAvatar";
import { stubClient } from "../../../test-utils";
import { filterConsole, stubClient } from "../../../test-utils";
import DMRoomMap from "../../../../src/utils/DMRoomMap";
import { LocalRoom } from "../../../../src/models/LocalRoom";
import * as AvatarModule from "../../../../src/Avatar";
Expand All @@ -29,6 +29,11 @@ import { DirectoryMember } from "../../../../src/utils/direct-messages";
describe("RoomAvatar", () => {
let client: MatrixClient;

filterConsole(
// unrelated for this test
"Room !room:example.com does not have an m.room.create event",
);

beforeAll(() => {
client = stubClient();
const dmRoomMap = new DMRoomMap(client);
Expand Down
12 changes: 5 additions & 7 deletions test/components/views/rooms/RoomTile-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,19 @@ describe("RoomTile", () => {
};

let client: Mocked<MatrixClient>;
let restoreConsole: () => void;
let voiceBroadcastInfoEvent: MatrixEvent;
let room: Room;
let renderResult: RenderResult;
let sdkContext: TestSdkContext;

filterConsole(
// irrelevant for this test
"Room !1:example.org does not have an m.room.create event",
);

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

restoreConsole = filterConsole(
// irrelevant for this test
"Room !1:example.org does not have an m.room.create event",
);

client = mocked(stubClient());
sdkContext.client = client;
DMRoomMap.makeShared();
Expand All @@ -105,7 +104,6 @@ describe("RoomTile", () => {
});

afterEach(() => {
restoreConsole();
jest.clearAllMocks();
});

Expand Down
8 changes: 4 additions & 4 deletions test/test-utils/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ const originalFunctions: FilteredConsole = {

/**
* Allows to filter out specific messages in console.*.
* Automagically restores the original function by implementing an afterAll hook.
*
* @param ignoreList Messages to be filtered
* @returns function to restore the console
*/
export const filterConsole = (...ignoreList: string[]): (() => void) => {
export const filterConsole = (...ignoreList: string[]): void => {
for (const [key, originalFunction] of Object.entries(originalFunctions)) {
window.console[key as keyof FilteredConsole] = (...data: any[]) => {
const message = data?.[0]?.message || data?.[0];
Expand All @@ -43,9 +43,9 @@ export const filterConsole = (...ignoreList: string[]): (() => void) => {
};
}

return () => {
afterAll(() => {
for (const [key, originalFunction] of Object.entries(originalFunctions)) {
window.console[key as keyof FilteredConsole] = originalFunction;
}
};
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ describe("VoiceBroadcastRecordingPip", () => {
let infoEvent: MatrixEvent;
let recording: VoiceBroadcastRecording;
let renderResult: RenderResult;
let restoreConsole: () => void;

const renderPip = async (state: VoiceBroadcastInfoState) => {
infoEvent = mkVoiceBroadcastInfoStateEvent(roomId, state, client.getUserId() || "", client.getDeviceId() || "");
Expand All @@ -85,6 +84,8 @@ describe("VoiceBroadcastRecordingPip", () => {
});
};

filterConsole("Starting load of AsyncWrapper for modal");

beforeAll(() => {
client = stubClient();
mocked(requestMediaPermissions).mockResolvedValue({
Expand All @@ -105,11 +106,6 @@ describe("VoiceBroadcastRecordingPip", () => {
[MediaDeviceKindEnum.VideoInput]: [],
});
jest.spyOn(MediaDeviceHandler.instance, "setDevice").mockImplementation();
restoreConsole = filterConsole("Starting load of AsyncWrapper for modal");
});

afterAll(() => {
restoreConsole();
});

describe("when rendering a started recording", () => {
Expand Down

0 comments on commit 87b1fad

Please sign in to comment.