|
| 1 | +/* |
| 2 | +Copyright 2022 The Matrix.org Foundation C.I.C. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +import { mocked } from "jest-mock"; |
| 18 | +import { EventType, MatrixClient, MatrixEvent } from "matrix-js-sdk/src/matrix"; |
| 19 | + |
| 20 | +import { setDMRoom } from "../src/Rooms"; |
| 21 | +import { mkEvent, stubClient } from "./test-utils"; |
| 22 | + |
| 23 | +describe("setDMRoom", () => { |
| 24 | + const userId1 = "@user1:example.com"; |
| 25 | + const userId2 = "@user2:example.com"; |
| 26 | + const userId3 = "@user3:example.com"; |
| 27 | + const roomId1 = "!room1:example.com"; |
| 28 | + const roomId2 = "!room2:example.com"; |
| 29 | + const roomId3 = "!room3:example.com"; |
| 30 | + const roomId4 = "!room4:example.com"; |
| 31 | + let client: MatrixClient; |
| 32 | + |
| 33 | + beforeEach(() => { |
| 34 | + client = mocked(stubClient()); |
| 35 | + client.getAccountData = jest.fn().mockImplementation((eventType: string): MatrixEvent | undefined => { |
| 36 | + if (eventType === EventType.Direct) { |
| 37 | + return mkEvent({ |
| 38 | + event: true, |
| 39 | + content: { |
| 40 | + [userId1]: [roomId1, roomId2], |
| 41 | + [userId2]: [roomId3], |
| 42 | + }, |
| 43 | + type: EventType.Direct, |
| 44 | + user: client.getUserId(), |
| 45 | + }); |
| 46 | + } |
| 47 | + |
| 48 | + return undefined; |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + describe("when logged in as a guest and marking a room as DM", () => { |
| 53 | + beforeEach(() => { |
| 54 | + mocked(client.isGuest).mockReturnValue(true); |
| 55 | + setDMRoom(roomId1, userId1); |
| 56 | + }); |
| 57 | + |
| 58 | + it("should not update the account data", () => { |
| 59 | + expect(client.setAccountData).not.toHaveBeenCalled(); |
| 60 | + }); |
| 61 | + }); |
| 62 | + |
| 63 | + describe("when adding a new room to an existing DM relation", () => { |
| 64 | + beforeEach(() => { |
| 65 | + setDMRoom(roomId4, userId1); |
| 66 | + }); |
| 67 | + |
| 68 | + it("should update the account data accordingly", () => { |
| 69 | + expect(client.setAccountData).toHaveBeenCalledWith(EventType.Direct, { |
| 70 | + [userId1]: [roomId1, roomId2, roomId4], |
| 71 | + [userId2]: [roomId3], |
| 72 | + }); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + describe("when adding a new DM room", () => { |
| 77 | + beforeEach(() => { |
| 78 | + setDMRoom(roomId4, userId3); |
| 79 | + }); |
| 80 | + |
| 81 | + it("should update the account data accordingly", () => { |
| 82 | + expect(client.setAccountData).toHaveBeenCalledWith(EventType.Direct, { |
| 83 | + [userId1]: [roomId1, roomId2], |
| 84 | + [userId2]: [roomId3], |
| 85 | + [userId3]: [roomId4], |
| 86 | + }); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + describe("when trying to add a DM, that already exists", () => { |
| 91 | + beforeEach(() => { |
| 92 | + setDMRoom(roomId1, userId1); |
| 93 | + }); |
| 94 | + |
| 95 | + it("should not update the account data", () => { |
| 96 | + expect(client.setAccountData).not.toHaveBeenCalled(); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + describe("when removing an existing DM", () => { |
| 101 | + beforeEach(() => { |
| 102 | + setDMRoom(roomId1, null); |
| 103 | + }); |
| 104 | + |
| 105 | + it("should update the account data accordingly", () => { |
| 106 | + expect(client.setAccountData).toHaveBeenCalledWith(EventType.Direct, { |
| 107 | + [userId1]: [roomId2], |
| 108 | + [userId2]: [roomId3], |
| 109 | + }); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + describe("when removing an unknown room", () => { |
| 114 | + beforeEach(() => { |
| 115 | + setDMRoom(roomId4, null); |
| 116 | + }); |
| 117 | + |
| 118 | + it("should not update the account data", () => { |
| 119 | + expect(client.setAccountData).not.toHaveBeenCalled(); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + describe("when the direct event is undefined", () => { |
| 124 | + beforeEach(() => { |
| 125 | + mocked(client.getAccountData).mockReturnValue(undefined); |
| 126 | + setDMRoom(roomId1, userId1); |
| 127 | + }); |
| 128 | + |
| 129 | + it("should update the account data accordingly", () => { |
| 130 | + expect(client.setAccountData).toHaveBeenCalledWith(EventType.Direct, { |
| 131 | + [userId1]: [roomId1], |
| 132 | + }); |
| 133 | + }); |
| 134 | + }); |
| 135 | + |
| 136 | + describe("when the current content is undefined", () => { |
| 137 | + beforeEach(() => { |
| 138 | + // @ts-ignore |
| 139 | + mocked(client.getAccountData).mockReturnValue({ |
| 140 | + getContent: jest.fn(), |
| 141 | + }); |
| 142 | + setDMRoom(roomId1, userId1); |
| 143 | + }); |
| 144 | + |
| 145 | + it("should update the account data accordingly", () => { |
| 146 | + expect(client.setAccountData).toHaveBeenCalledWith(EventType.Direct, { |
| 147 | + [userId1]: [roomId1], |
| 148 | + }); |
| 149 | + }); |
| 150 | + }); |
| 151 | +}); |
0 commit comments