MatrixChat test robustness fixes (#30413)

* MatrixChat-test: clean up better in `afterEach`

Make the MatrixChat tests behave better by letting them finish their work in an
`act` in afterEach. Otherwise we can end up mounting new components during
cleanup, which run tasks in the background

* MatrixChat-test: clean up dispatcher test

This test was kicking off a dispatcher job which would then open a
UserDeviceSettings dialog once the test had finished. That would then throw
exceptions because some of the mock environment had been torn down.

We're just testing that it opens the right dialog, so better to intercept
`createDialog`.

Aso add an `act` to reduce warnings, and replace a `flushPromises` with a
`waitFor` to make the test more robust.
This commit is contained in:
Richard van der Hoff 2025-07-28 17:32:53 +01:00 committed by GitHub
parent beaabd5b44
commit 7e40e3697f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -71,6 +71,8 @@ import { SetupEncryptionStore } from "../../../../src/stores/SetupEncryptionStor
import { ShareFormat } from "../../../../src/dispatcher/payloads/SharePayload.ts";
import { clearStorage } from "../../../../src/Lifecycle";
import RoomListStore from "../../../../src/stores/room-list/RoomListStore.ts";
import UserSettingsDialog from "../../../../src/components/views/dialogs/UserSettingsDialog.tsx";
import { SdkContextClass } from "../../../../src/contexts/SDKContext.ts";
jest.mock("matrix-js-sdk/src/oidc/authorize", () => ({
completeAuthorizationCodeGrant: jest.fn(),
@ -268,6 +270,10 @@ describe("<MatrixChat />", () => {
// (must be sync otherwise the next test will start before it happens)
act(() => defaultDispatcher.dispatch({ action: Action.OnLoggedOut }, true));
// that will cause the Login to kick off an update in the background, which we need to allow to finish within
// an `act` to avoid warnings
await flushPromises();
localStorage.clear();
});
@ -640,22 +646,29 @@ describe("<MatrixChat />", () => {
});
describe("onAction()", () => {
beforeEach(() => {
jest.spyOn(defaultDispatcher, "dispatch").mockClear();
jest.spyOn(defaultDispatcher, "fire").mockClear();
afterEach(() => {
jest.restoreAllMocks();
});
it("should open user device settings", async () => {
it("ViewUserDeviceSettings should open user device settings", async () => {
await getComponentAndWaitForReady();
const createDialog = jest.spyOn(Modal, "createDialog").mockReturnValue({} as any);
await act(async () => {
defaultDispatcher.dispatch({
action: Action.ViewUserDeviceSettings,
});
await flushPromises();
expect(defaultDispatcher.dispatch).toHaveBeenCalledWith({
action: Action.ViewUserSettings,
initialTabId: UserTab.SessionManager,
await waitFor(() =>
expect(createDialog).toHaveBeenCalledWith(
UserSettingsDialog,
{ initialTabId: UserTab.SessionManager, sdkContext: expect.any(SdkContextClass) },
/*className=*/ undefined,
/*isPriority=*/ false,
/*isStatic=*/ true,
),
);
});
});
@ -674,10 +687,6 @@ describe("<MatrixChat />", () => {
jest.spyOn(ReleaseAnnouncementStore.instance, "getReleaseAnnouncement").mockReturnValue(null);
});
afterEach(() => {
jest.restoreAllMocks();
});
describe("forget_room", () => {
it("should dispatch after_forget_room action on successful forget", async () => {
await clearAllModals();