element-web-Github/test/unit-tests/Rooms-test.ts
Michael Telatynski c05c429803
Absorb the matrix-react-sdk repository (#28192)
Co-authored-by: github-merge-queue <118344674+github-merge-queue@users.noreply.github.com>
Co-authored-by: github-merge-queue <github-merge-queue@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Florian Duros <florian.duros@ormaz.fr>
Co-authored-by: Kim Brose <kim.brose@nordeck.net>
Co-authored-by: Florian Duros <florianduros@element.io>
Co-authored-by: R Midhun Suresh <hi@midhun.dev>
Co-authored-by: dbkr <986903+dbkr@users.noreply.github.com>
Co-authored-by: ElementRobot <releases@riot.im>
Co-authored-by: dbkr <dbkr@users.noreply.github.com>
Co-authored-by: David Baker <dbkr@users.noreply.github.com>
Co-authored-by: Michael Telatynski <7t3chguy@gmail.com>
Co-authored-by: Richard van der Hoff <1389908+richvdh@users.noreply.github.com>
Co-authored-by: David Langley <davidl@element.io>
Co-authored-by: Michael Weimann <michaelw@matrix.org>
Co-authored-by: Timshel <Timshel@users.noreply.github.com>
Co-authored-by: Sahil Silare <32628578+sahil9001@users.noreply.github.com>
Co-authored-by: Will Hunt <will@half-shot.uk>
Co-authored-by: Hubert Chathi <hubert@uhoreg.ca>
Co-authored-by: Andrew Ferrazzutti <andrewf@element.io>
Co-authored-by: Robin <robin@robin.town>
Co-authored-by: Tulir Asokan <tulir@maunium.net>
2024-10-16 13:31:55 +01:00

144 lines
4.5 KiB
TypeScript

/*
Copyright 2024 New Vector Ltd.
Copyright 2022 The Matrix.org Foundation C.I.C.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
Please see LICENSE files in the repository root for full details.
*/
import { mocked } from "jest-mock";
import { EventType, MatrixClient, MatrixEvent } from "matrix-js-sdk/src/matrix";
import { setDMRoom } from "../../src/Rooms";
import { mkEvent, stubClient } from "../test-utils";
describe("setDMRoom", () => {
const userId1 = "@user1:example.com";
const userId2 = "@user2:example.com";
const userId3 = "@user3:example.com";
const roomId1 = "!room1:example.com";
const roomId2 = "!room2:example.com";
const roomId3 = "!room3:example.com";
const roomId4 = "!room4:example.com";
let client: MatrixClient;
beforeEach(() => {
client = mocked(stubClient());
client.getAccountData = jest.fn().mockImplementation((eventType: string): MatrixEvent | undefined => {
if (eventType === EventType.Direct) {
return mkEvent({
event: true,
content: {
[userId1]: [roomId1, roomId2],
[userId2]: [roomId3],
},
type: EventType.Direct,
user: client.getSafeUserId(),
});
}
return undefined;
});
});
describe("when logged in as a guest and marking a room as DM", () => {
beforeEach(() => {
mocked(client.isGuest).mockReturnValue(true);
setDMRoom(client, roomId1, userId1);
});
it("should not update the account data", () => {
expect(client.setAccountData).not.toHaveBeenCalled();
});
});
describe("when adding a new room to an existing DM relation", () => {
beforeEach(() => {
setDMRoom(client, roomId4, userId1);
});
it("should update the account data accordingly", () => {
expect(client.setAccountData).toHaveBeenCalledWith(EventType.Direct, {
[userId1]: [roomId1, roomId2, roomId4],
[userId2]: [roomId3],
});
});
});
describe("when adding a new DM room", () => {
beforeEach(() => {
setDMRoom(client, roomId4, userId3);
});
it("should update the account data accordingly", () => {
expect(client.setAccountData).toHaveBeenCalledWith(EventType.Direct, {
[userId1]: [roomId1, roomId2],
[userId2]: [roomId3],
[userId3]: [roomId4],
});
});
});
describe("when trying to add a DM, that already exists", () => {
beforeEach(() => {
setDMRoom(client, roomId1, userId1);
});
it("should not update the account data", () => {
expect(client.setAccountData).not.toHaveBeenCalled();
});
});
describe("when removing an existing DM", () => {
beforeEach(() => {
setDMRoom(client, roomId1, null);
});
it("should update the account data accordingly", () => {
expect(client.setAccountData).toHaveBeenCalledWith(EventType.Direct, {
[userId1]: [roomId2],
[userId2]: [roomId3],
});
});
});
describe("when removing an unknown room", () => {
beforeEach(() => {
setDMRoom(client, roomId4, null);
});
it("should not update the account data", () => {
expect(client.setAccountData).not.toHaveBeenCalled();
});
});
describe("when the direct event is undefined", () => {
beforeEach(() => {
mocked(client.getAccountData).mockReturnValue(undefined);
setDMRoom(client, roomId1, userId1);
});
it("should update the account data accordingly", () => {
expect(client.setAccountData).toHaveBeenCalledWith(EventType.Direct, {
[userId1]: [roomId1],
});
});
});
describe("when the current content is undefined", () => {
beforeEach(() => {
// @ts-ignore
mocked(client.getAccountData).mockReturnValue({
getContent: jest.fn(),
});
setDMRoom(client, roomId1, userId1);
});
it("should update the account data accordingly", () => {
expect(client.setAccountData).toHaveBeenCalledWith(EventType.Direct, {
[userId1]: [roomId1],
});
});
});
});