Handle SDKContextClass client initialisation internally (#34146)
* Handle SDKContextClass `client` initialisation internally Rather than via MatrixChat - this is predominantly for Lifecycle tests as they don't use a MatrixChat and it doesn't make much sense for this component to own this state. * Fix tests
This commit is contained in:
@@ -33,6 +33,7 @@ import { type IConfigOptions } from "../../../../../src/IConfigOptions";
|
||||
import { SDKContextClass } from "../../../../../src/contexts/SDKContextClass";
|
||||
import { type IProfileInfo } from "../../../../../src/hooks/useProfileInfo";
|
||||
import { DirectoryMember, startDmOnFirstMessage } from "../../../../../src/utils/direct-messages";
|
||||
import { TestSDKContext } from "../../../TestSDKContext.ts";
|
||||
|
||||
const mockGetAccessToken = jest.fn().mockResolvedValue("getAccessToken");
|
||||
jest.mock("../../../../../src/IdentityAuthClient", () =>
|
||||
@@ -94,6 +95,7 @@ const bobProfileInfo: IProfileInfo = {
|
||||
describe("InviteDialog", () => {
|
||||
let mockClient: Mocked<MatrixClient>;
|
||||
let room: Room;
|
||||
let sdkContext: TestSDKContext;
|
||||
|
||||
filterConsole(
|
||||
"Error retrieving profile for userId @carol:example.com",
|
||||
@@ -178,13 +180,15 @@ describe("InviteDialog", () => {
|
||||
mockClient.getRooms.mockReturnValue([room]);
|
||||
mockClient.getRoom.mockReturnValue(room);
|
||||
|
||||
SDKContextClass.instance.client = mockClient;
|
||||
sdkContext = new TestSDKContext();
|
||||
// @ts-ignore UserMenuViewModel uses SDKContext in the constructor
|
||||
SDKContextClass.instance = sdkContext;
|
||||
sdkContext._client = mockClient;
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await clearAllModals();
|
||||
SDKContextClass.instance.onLoggedOut();
|
||||
SDKContextClass.instance.client = undefined;
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
|
||||
@@ -24,7 +24,7 @@ import MatrixClientContext from "../../../../../src/contexts/MatrixClientContext
|
||||
import SettingsStore from "../../../../../src/settings/SettingsStore";
|
||||
import { UIFeature } from "../../../../../src/settings/UIFeature";
|
||||
import DMRoomMap from "../../../../../src/utils/DMRoomMap";
|
||||
import { SDKContextClass } from "../../../../../src/contexts/SDKContextClass";
|
||||
import { TestSDKContext } from "../../../TestSDKContext.ts";
|
||||
|
||||
describe("<RoomSettingsDialog />", () => {
|
||||
const userId = "@alice:server.org";
|
||||
@@ -44,7 +44,7 @@ describe("<RoomSettingsDialog />", () => {
|
||||
const room2 = new Room("!room2:server.org", mockClient, userId);
|
||||
room2.name = "Another Room";
|
||||
|
||||
let sdkContext: SDKContextClass;
|
||||
let sdkContext: TestSDKContext;
|
||||
|
||||
jest.spyOn(SettingsStore, "getValue");
|
||||
|
||||
@@ -57,8 +57,8 @@ describe("<RoomSettingsDialog />", () => {
|
||||
return null;
|
||||
});
|
||||
|
||||
sdkContext = new SDKContextClass();
|
||||
sdkContext.client = mockClient;
|
||||
sdkContext = new TestSDKContext();
|
||||
sdkContext._client = mockClient;
|
||||
|
||||
jest.spyOn(SettingsStore, "getValue").mockReset().mockReturnValue(false);
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@ import {
|
||||
} from "../../../../test-utils";
|
||||
import { UIFeature } from "../../../../../src/settings/UIFeature";
|
||||
import { SettingLevel } from "../../../../../src/settings/SettingLevel";
|
||||
import { SDKContextClass } from "../../../../../src/contexts/SDKContextClass";
|
||||
import { TestSDKContext } from "../../../TestSDKContext.ts";
|
||||
import { type FeatureSettingKey } from "../../../../../src/settings/Settings.tsx";
|
||||
import { mockOpenIdConfiguration } from "../../../../test-utils/oidc.ts";
|
||||
|
||||
@@ -57,7 +57,7 @@ describe("<UserSettingsDialog />", () => {
|
||||
const mockSettingsStore = mocked(SettingsStore);
|
||||
let mockClient!: MockedObject<MatrixClient>;
|
||||
|
||||
let sdkContext: SDKContextClass;
|
||||
let sdkContext: TestSDKContext;
|
||||
const defaultProps = { onFinished: jest.fn() };
|
||||
const getComponent = (
|
||||
props: Partial<typeof defaultProps & { initialTabId?: UserTab; props: Record<string, any> }> = {},
|
||||
@@ -76,8 +76,8 @@ describe("<UserSettingsDialog />", () => {
|
||||
getMediaConfig: jest.fn(),
|
||||
getAuthMetadata: jest.fn().mockResolvedValue(mockOpenIdConfiguration()),
|
||||
});
|
||||
sdkContext = new SDKContextClass();
|
||||
sdkContext.client = mockClient;
|
||||
sdkContext = new TestSDKContext();
|
||||
sdkContext._client = mockClient;
|
||||
mockSettingsStore.getValue.mockReturnValue(false);
|
||||
mockSettingsStore.getValueAt.mockReturnValue(false);
|
||||
mockSettingsStore.getFeatureSettingNames.mockReturnValue([]);
|
||||
|
||||
@@ -48,11 +48,12 @@ describe("<Pill>", () => {
|
||||
const user3Id = "@user3:example.com";
|
||||
let renderResult: RenderResult;
|
||||
let pillParentClickHandler: (e: ButtonEvent) => void;
|
||||
let sdkContext: TestSDKContext;
|
||||
|
||||
const renderPill = (props: PillProps): void => {
|
||||
const cli = MatrixClientPeg.safeGet();
|
||||
const mockSdkContext = new TestSDKContext();
|
||||
mockSdkContext.client = cli;
|
||||
mockSdkContext._client = cli;
|
||||
|
||||
const withDefault = {
|
||||
inMessage: true,
|
||||
@@ -79,7 +80,10 @@ describe("<Pill>", () => {
|
||||
|
||||
beforeEach(() => {
|
||||
client = mocked(stubClient());
|
||||
SDKContextClass.instance.client = client;
|
||||
sdkContext = new TestSDKContext();
|
||||
// @ts-ignore Pill uses the SDKContext global
|
||||
SDKContextClass.instance = sdkContext;
|
||||
sdkContext._client = client;
|
||||
DMRoomMap.makeShared(client);
|
||||
room1 = new Room(room1Id, client, user1Id);
|
||||
room1.name = "Room 1";
|
||||
|
||||
+8
-8
@@ -12,7 +12,7 @@ import { type EventTimeline, JoinRule, Room } from "matrix-js-sdk/src/matrix";
|
||||
import { KnownMembership } from "matrix-js-sdk/src/types";
|
||||
|
||||
import { SDKContext } from "../../../../../../src/contexts/SDKContext";
|
||||
import { SDKContextClass } from "../../../../../../src/contexts/SDKContextClass";
|
||||
import { TestSDKContext } from "../../../../TestSDKContext.ts";
|
||||
import { getMockClientWithEventEmitter, mockClientMethodsUser } from "../../../../../test-utils";
|
||||
import {
|
||||
CallGuestLinkButton,
|
||||
@@ -26,7 +26,7 @@ import SettingsStore from "../../../../../../src/settings/SettingsStore";
|
||||
|
||||
describe("<CallGuestLinkButton />", () => {
|
||||
const roomId = "!room:server.org";
|
||||
let sdkContext!: SDKContextClass;
|
||||
let sdkContext!: TestSDKContext;
|
||||
let modalSpy: jest.SpyInstance;
|
||||
let modalResolve: (value: unknown[] | PromiseLike<unknown[]>) => void;
|
||||
let room: Room;
|
||||
@@ -78,8 +78,8 @@ describe("<CallGuestLinkButton />", () => {
|
||||
...mockClientMethodsUser(),
|
||||
sendStateEvent: jest.fn(),
|
||||
});
|
||||
sdkContext = new SDKContextClass();
|
||||
sdkContext.client = client;
|
||||
sdkContext = new TestSDKContext();
|
||||
sdkContext._client = client;
|
||||
const modalPromise = new Promise<unknown[]>((resolve) => {
|
||||
modalResolve = resolve;
|
||||
});
|
||||
@@ -94,7 +94,7 @@ describe("<CallGuestLinkButton />", () => {
|
||||
return oldGet(key);
|
||||
});
|
||||
jest.spyOn(room, "hasEncryptionStateEvent").mockReturnValue(true);
|
||||
jest.spyOn(SDKContextClass.instance.roomViewStore, "isViewingCall").mockReturnValue(true);
|
||||
jest.spyOn(sdkContext.roomViewStore, "isViewingCall").mockReturnValue(true);
|
||||
});
|
||||
afterEach(() => {
|
||||
jest.restoreAllMocks();
|
||||
@@ -152,7 +152,7 @@ describe("<CallGuestLinkButton />", () => {
|
||||
|
||||
it("don't show external conference button if now guest spa link is configured", () => {
|
||||
jest.spyOn(room, "getJoinRule").mockReturnValue(JoinRule.Public);
|
||||
jest.spyOn(SDKContextClass.instance.roomViewStore, "isViewingCall").mockReturnValue(true);
|
||||
jest.spyOn(sdkContext.roomViewStore, "isViewingCall").mockReturnValue(true);
|
||||
|
||||
jest.spyOn(SdkConfig, "get").mockImplementation((key) => {
|
||||
if (key === "element_call") {
|
||||
@@ -179,7 +179,7 @@ describe("<CallGuestLinkButton />", () => {
|
||||
|
||||
it("opens the share dialog with the correct share link in an encrypted room", () => {
|
||||
jest.spyOn(room, "getJoinRule").mockReturnValue(JoinRule.Public);
|
||||
jest.spyOn(SDKContextClass.instance.roomViewStore, "isViewingCall").mockReturnValue(true);
|
||||
jest.spyOn(sdkContext.roomViewStore, "isViewingCall").mockReturnValue(true);
|
||||
|
||||
getComponent(room);
|
||||
const modalSpy = jest.spyOn(Modal, "createDialog");
|
||||
@@ -201,7 +201,7 @@ describe("<CallGuestLinkButton />", () => {
|
||||
it("share dialog has correct link in an unencrypted room", () => {
|
||||
jest.spyOn(room, "getJoinRule").mockReturnValue(JoinRule.Public);
|
||||
jest.spyOn(room, "hasEncryptionStateEvent").mockReturnValue(false);
|
||||
jest.spyOn(SDKContextClass.instance.roomViewStore, "isViewingCall").mockReturnValue(true);
|
||||
jest.spyOn(sdkContext.roomViewStore, "isViewingCall").mockReturnValue(true);
|
||||
|
||||
getComponent(room);
|
||||
const modalSpy = jest.spyOn(Modal, "createDialog");
|
||||
|
||||
+4
-4
@@ -13,7 +13,7 @@ import { fireEvent, render, screen, waitFor } from "jest-matrix-react";
|
||||
|
||||
import { VideoRoomChatButton } from "../../../../../../src/components/views/rooms/RoomHeader/VideoRoomChatButton";
|
||||
import { SDKContext } from "../../../../../../src/contexts/SDKContext";
|
||||
import { SDKContextClass } from "../../../../../../src/contexts/SDKContextClass";
|
||||
import { TestSDKContext } from "../../../../TestSDKContext.ts";
|
||||
import type RightPanelStore from "../../../../../../src/stores/right-panel/RightPanelStore";
|
||||
import { getMockClientWithEventEmitter, mockClientMethodsUser } from "../../../../../test-utils";
|
||||
import { RoomNotificationState } from "../../../../../../src/stores/notifications/RoomNotificationState";
|
||||
@@ -23,7 +23,7 @@ import { RightPanelPhases } from "../../../../../../src/stores/right-panel/Right
|
||||
|
||||
describe("<VideoRoomChatButton />", () => {
|
||||
const roomId = "!room:server.org";
|
||||
let sdkContext!: SDKContextClass;
|
||||
let sdkContext!: TestSDKContext;
|
||||
let rightPanelStore!: MockedObject<RightPanelStore>;
|
||||
|
||||
/**
|
||||
@@ -59,8 +59,8 @@ describe("<VideoRoomChatButton />", () => {
|
||||
rightPanelStore = {
|
||||
showOrHidePhase: jest.fn(),
|
||||
} as unknown as MockedObject<RightPanelStore>;
|
||||
sdkContext = new SDKContextClass();
|
||||
sdkContext.client = client;
|
||||
sdkContext = new TestSDKContext();
|
||||
sdkContext._client = client;
|
||||
jest.spyOn(sdkContext, "rightPanelStore", "get").mockReturnValue(rightPanelStore);
|
||||
});
|
||||
|
||||
|
||||
@@ -122,7 +122,7 @@ export async function renderMemberList(
|
||||
}
|
||||
|
||||
const context = new TestSDKContext();
|
||||
context.client = client;
|
||||
context._client = client;
|
||||
context.memberListStore.isPresenceEnabled = jest.fn().mockReturnValue(enablePresence);
|
||||
const root = render(
|
||||
<MatrixClientContext.Provider value={client}>
|
||||
|
||||
+4
-4
@@ -16,7 +16,7 @@ import { ToastContext, ToastRack } from "@element-hq/web-shared-components";
|
||||
|
||||
import AccountUserSettingsTab from "../../../../../../../src/components/views/settings/tabs/user/AccountUserSettingsTab";
|
||||
import { SDKContext } from "../../../../../../../src/contexts/SDKContext";
|
||||
import { SDKContextClass } from "../../../../../../../src/contexts/SDKContextClass";
|
||||
import { TestSDKContext } from "../../../../../TestSDKContext.ts";
|
||||
import SettingsStore from "../../../../../../../src/settings/SettingsStore";
|
||||
import {
|
||||
getMockClientWithEventEmitter,
|
||||
@@ -51,7 +51,7 @@ describe("<AccountUserSettingsTab />", () => {
|
||||
const userId = "@alice:server.org";
|
||||
let mockClient: MockedObject<MatrixClient>;
|
||||
|
||||
let stores: SDKContextClass;
|
||||
let stores: TestSDKContext;
|
||||
|
||||
const getComponent = () => (
|
||||
<MatrixClientContext.Provider value={mockClient}>
|
||||
@@ -87,8 +87,8 @@ describe("<AccountUserSettingsTab />", () => {
|
||||
id_server_unbind_result: "success",
|
||||
});
|
||||
|
||||
stores = new SDKContextClass();
|
||||
stores.client = mockClient;
|
||||
stores = new TestSDKContext();
|
||||
stores._client = mockClient;
|
||||
// stub out this store completely to avoid mocking initialisation
|
||||
const mockOidcClientStore = {} as unknown as OidcClientStore;
|
||||
jest.spyOn(stores, "oidcClientStore", "get").mockReturnValue(mockOidcClientStore);
|
||||
|
||||
+3
-3
@@ -20,7 +20,7 @@ import {
|
||||
mockPlatformPeg,
|
||||
} from "../../../../../../test-utils";
|
||||
import { SDKContext } from "../../../../../../../src/contexts/SDKContext";
|
||||
import { SDKContextClass } from "../../../../../../../src/contexts/SDKContextClass";
|
||||
import { TestSDKContext } from "../../../../../TestSDKContext.ts";
|
||||
import defaultDispatcher from "../../../../../../../src/dispatcher/dispatcher";
|
||||
import { UIFeature } from "../../../../../../../src/settings/UIFeature";
|
||||
import SettingsStore from "../../../../../../../src/settings/SettingsStore";
|
||||
@@ -42,8 +42,8 @@ describe("<SecurityUserSettingsTab />", () => {
|
||||
setIgnoredUsers,
|
||||
});
|
||||
|
||||
const sdkContext = new SDKContextClass();
|
||||
sdkContext.client = mockClient;
|
||||
const sdkContext = new TestSDKContext();
|
||||
sdkContext._client = mockClient;
|
||||
|
||||
const getComponent = () => (
|
||||
<MatrixClientContext.Provider value={mockClient}>
|
||||
|
||||
+4
-4
@@ -57,7 +57,7 @@ import { INACTIVE_DEVICE_AGE_MS } from "../../../../../../../src/components/view
|
||||
import SettingsStore from "../../../../../../../src/settings/SettingsStore";
|
||||
import { getClientInformationEventType } from "../../../../../../../src/utils/device/clientInformation";
|
||||
import { SDKContext } from "../../../../../../../src/contexts/SDKContext";
|
||||
import { SDKContextClass } from "../../../../../../../src/contexts/SDKContextClass";
|
||||
import { TestSDKContext } from "../../../../../TestSDKContext.ts";
|
||||
import { type OidcClientStore } from "../../../../../../../src/stores/oidc/OidcClientStore";
|
||||
import { makeDelegatedAuthConfig } from "../../../../../../test-utils/oidc";
|
||||
import MatrixClientContext from "../../../../../../../src/contexts/MatrixClientContext";
|
||||
@@ -134,7 +134,7 @@ describe("<SessionManagerTab />", () => {
|
||||
} as unknown as CryptoApi);
|
||||
|
||||
let mockClient!: MockedObject<MatrixClient>;
|
||||
let sdkContext: SDKContextClass;
|
||||
let sdkContext: TestSDKContext;
|
||||
|
||||
const defaultProps = {};
|
||||
const getComponent = (props = {}): React.ReactElement => (
|
||||
@@ -250,8 +250,8 @@ describe("<SessionManagerTab />", () => {
|
||||
}
|
||||
});
|
||||
|
||||
sdkContext = new SDKContextClass();
|
||||
sdkContext.client = mockClient;
|
||||
sdkContext = new TestSDKContext();
|
||||
sdkContext._client = mockClient;
|
||||
|
||||
// @ts-ignore allow delete of non-optional prop
|
||||
delete window.location;
|
||||
|
||||
@@ -17,7 +17,7 @@ import { MetaSpace, type SpaceKey } from "../../../../../src/stores/spaces";
|
||||
import { shouldShowComponent } from "../../../../../src/customisations/helpers/UIComponents";
|
||||
import { UIComponent } from "../../../../../src/settings/UIFeature";
|
||||
import { mkStubRoom, wrapInMatrixClientContext, wrapInSdkContext } from "../../../../test-utils";
|
||||
import { SDKContextClass } from "../../../../../src/contexts/SDKContextClass";
|
||||
import { TestSDKContext } from "../../../TestSDKContext.ts";
|
||||
import SpaceStore from "../../../../../src/stores/spaces/SpaceStore";
|
||||
import DMRoomMap from "../../../../../src/utils/DMRoomMap";
|
||||
import { type SpaceNotificationState } from "../../../../../src/stores/notifications/SpaceNotificationState";
|
||||
@@ -124,12 +124,13 @@ describe("<SpacePanel />", () => {
|
||||
isVersionSupported: jest.fn().mockResolvedValue(true),
|
||||
doesServerSupportUnstableFeature: jest.fn().mockResolvedValue(false),
|
||||
} as unknown as MatrixClient;
|
||||
const SpacePanel = wrapInSdkContext(wrapInMatrixClientContext(UnwrappedSpacePanel), SDKContextClass.instance);
|
||||
const sdkContext = new TestSDKContext();
|
||||
const SpacePanel = wrapInSdkContext(wrapInMatrixClientContext(UnwrappedSpacePanel), sdkContext);
|
||||
|
||||
beforeAll(() => {
|
||||
jest.spyOn(MatrixClientPeg, "get").mockReturnValue(mockClient);
|
||||
jest.spyOn(MatrixClientPeg, "safeGet").mockReturnValue(mockClient);
|
||||
SDKContextClass.instance.client = mockClient;
|
||||
sdkContext._client = mockClient;
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
|
||||
Reference in New Issue
Block a user