Files
ThreadNet-Web/apps/web/src/utils/createMatrixClient.test.ts
T
David BakerandGitHub ff8329be09 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
2026-07-03 15:21:59 +00:00

154 lines
5.9 KiB
TypeScript

/*
Copyright 2026 Element Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE files in the repository root for full details.
*/
import { vi, describe, beforeEach, it, expect } from "vitest";
import { type MatrixClient, RoomNameType } from "matrix-js-sdk/src/matrix";
import { createClientWithCreds } from "./createMatrixClient";
describe("createMatrixClient", () => {
let client: MatrixClient;
beforeEach(() => {
vi.stubGlobal("localStorage", {
getItem: vi.fn().mockReturnValue(null),
setItem: vi.fn(),
removeItem: vi.fn(),
});
client = createClientWithCreds({
homeserverUrl: "https://test.dummy",
userId: "@user:test.dummy",
accessToken: "access_token",
});
});
describe("room name generator", () => {
it("should return empty room for an empty room", () => {
const roomName = client.roomNameGenerator?.("", {
type: RoomNameType.EmptyRoom,
});
expect(roomName).toBe("Empty room");
});
it("should include the old name for an empty room that used to have a name", () => {
const roomName = client.roomNameGenerator?.("", {
type: RoomNameType.EmptyRoom,
oldName: "Old Room",
});
expect(roomName).toBe("Empty room (was Old Room)");
});
it("should return null for an actual room name", () => {
const roomName = client.roomNameGenerator?.("", {
type: RoomNameType.Actual,
name: "Some name",
});
expect(roomName).toBeNull();
});
describe("generated room names", () => {
it("should return empty room when there are no members", () => {
const roomName = client.roomNameGenerator?.("", {
type: RoomNameType.Generated,
names: [],
count: 0,
});
expect(roomName).toBe("Empty room");
});
it("should return the single member name when there is only one other member", () => {
const roomName = client.roomNameGenerator?.("", {
type: RoomNameType.Generated,
names: ["Alice"],
count: 2,
});
expect(roomName).toBe("Alice");
});
it("should join two member names with 'and'", () => {
const roomName = client.roomNameGenerator?.("", {
type: RoomNameType.Generated,
names: ["Alice", "Bob"],
count: 2,
});
expect(roomName).toBe("Alice and Bob");
});
it("should name the first member and count the rest when there is one other member not named", () => {
const roomName = client.roomNameGenerator?.("", {
type: RoomNameType.Generated,
names: ["Alice", "Bob"],
count: 3,
});
expect(roomName).toBe("Alice and one other");
});
it("should name the first member and count the rest when there are multiple members not named", () => {
const roomName = client.roomNameGenerator?.("", {
type: RoomNameType.Generated,
names: ["Alice", "Bob", "Carol"],
count: 3,
});
expect(roomName).toBe("Alice and 2 others");
});
describe("when inviting", () => {
it("should return empty room when there are no invitees", () => {
const roomName = client.roomNameGenerator?.("", {
type: RoomNameType.Generated,
subtype: "Inviting",
names: [],
count: 0,
});
expect(roomName).toBe("Empty room");
});
it("should return the single invitee name when there is only one invitee", () => {
const roomName = client.roomNameGenerator?.("", {
type: RoomNameType.Generated,
subtype: "Inviting",
names: ["Alice"],
count: 1,
});
expect(roomName).toBe("Alice");
});
it("should say who is being invited when there are two invitees", () => {
const roomName = client.roomNameGenerator?.("", {
type: RoomNameType.Generated,
subtype: "Inviting",
names: ["Alice", "Bob"],
count: 2,
});
expect(roomName).toBe("Inviting Alice and Bob");
});
it("should name the first invitee and count the rest when there are more than two invitees", () => {
const roomName = client.roomNameGenerator?.("", {
type: RoomNameType.Generated,
subtype: "Inviting",
names: ["Alice", "Bob", "Carol"],
count: 3,
});
expect(roomName).toBe("Inviting Alice and 2 others");
});
it("should count uninvited members separately from named invitees", () => {
const roomName = client.roomNameGenerator?.("", {
type: RoomNameType.Generated,
subtype: "Inviting",
names: ["Alice", "Bob"],
count: 4,
});
expect(roomName).toBe("Inviting Alice and 3 others");
});
});
});
});
});