Take client creating functionality out of MatrixClientPeg (#34092)
* Take client creating functionality out of MatrixClientPeg Because all sorts of things import MatrixClientPeg and this means they pull in all manner of things related to creating a client, when all they need is to get the current one (and specifically it fixes the import cycle that means I can't add my test). * Remove fake indexeddb as it seems like the tests didn't actually see the fake indexeddb before (somehow) but now do, and are failing because they have that but no postmessage. It feels like the right solution is for these tests to not need indexeddb. * Don't mock a refresh token The tests don't mock out enough for the token refreshing setup to work, it wa somehow always ending up as null previously and now wasn't, at which point it broke, so just make it actually unset. * Move formatter instatiation to lazy rather than eagerly at parse time, as this apparently shifted one test to hit this import cycle instead. * Tests for room name generator
This commit is contained in:
@@ -70,7 +70,7 @@ export function stubClient(): MatrixClient {
|
||||
vi.spyOn(peg, "get");
|
||||
vi.spyOn(peg, "safeGet");
|
||||
vi.spyOn(peg, "unset");
|
||||
vi.spyOn(peg, "replaceUsingCreds");
|
||||
vi.spyOn(peg, "set");
|
||||
// MatrixClientPeg.safeGet() is called a /lot/, so implement it with our own
|
||||
// fast stub function rather than a sinon stub
|
||||
peg.get = () => client;
|
||||
|
||||
@@ -29,6 +29,7 @@ import { persistAccessTokenInStorage, persistRefreshTokenInStorage } from "../..
|
||||
import { encryptPickleKey } from "../../src/utils/tokens/pickling";
|
||||
import * as StorageManager from "../../src/utils/StorageManager.ts";
|
||||
import type BasePlatform from "../../src/BasePlatform.ts";
|
||||
import * as createMatrixClientModule from "../../src/utils/createMatrixClient";
|
||||
|
||||
const { logout, restoreSessionFromStorage, setLoggedIn } = Lifecycle;
|
||||
|
||||
@@ -71,9 +72,11 @@ describe("Lifecycle", () => {
|
||||
logout: jest.fn().mockResolvedValue(undefined),
|
||||
getAccessToken: jest.fn(),
|
||||
getRefreshToken: jest.fn(),
|
||||
setGuest: jest.fn(),
|
||||
setNotifTimelineSet: jest.fn(),
|
||||
});
|
||||
// stub this
|
||||
jest.spyOn(MatrixClientPeg, "replaceUsingCreds").mockImplementation(() => {});
|
||||
jest.spyOn(MatrixClientPeg, "set").mockImplementation(() => {});
|
||||
jest.spyOn(MatrixClientPeg, "start").mockResolvedValue(undefined);
|
||||
|
||||
// reset any mocking
|
||||
@@ -187,6 +190,7 @@ describe("Lifecycle", () => {
|
||||
jest.spyOn(logger, "log").mockClear();
|
||||
|
||||
jest.spyOn(MatrixJs, "createClient").mockReturnValue(mockClient);
|
||||
jest.spyOn(createMatrixClientModule, "createClientWithCreds").mockReturnValue(mockClient);
|
||||
|
||||
// stub this out
|
||||
jest.spyOn(Modal, "createDialog").mockReturnValue(
|
||||
@@ -235,7 +239,7 @@ describe("Lifecycle", () => {
|
||||
it("should restore guest accounts when ignoreGuest is false", async () => {
|
||||
expect(await restoreSessionFromStorage({ ignoreGuest: false })).toEqual(true);
|
||||
|
||||
expect(MatrixClientPeg.replaceUsingCreds).toHaveBeenCalledWith(
|
||||
expect(createMatrixClientModule.createClientWithCreds).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
userId,
|
||||
guest: true,
|
||||
@@ -279,7 +283,7 @@ describe("Lifecycle", () => {
|
||||
it("should create and start new matrix client with credentials", async () => {
|
||||
expect(await restoreSessionFromStorage()).toEqual(true);
|
||||
|
||||
expect(MatrixClientPeg.replaceUsingCreds).toHaveBeenCalledWith(
|
||||
expect(createMatrixClientModule.createClientWithCreds).toHaveBeenCalledWith(
|
||||
{
|
||||
userId,
|
||||
accessToken,
|
||||
@@ -328,7 +332,7 @@ describe("Lifecycle", () => {
|
||||
it("should create new matrix client with credentials", async () => {
|
||||
expect(await restoreSessionFromStorage()).toEqual(true);
|
||||
|
||||
expect(MatrixClientPeg.replaceUsingCreds).toHaveBeenCalledWith(
|
||||
expect(createMatrixClientModule.createClientWithCreds).toHaveBeenCalledWith(
|
||||
{
|
||||
userId,
|
||||
accessToken,
|
||||
@@ -410,7 +414,7 @@ describe("Lifecycle", () => {
|
||||
expect(await restoreSessionFromStorage()).toEqual(true);
|
||||
|
||||
// Ensure that the expected calls were made
|
||||
expect(MatrixClientPeg.replaceUsingCreds).toHaveBeenCalledWith(
|
||||
expect(createMatrixClientModule.createClientWithCreds).toHaveBeenCalledWith(
|
||||
{
|
||||
userId,
|
||||
// decrypted accessToken
|
||||
@@ -448,7 +452,7 @@ describe("Lifecycle", () => {
|
||||
it("should create new matrix client with credentials", async () => {
|
||||
expect(await restoreSessionFromStorage()).toEqual(true);
|
||||
|
||||
expect(MatrixClientPeg.replaceUsingCreds).toHaveBeenCalledWith(
|
||||
expect(createMatrixClientModule.createClientWithCreds).toHaveBeenCalledWith(
|
||||
{
|
||||
userId,
|
||||
accessToken,
|
||||
@@ -501,7 +505,7 @@ describe("Lifecycle", () => {
|
||||
expect(await restoreSessionFromStorage()).toEqual(true);
|
||||
|
||||
// Ensure that the expected calls were made
|
||||
expect(MatrixClientPeg.replaceUsingCreds).toHaveBeenCalledWith(
|
||||
expect(createMatrixClientModule.createClientWithCreds).toHaveBeenCalledWith(
|
||||
{
|
||||
userId,
|
||||
// decrypted accessToken
|
||||
@@ -614,6 +618,7 @@ describe("Lifecycle", () => {
|
||||
describe("without a pickle key", () => {
|
||||
beforeEach(() => {
|
||||
jest.spyOn(mockPlatform, "createPickleKey").mockResolvedValue(null);
|
||||
jest.spyOn(createMatrixClientModule, "createClientWithCreds").mockReturnValue(mockClient);
|
||||
});
|
||||
|
||||
it("should persist credentials", async () => {
|
||||
@@ -667,7 +672,7 @@ describe("Lifecycle", () => {
|
||||
it("should create new matrix client with credentials", async () => {
|
||||
expect(await setLoggedIn(credentials)).toEqual(mockClient);
|
||||
|
||||
expect(MatrixClientPeg.replaceUsingCreds).toHaveBeenCalledWith(
|
||||
expect(createMatrixClientModule.createClientWithCreds).toHaveBeenCalledWith(
|
||||
{
|
||||
userId,
|
||||
accessToken,
|
||||
@@ -763,9 +768,10 @@ describe("Lifecycle", () => {
|
||||
});
|
||||
|
||||
it("should create new matrix client with credentials", async () => {
|
||||
jest.spyOn(createMatrixClientModule, "createClientWithCreds").mockReturnValue(mockClient);
|
||||
expect(await setLoggedIn(credentials)).toEqual(mockClient);
|
||||
|
||||
expect(MatrixClientPeg.replaceUsingCreds).toHaveBeenCalledWith(
|
||||
expect(createMatrixClientModule.createClientWithCreds).toHaveBeenCalledWith(
|
||||
{
|
||||
userId,
|
||||
accessToken,
|
||||
@@ -856,7 +862,7 @@ describe("Lifecycle", () => {
|
||||
}),
|
||||
).toEqual(mockClient);
|
||||
|
||||
expect(MatrixClientPeg.replaceUsingCreds).toHaveBeenCalledWith(
|
||||
expect(createMatrixClientModule.createClientWithCreds).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
accessToken,
|
||||
refreshToken,
|
||||
@@ -877,7 +883,7 @@ describe("Lifecycle", () => {
|
||||
}),
|
||||
).toEqual(mockClient);
|
||||
|
||||
expect(MatrixClientPeg.replaceUsingCreds).toHaveBeenCalledWith(
|
||||
expect(createMatrixClientModule.createClientWithCreds).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
accessToken,
|
||||
refreshToken,
|
||||
@@ -939,6 +945,7 @@ describe("Lifecycle", () => {
|
||||
|
||||
it("should replace the current login with a new one", async () => {
|
||||
const stopSpy = jest.spyOn(mockClient, "stopClient").mockReturnValue(undefined);
|
||||
jest.spyOn(createMatrixClientModule, "createClientWithCreds").mockReturnValue(mockClient);
|
||||
const dis = window.mxDispatcher;
|
||||
|
||||
const firstLoginEvent: Promise<void> = new Promise((resolve) => {
|
||||
@@ -958,7 +965,7 @@ describe("Lifecycle", () => {
|
||||
// So spy on it and make sure it's not called.
|
||||
jest.spyOn(MatrixClientPeg, "unset").mockReturnValue(undefined);
|
||||
|
||||
expect(MatrixClientPeg.replaceUsingCreds).toHaveBeenCalledWith(
|
||||
expect(createMatrixClientModule.createClientWithCreds).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
userId,
|
||||
}),
|
||||
@@ -992,7 +999,7 @@ describe("Lifecycle", () => {
|
||||
// the client should have been stopped
|
||||
expect(stopSpy).toHaveBeenCalledTimes(2);
|
||||
|
||||
expect(MatrixClientPeg.replaceUsingCreds).toHaveBeenCalledWith(
|
||||
expect(createMatrixClientModule.createClientWithCreds).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
userId: otherCredentials.userId,
|
||||
}),
|
||||
|
||||
@@ -7,9 +7,10 @@ Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
import { logger } from "matrix-js-sdk/src/logger";
|
||||
import { type MatrixClient } from "matrix-js-sdk/src/matrix";
|
||||
import fetchMock from "@fetch-mock/jest";
|
||||
|
||||
import { advanceDateAndTime, stubClient } from "../test-utils";
|
||||
import { advanceDateAndTime, createTestClient, stubClient } from "../test-utils";
|
||||
import { type IMatrixClientPeg, MatrixClientPeg as peg } from "../../src/MatrixClientPeg";
|
||||
|
||||
jest.useFakeTimers();
|
||||
@@ -70,12 +71,11 @@ describe("MatrixClientPeg", () => {
|
||||
// instantiate a MatrixClientPegClass instance, with a new MatrixClient
|
||||
testPeg = new PegClass();
|
||||
fetchMock.get("http://example.com/_matrix/client/versions", {});
|
||||
testPeg.replaceUsingCreds({
|
||||
accessToken: "SEKRET",
|
||||
homeserverUrl: "http://example.com",
|
||||
userId: "@user:example.com",
|
||||
deviceId: "TEST_DEVICE_ID",
|
||||
});
|
||||
|
||||
const mockClient = createTestClient();
|
||||
mockClient.initRustCrypto = jest.fn();
|
||||
mockClient.startClient = jest.fn();
|
||||
testPeg.set(mockClient as unknown as MatrixClient);
|
||||
});
|
||||
|
||||
it("should initialise the rust crypto library by default", async () => {
|
||||
|
||||
@@ -6,7 +6,6 @@ SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Com
|
||||
Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
import "fake-indexeddb/auto";
|
||||
import React, { type ComponentProps, createRef, type RefObject } from "react";
|
||||
import { fireEvent, render, type RenderResult, screen, waitFor, within, act } from "jest-matrix-react";
|
||||
import { type Mocked, mocked } from "jest-mock-vitest-adapter";
|
||||
@@ -474,7 +473,7 @@ describe("<MatrixChat />", () => {
|
||||
|
||||
const tokenResponse: BearerTokenResponse = {
|
||||
access_token: accessToken,
|
||||
refresh_token: "def456",
|
||||
refresh_token: undefined,
|
||||
id_token: "ghi789",
|
||||
scope: "test",
|
||||
token_type: "Bearer",
|
||||
@@ -644,12 +643,6 @@ describe("<MatrixChat />", () => {
|
||||
});
|
||||
|
||||
describe("when login succeeds", () => {
|
||||
beforeEach(() => {
|
||||
jest.spyOn(StorageAccess, "idbLoad").mockImplementation(
|
||||
async (_table: string, key: string | string[]) => (key === "mx_access_token" ? accessToken : null),
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
SettingsStore.reset();
|
||||
});
|
||||
@@ -1366,7 +1359,7 @@ describe("<MatrixChat />", () => {
|
||||
// but as the exception was swallowed, the test was passing (see in `initClientCrypto`).
|
||||
// There are several uses of the peg in the app, so during all these tests you might end-up
|
||||
// with a real client instead of the mocked one. Not sure how reliable all these tests are.
|
||||
jest.spyOn(MatrixClientPeg, "replaceUsingCreds");
|
||||
jest.spyOn(MatrixClientPeg, "set");
|
||||
jest.spyOn(MatrixClientPeg, "get").mockReturnValue(mockClient);
|
||||
|
||||
const result = getComponent();
|
||||
|
||||
Reference in New Issue
Block a user