Skip to content

Commit

Permalink
Correctly fill window.matrixChat even when a Wrapper module is active
Browse files Browse the repository at this point in the history
Signed-off-by: Dominik Henneke <[email protected]>
  • Loading branch information
dhenneke committed Oct 18, 2023
1 parent 3930c6c commit 53bf443
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
3 changes: 2 additions & 1 deletion src/vector/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function onTokenLoginCompleted(): void {
window.history.replaceState(null, "", url.href);
}

export async function loadApp(fragParams: {}): Promise<ReactElement> {
export async function loadApp(fragParams: {}, matrixChatRef: React.Ref<MatrixChat>): Promise<ReactElement> {
initRouting();
const platform = PlatformPeg.get();

Expand Down Expand Up @@ -117,6 +117,7 @@ export async function loadApp(fragParams: {}): Promise<ReactElement> {
return (
<wrapperOpts.Wrapper>
<MatrixChat
ref={matrixChatRef}
onNewScreen={onNewScreen}
config={config}
realQueryParams={params}
Expand Down
6 changes: 5 additions & 1 deletion src/vector/init.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import SdkConfig from "matrix-react-sdk/src/SdkConfig";
import { setTheme } from "matrix-react-sdk/src/theme";
import { logger } from "matrix-js-sdk/src/logger";
import { ModuleRunner } from "matrix-react-sdk/src/modules/ModuleRunner";
import MatrixChat from "matrix-react-sdk/src/components/structures/MatrixChat";

import ElectronPlatform from "./platform/ElectronPlatform";
import PWAPlatform from "./platform/PWAPlatform";
Expand Down Expand Up @@ -147,7 +148,10 @@ export async function loadApp(fragParams: {}): Promise<void> {
/* webpackPreload: true */
"./app"
);
window.matrixChat = ReactDOM.render(await module.loadApp(fragParams), document.getElementById("matrixchat"));
function setWindowMatrixChat(matrixChat: MatrixChat): void {
window.matrixChat = matrixChat;
}
ReactDOM.render(await module.loadApp(fragParams, setWindowMatrixChat), document.getElementById("matrixchat"));
}

export async function showError(title: string, messages?: string[]): Promise<void> {
Expand Down
6 changes: 3 additions & 3 deletions test/app-tests/server-config-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ describe("Loading server config", function () {
},
},
});
await loadApp({});
await loadApp({}, null);
expect((SdkConfig.get("validated_server_config") || {}).hsUrl).toBe("https://matrix-client.matrix.org");
});

it("should use the default_server_name when resolveable", async function () {
SdkConfig.put({
default_server_name: "matrix.org",
});
await loadApp({});
await loadApp({}, null);
expect((SdkConfig.get("validated_server_config") || {}).hsUrl).toBe("https://matrix-client.matrix.org");
});

Expand All @@ -72,7 +72,7 @@ describe("Loading server config", function () {
},
},
});
await loadApp({});
await loadApp({}, null);
expect((SdkConfig.get("validated_server_config") || {}).hsUrl).toBe("https://matrix-client.matrix.org");
},
);
Expand Down
7 changes: 6 additions & 1 deletion test/app-tests/wrapper-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import fetchMock from "fetch-mock-jest";
import { render, RenderResult, screen } from "@testing-library/react";
import { ModuleRunner } from "matrix-react-sdk/src/modules/ModuleRunner";
import { WrapperLifecycle, WrapperOpts } from "@matrix-org/react-sdk-module-api/lib/lifecycles/WrapperLifecycle";
import MatrixChat from "matrix-react-sdk/src/components/structures/MatrixChat";

import WebPlatform from "../../src/vector/platform/WebPlatform";
import { loadApp } from "../../src/vector/app";
Expand Down Expand Up @@ -68,7 +69,8 @@ describe("Wrapper", () => {
}
});

const matrixChatResult: RenderResult = render(await loadApp({}));
const ref = React.createRef<MatrixChat>();
const matrixChatResult: RenderResult = render(await loadApp({}, ref));

// at this point, we're trying to do a guest registration;
// we expect a spinner
Expand All @@ -83,5 +85,8 @@ describe("Wrapper", () => {

expect(header.nextSibling).toBe(matrixChat);
expect(matrixChat.nextSibling).toBe(footer);

// Should still hold a reference to the MatrixChat component
expect(ref.current).toBeInstanceOf(MatrixChat);
});
});

0 comments on commit 53bf443

Please sign in to comment.