Change module API to be an instance getter (#31025)

* Change module API to be an instance getter

Helps with circular dependencies by not instantating the module API
on the initial evaluation of the files.

* Add basic test

* add another test
This commit is contained in:
David Baker
2025-10-15 10:20:48 +01:00
committed by GitHub
parent 6cfe197a38
commit 146e4772ac
8 changed files with 82 additions and 23 deletions

View File

@@ -16,7 +16,7 @@ import { MatrixClientPeg } from "../../../../../src/MatrixClientPeg";
import DMRoomMap from "../../../../../src/utils/DMRoomMap";
import RoomPreviewBar from "../../../../../src/components/views/rooms/RoomPreviewBar";
import defaultDispatcher from "../../../../../src/dispatcher/dispatcher";
import ModuleApi from "../../../../../src/modules/Api.ts";
import { ModuleApi } from "../../../../../src/modules/Api.ts";
jest.mock("../../../../../src/IdentityAuthClient", () => {
return jest.fn().mockImplementation(() => {
@@ -500,7 +500,7 @@ describe("<RoomPreviewBar />", () => {
});
it("should render Module roomPreviewBarRenderer if specified", () => {
jest.spyOn(ModuleApi.customComponents, "roomPreviewBarRenderer", "get").mockReturnValue(() => (
jest.spyOn(ModuleApi.instance.customComponents, "roomPreviewBarRenderer", "get").mockReturnValue(() => (
<>Test component</>
));
const { getByText } = render(<RoomPreviewBar />);

View File

@@ -13,10 +13,13 @@ import {
JSONEventFactory,
MessageEventFactory,
pickFactory,
renderTile,
RoomCreateEventFactory,
} from "../../../src/events/EventTileFactory";
import SettingsStore from "../../../src/settings/SettingsStore";
import { createTestClient, mkEvent } from "../../test-utils";
import { TimelineRenderingType } from "../../../src/contexts/RoomContext";
import { ModuleApi } from "../../../src/modules/Api";
const roomId = "!room:example.com";
@@ -205,3 +208,54 @@ describe("pickFactory", () => {
});
});
});
describe("renderTile", () => {
let client: MatrixClient;
beforeEach(() => {
client = createTestClient();
});
it("rendering a tile defers to the module API", () => {
ModuleApi.instance.customComponents.renderMessage = jest.fn();
const messageEvent = mkEvent({
event: true,
type: EventType.RoomMessage,
user: client.getUserId()!,
room: roomId,
content: {
msgtype: MsgType.Text,
},
});
renderTile(TimelineRenderingType.Room, { mxEvent: messageEvent, showHiddenEvents: false }, client);
expect(ModuleApi.instance.customComponents.renderMessage).toHaveBeenCalledWith(
{
mxEvent: messageEvent,
},
expect.any(Function),
);
});
it("rendering a tile for a message of unknown type defers to the module API", () => {
ModuleApi.instance.customComponents.renderMessage = jest.fn();
const messageEvent = mkEvent({
event: true,
type: "weird.type",
user: client.getUserId()!,
room: roomId,
content: {
msgtype: MsgType.Text,
},
});
renderTile(TimelineRenderingType.Room, { mxEvent: messageEvent, showHiddenEvents: false }, client);
expect(ModuleApi.instance.customComponents.renderMessage).toHaveBeenCalledWith({
mxEvent: messageEvent,
});
});
});