2022-03-21 12:42:58 +01:00
|
|
|
/*
|
2024-09-09 14:57:16 +01:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2022-03-21 12:42:58 +01:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
2025-01-06 11:18:54 +00:00
|
|
|
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-03-21 12:42:58 +01:00
|
|
|
*/
|
|
|
|
|
|
2025-02-05 13:25:06 +00:00
|
|
|
import { type MethodLikeKeys, mocked, type MockedObject } from "jest-mock";
|
2022-03-21 12:42:58 +01:00
|
|
|
|
|
|
|
|
import BasePlatform from "../../src/BasePlatform";
|
|
|
|
|
import PlatformPeg from "../../src/PlatformPeg";
|
2025-09-23 15:46:01 +01:00
|
|
|
import * as SessionLock from "../../src/utils/SessionLock";
|
2022-03-21 12:42:58 +01:00
|
|
|
|
|
|
|
|
// doesn't implement abstract
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
class MockPlatform extends BasePlatform {
|
2024-02-16 14:43:58 +00:00
|
|
|
constructor(platformMocks: Partial<Record<keyof BasePlatform, unknown>>) {
|
2022-03-21 12:42:58 +01:00
|
|
|
super();
|
|
|
|
|
Object.assign(this, platformMocks);
|
|
|
|
|
}
|
2025-09-23 15:46:01 +01:00
|
|
|
|
|
|
|
|
public checkSessionLockFree(): boolean {
|
|
|
|
|
return SessionLock.checkSessionLockFree();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async getSessionLock(onNewInstance: () => Promise<void>): Promise<boolean> {
|
|
|
|
|
return SessionLock.getSessionLock(onNewInstance);
|
|
|
|
|
}
|
2022-03-21 12:42:58 +01:00
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Mock Platform Peg
|
|
|
|
|
* Creates a mock BasePlatform class
|
2022-11-18 09:22:43 +00:00
|
|
|
* spies on PlatformPeg.get and returns mock platform
|
2022-03-21 12:42:58 +01:00
|
|
|
* @returns MockPlatform instance
|
|
|
|
|
*/
|
|
|
|
|
export const mockPlatformPeg = (
|
2022-11-04 10:48:08 +00:00
|
|
|
platformMocks: Partial<Record<MethodLikeKeys<BasePlatform>, unknown>> = {},
|
2022-03-21 12:42:58 +01:00
|
|
|
): MockedObject<BasePlatform> => {
|
|
|
|
|
const mockPlatform = new MockPlatform(platformMocks);
|
|
|
|
|
jest.spyOn(PlatformPeg, "get").mockReturnValue(mockPlatform);
|
|
|
|
|
return mocked(mockPlatform);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const unmockPlatformPeg = () => {
|
|
|
|
|
jest.spyOn(PlatformPeg, "get").mockRestore();
|
|
|
|
|
};
|