Update type and usage of window.matrixChat to be better React 18 friendly (#28415)

* Update type and usage of window.matrixChat to be better React 18 friendly

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

* Improve coverage

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

* Make modules import async to make the file testable

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>

---------

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
This commit is contained in:
Michael Telatynski 2024-11-13 14:16:29 +00:00 committed by GitHub
parent 349c9b0c26
commit ca239fee4d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 351 additions and 13 deletions

View file

@ -6,7 +6,8 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
Please see LICENSE files in the repository root for full details.
*/
import { getInitialScreenAfterLogin, onNewScreen } from "../../../src/vector/routing";
import { getInitialScreenAfterLogin, init, onNewScreen } from "../../../src/vector/routing";
import MatrixChat from "../../../src/components/structures/MatrixChat.tsx";
describe("onNewScreen", () => {
it("should replace history if stripping via fields", () => {
@ -95,3 +96,27 @@ describe("getInitialScreenAfterLogin", () => {
});
});
});
describe("init", () => {
afterAll(() => {
// @ts-ignore
delete window.matrixChat;
});
it("should call showScreen on MatrixChat on hashchange", () => {
Object.defineProperty(window, "location", {
value: {
hash: "#/room/!room:server?via=abc",
},
});
window.matrixChat = {
showScreen: jest.fn(),
} as unknown as MatrixChat;
init();
window.dispatchEvent(new HashChangeEvent("hashchange"));
expect(window.matrixChat.showScreen).toHaveBeenCalledWith("room/!room:server", { via: "abc" });
});
});