1
0
mirror of https://github.com/matrix-org/matrix-react-sdk.git synced 2025-08-07 21:23:00 +03:00

Add MatrixClientPeg::safeGet and use it in tests (#10985)

This commit is contained in:
Michael Telatynski
2023-06-05 18:12:23 +01:00
committed by GitHub
parent c47b587225
commit 6b46d6e4f8
88 changed files with 290 additions and 226 deletions

View File

@@ -71,6 +71,7 @@ export const getMockClientWithEventEmitter = (
const mock = mocked(new MockClientWithEventEmitter(mockProperties) as unknown as MatrixClient);
jest.spyOn(MatrixClientPeg, "get").mockReturnValue(mock);
jest.spyOn(MatrixClientPeg, "safeGet").mockReturnValue(mock);
// @ts-ignore simplified test stub
mock.canSupport = new Map();
@@ -80,7 +81,10 @@ export const getMockClientWithEventEmitter = (
return mock;
};
export const unmockClientPeg = () => jest.spyOn(MatrixClientPeg, "get").mockRestore();
export const unmockClientPeg = () => {
jest.spyOn(MatrixClientPeg, "get").mockRestore();
jest.spyOn(MatrixClientPeg, "safeGet").mockRestore();
};
/**
* Returns basic mocked client methods related to the current user

View File

@@ -69,13 +69,13 @@ export function stubClient(): MatrixClient {
// 'sandbox.restore()' doesn't work correctly on inherited methods,
// so we do this for each method
jest.spyOn(peg, "get");
jest.spyOn(peg, "safeGet");
jest.spyOn(peg, "unset");
jest.spyOn(peg, "replaceUsingCreds");
// MatrixClientPeg.get() is called a /lot/, so implement it with our own
// MatrixClientPeg.safeGet() is called a /lot/, so implement it with our own
// fast stub function rather than a sinon stub
peg.get = function () {
return client;
};
peg.get = () => client;
peg.safeGet = () => client;
MatrixClientBackedSettingsHandler.matrixClient = client;
return client;
}

View File

@@ -29,7 +29,7 @@ export function wrapInMatrixClientContext<T>(WrappedComponent: ComponentType<T>)
constructor(props: WrapperProps<T>) {
super(props);
this._matrixClient = peg.get();
this._matrixClient = peg.safeGet();
}
render() {