From 4a5b686aaa00ea77d5517a0ca6f7fac2e0f1adfa Mon Sep 17 00:00:00 2001 From: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> Date: Thu, 17 Aug 2023 20:06:45 +0100 Subject: [PATCH] Update MatrixChat-test to `restoreAllMocks` after each test (#11424) * Update MatrixChat to `restoreAllMocks` after each test This helps keep tests independent. * more MatrixChat fixes --- src/stores/OwnProfileStore.ts | 1 + .../components/structures/MatrixChat-test.tsx | 19 +++++++++++-------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/stores/OwnProfileStore.ts b/src/stores/OwnProfileStore.ts index 5226055994..5444ff9558 100644 --- a/src/stores/OwnProfileStore.ts +++ b/src/stores/OwnProfileStore.ts @@ -100,6 +100,7 @@ export class OwnProfileStore extends AsyncStoreWithClient { } protected async onNotReady(): Promise { + this.onProfileUpdate.cancel(); if (this.monitoredUser) { this.monitoredUser.removeListener(UserEvent.DisplayName, this.onProfileUpdate); this.monitoredUser.removeListener(UserEvent.AvatarUrl, this.onProfileUpdate); diff --git a/test/components/structures/MatrixChat-test.tsx b/test/components/structures/MatrixChat-test.tsx index eab417931f..233814019c 100644 --- a/test/components/structures/MatrixChat-test.tsx +++ b/test/components/structures/MatrixChat-test.tsx @@ -17,7 +17,7 @@ limitations under the License. import React, { ComponentProps } from "react"; import { fireEvent, render, RenderResult, screen, within } from "@testing-library/react"; import fetchMock from "fetch-mock-jest"; -import { mocked } from "jest-mock"; +import { Mocked, mocked } from "jest-mock"; import { ClientEvent, MatrixClient, MatrixEvent, Room, SyncState } from "matrix-js-sdk/src/matrix"; import { MediaHandler } from "matrix-js-sdk/src/webrtc/mediaHandler"; import * as MatrixJs from "matrix-js-sdk/src/matrix"; @@ -107,7 +107,7 @@ describe("", () => { logout: jest.fn(), getDeviceId: jest.fn(), }); - let mockClient = getMockClientWithEventEmitter(getMockClientMethods()); + let mockClient: Mocked; const serverConfig = { hsUrl: "https://test.com", hsName: "Test Server", @@ -185,9 +185,12 @@ describe("", () => { }); afterEach(() => { - jest.clearAllMocks(); + jest.restoreAllMocks(); localStorage.clear(); sessionStorage.clear(); + + // emit a loggedOut event so that all of the Store singletons forget about their references to the mock client + defaultDispatcher.dispatch({ action: Action.OnLoggedOut }); }); it("should render spinner while app is loading", () => { @@ -540,11 +543,6 @@ describe("", () => { describe("login via key/pass", () => { let loginClient!: ReturnType; - const mockCrypto = { - getVerificationRequestsToDeviceInProgress: jest.fn().mockReturnValue([]), - getUserDeviceInfo: jest.fn().mockResolvedValue(new Map()), - }; - const userName = "ernie"; const password = "ilovebert"; @@ -578,6 +576,7 @@ describe("", () => { beforeEach(() => { loginClient = getMockClientWithEventEmitter(getMockClientMethods()); // this is used to create a temporary client during login + // FIXME: except it is *also* used as the permanent client for the rest of the test. jest.spyOn(MatrixJs, "createClient").mockClear().mockReturnValue(loginClient); loginClient.login.mockClear().mockResolvedValue({ @@ -600,6 +599,10 @@ describe("", () => { describe("post login setup", () => { beforeEach(() => { + const mockCrypto = { + getVerificationRequestsToDeviceInProgress: jest.fn().mockReturnValue([]), + getUserDeviceInfo: jest.fn().mockResolvedValue(new Map()), + }; loginClient.isCryptoEnabled.mockReturnValue(true); loginClient.getCrypto.mockReturnValue(mockCrypto as any); loginClient.userHasCrossSigningKeys.mockClear().mockResolvedValue(false);