Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

103 lines
3.6 KiB
TypeScript
Raw Permalink Normal View History

2022-02-23 12:21:11 +01:00
/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
2022-02-23 12:21:11 +01:00
Copyright 2022 The Matrix.org Foundation C.I.C.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
2024-09-09 14:57:16 +01:00
Please see LICENSE files in the repository root for full details.
2022-02-23 12:21:11 +01:00
*/
2025-02-05 13:25:06 +00:00
import React, { type ComponentType, type Ref } from "react";
import { type MatrixClient } from "matrix-js-sdk/src/matrix";
import { type RenderOptions } from "jest-matrix-react";
2022-02-23 12:21:11 +01:00
import { MatrixClientPeg as peg } from "../../src/MatrixClientPeg";
import MatrixClientContext from "../../src/contexts/MatrixClientContext";
2026-07-01 13:49:18 +01:00
import { SDKContext } from "../../src/contexts/SDKContext";
import { type SDKContextClass } from "../../src/contexts/SDKContextClass";
import { type RoomContextType } from "../../src/contexts/RoomContext.ts";
import { ScopedRoomContextProvider } from "../../src/contexts/ScopedRoomContext.tsx";
2022-02-23 12:21:11 +01:00
type WrapperProps<T> = { wrappedRef?: Ref<ComponentType<T>> } & T;
2022-03-09 14:23:58 +01:00
export function wrapInMatrixClientContext<T>(WrappedComponent: ComponentType<T>): ComponentType<WrapperProps<T>> {
class Wrapper extends React.Component<WrapperProps<T>> {
2022-02-23 12:21:11 +01:00
_matrixClient: MatrixClient;
2023-02-13 11:39:16 +00:00
constructor(props: WrapperProps<T>) {
2022-02-23 12:21:11 +01:00
super(props);
this._matrixClient = peg.safeGet();
2022-02-23 12:21:11 +01:00
}
render() {
return (
<MatrixClientContext.Provider value={this._matrixClient}>
<WrappedComponent ref={this.props.wrappedRef} {...this.props} />
</MatrixClientContext.Provider>
);
}
}
return Wrapper;
2022-02-23 12:21:11 +01:00
}
export function wrapInSdkContext<T>(
WrappedComponent: ComponentType<T>,
2026-07-01 13:49:18 +01:00
sdkContext: SDKContextClass,
): ComponentType<WrapperProps<T>> {
return class extends React.Component<WrapperProps<T>> {
render() {
return (
<SDKContext.Provider value={sdkContext}>
<WrappedComponent {...this.props} />
</SDKContext.Provider>
);
}
};
}
/**
* Test helper to generate React testing library render options for wrapping with a MatrixClientContext.Provider
* @param client the MatrixClient instance to expose via the provider
*/
export function withClientContextRenderOptions(client: MatrixClient): RenderOptions {
return {
wrapper: ({ children }) => (
<MatrixClientContext.Provider value={client}>{children}</MatrixClientContext.Provider>
),
};
}
2026-07-01 13:49:18 +01:00
export function clientAndSDKContextRenderOptions(client: MatrixClient, sdkContext: SDKContextClass): RenderOptions {
return {
wrapper: ({ children }) => (
<SDKContext.Provider value={sdkContext}>
<MatrixClientContext.Provider value={client}>{children}</MatrixClientContext.Provider>
</SDKContext.Provider>
),
};
}
export function withContexts({
sdkContext,
matrixClient,
roomContext,
}: {
sdkContext?: SDKContextClass;
matrixClient?: MatrixClient;
roomContext?: RoomContextType;
}): RenderOptions {
return {
wrapper: ({ children }) => {
const client = matrixClient || sdkContext?.client;
if (client) {
children = <MatrixClientContext.Provider value={client}>{children}</MatrixClientContext.Provider>;
}
if (sdkContext) {
children = <SDKContext.Provider value={sdkContext}>{children}</SDKContext.Provider>;
}
if (roomContext) {
children = <ScopedRoomContextProvider {...roomContext}>{children}</ScopedRoomContextProvider>;
}
return children;
},
};
}