element-web/test/utils/direct-messages-test.ts

170 lines
6.3 KiB
TypeScript
Raw Normal View History

/*
Copyright 2022 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { mocked } from "jest-mock";
import { ClientEvent, MatrixClient, Room } from "matrix-js-sdk/src/matrix";
import DMRoomMap from "../../src/utils/DMRoomMap";
import { createTestClient } from "../test-utils";
import { LocalRoom, LocalRoomState, LOCAL_ROOM_ID_PREFIX } from "../../src/models/LocalRoom";
import * as dmModule from "../../src/utils/direct-messages";
import dis from "../../src/dispatcher/dispatcher";
import { Action } from "../../src/dispatcher/actions";
import { MatrixClientPeg } from "../../src/MatrixClientPeg";
import { waitForRoomReadyAndApplyAfterCreateCallbacks } from "../../src/utils/local-room";
import { findDMRoom } from "../../src/utils/dm/findDMRoom";
import { createDmLocalRoom } from "../../src/utils/dm/createDmLocalRoom";
import { startDm } from "../../src/utils/dm/startDm";
jest.mock("../../src/utils/rooms", () => ({
...(jest.requireActual("../../src/utils/rooms") as object),
privateShouldBeEncrypted: jest.fn(),
}));
jest.mock("../../src/createRoom", () => ({
...(jest.requireActual("../../src/createRoom") as object),
canEncryptToAllUsers: jest.fn(),
}));
jest.mock("../../src/utils/local-room", () => ({
waitForRoomReadyAndApplyAfterCreateCallbacks: jest.fn(),
}));
jest.mock("../../src/utils/dm/findDMForUser", () => ({
findDMForUser: jest.fn(),
}));
jest.mock("../../src/utils/dm/findDMRoom", () => ({
findDMRoom: jest.fn(),
}));
jest.mock("../../src/utils/dm/createDmLocalRoom", () => ({
createDmLocalRoom: jest.fn(),
}));
jest.mock("../../src/utils/dm/startDm", () => ({
startDm: jest.fn(),
}));
describe("direct-messages", () => {
const userId1 = "@user1:example.com";
const member1 = new dmModule.DirectoryMember({ user_id: userId1 });
let room1: Room;
let localRoom: LocalRoom;
let dmRoomMap: DMRoomMap;
let mockClient: MatrixClient;
let roomEvents: Room[];
beforeEach(() => {
jest.restoreAllMocks();
mockClient = createTestClient();
jest.spyOn(MatrixClientPeg, "get").mockReturnValue(mockClient);
roomEvents = [];
mockClient.on(ClientEvent.Room, (room: Room) => {
roomEvents.push(room);
});
room1 = new Room("!room1:example.com", mockClient, userId1);
room1.getMyMembership = () => "join";
localRoom = new LocalRoom(LOCAL_ROOM_ID_PREFIX + "test", mockClient, userId1);
dmRoomMap = {
getDMRoomForIdentifiers: jest.fn(),
getDMRoomsForUserId: jest.fn(),
} as unknown as DMRoomMap;
jest.spyOn(DMRoomMap, "shared").mockReturnValue(dmRoomMap);
jest.spyOn(dis, "dispatch");
jest.setSystemTime(new Date(2022, 7, 4, 11, 12, 30, 42));
});
describe("startDmOnFirstMessage", () => {
describe("if no room exists", () => {
beforeEach(() => {
mocked(findDMRoom).mockReturnValue(null);
});
it("should create a local room and dispatch a view room event", async () => {
mocked(createDmLocalRoom).mockResolvedValue(localRoom);
const room = await dmModule.startDmOnFirstMessage(mockClient, [member1]);
expect(room).toBe(localRoom);
expect(dis.dispatch).toHaveBeenCalledWith({
action: Action.ViewRoom,
room_id: room.roomId,
joining: false,
targets: [member1],
});
});
});
describe("if a room exists", () => {
beforeEach(() => {
mocked(findDMRoom).mockReturnValue(room1);
});
it("should return the room and dispatch a view room event", async () => {
const room = await dmModule.startDmOnFirstMessage(mockClient, [member1]);
expect(room).toBe(room1);
expect(dis.dispatch).toHaveBeenCalledWith({
action: Action.ViewRoom,
room_id: room1.roomId,
should_peek: false,
joining: false,
metricsTrigger: "MessageUser",
});
});
});
});
describe("createRoomFromLocalRoom", () => {
[LocalRoomState.CREATING, LocalRoomState.CREATED, LocalRoomState.ERROR].forEach((state: LocalRoomState) => {
it(`should do nothing for room in state ${state}`, async () => {
localRoom.state = state;
await dmModule.createRoomFromLocalRoom(mockClient, localRoom);
expect(localRoom.state).toBe(state);
expect(startDm).not.toHaveBeenCalled();
});
});
describe("on startDm error", () => {
beforeEach(() => {
mocked(startDm).mockRejectedValue(true);
});
it("should set the room state to error", async () => {
await dmModule.createRoomFromLocalRoom(mockClient, localRoom);
expect(localRoom.state).toBe(LocalRoomState.ERROR);
});
});
describe("on startDm success", () => {
beforeEach(() => {
mocked(waitForRoomReadyAndApplyAfterCreateCallbacks).mockResolvedValue(room1.roomId);
mocked(startDm).mockResolvedValue(room1.roomId);
});
2022-12-12 14:24:14 +03:00
it("should set the room into creating state and call waitForRoomReadyAndApplyAfterCreateCallbacks", async () => {
const result = await dmModule.createRoomFromLocalRoom(mockClient, localRoom);
expect(result).toBe(room1.roomId);
expect(localRoom.state).toBe(LocalRoomState.CREATING);
expect(waitForRoomReadyAndApplyAfterCreateCallbacks).toHaveBeenCalledWith(mockClient, localRoom);
});
});
});
});