Update MatrixChat-test to restoreAllMocks after each test (#11424)

* Update MatrixChat to  `restoreAllMocks` after each test

This helps keep tests independent.

* more MatrixChat fixes
This commit is contained in:
Richard van der Hoff 2023-08-17 20:06:45 +01:00 committed by GitHub
parent 5aefd4652a
commit 4a5b686aaa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 8 deletions

View file

@ -100,6 +100,7 @@ export class OwnProfileStore extends AsyncStoreWithClient<IState> {
}
protected async onNotReady(): Promise<void> {
this.onProfileUpdate.cancel();
if (this.monitoredUser) {
this.monitoredUser.removeListener(UserEvent.DisplayName, this.onProfileUpdate);
this.monitoredUser.removeListener(UserEvent.AvatarUrl, this.onProfileUpdate);

View file

@ -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("<MatrixChat />", () => {
logout: jest.fn(),
getDeviceId: jest.fn(),
});
let mockClient = getMockClientWithEventEmitter(getMockClientMethods());
let mockClient: Mocked<MatrixClient>;
const serverConfig = {
hsUrl: "https://test.com",
hsName: "Test Server",
@ -185,9 +185,12 @@ describe("<MatrixChat />", () => {
});
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("<MatrixChat />", () => {
describe("login via key/pass", () => {
let loginClient!: ReturnType<typeof getMockClientWithEventEmitter>;
const mockCrypto = {
getVerificationRequestsToDeviceInProgress: jest.fn().mockReturnValue([]),
getUserDeviceInfo: jest.fn().mockResolvedValue(new Map()),
};
const userName = "ernie";
const password = "ilovebert";
@ -578,6 +576,7 @@ describe("<MatrixChat />", () => {
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("<MatrixChat />", () => {
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);