|
| 1 | +/* |
| 2 | +Copyright 2017 - 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 { Room } from 'matrix-js-sdk/src/matrix'; |
| 18 | + |
| 19 | +import { RoomViewStore } from '../../src/stores/RoomViewStore'; |
| 20 | +import { Action } from '../../src/dispatcher/actions'; |
| 21 | +import * as testUtils from '../test-utils'; |
| 22 | +import { flushPromises, getMockClientWithEventEmitter } from '../test-utils'; |
| 23 | + |
| 24 | +const dispatch = testUtils.getDispatchForStore(RoomViewStore.instance); |
| 25 | + |
| 26 | +jest.mock('../../src/utils/DMRoomMap', () => { |
| 27 | + const mock = { |
| 28 | + getUserIdForRoomId: jest.fn(), |
| 29 | + getDMRoomsForUserId: jest.fn(), |
| 30 | + }; |
| 31 | + |
| 32 | + return { |
| 33 | + shared: jest.fn().mockReturnValue(mock), |
| 34 | + sharedInstance: mock, |
| 35 | + }; |
| 36 | +}); |
| 37 | + |
| 38 | +describe('RoomViewStore', function() { |
| 39 | + const userId = '@alice:server'; |
| 40 | + const mockClient = getMockClientWithEventEmitter({ |
| 41 | + joinRoom: jest.fn(), |
| 42 | + getRoom: jest.fn(), |
| 43 | + getRoomIdForAlias: jest.fn(), |
| 44 | + }); |
| 45 | + const room = new Room('!room:server', mockClient, userId); |
| 46 | + |
| 47 | + beforeEach(function() { |
| 48 | + jest.clearAllMocks(); |
| 49 | + mockClient.credentials = { userId: "@test:example.com" }; |
| 50 | + mockClient.joinRoom.mockResolvedValue(room); |
| 51 | + mockClient.getRoom.mockReturnValue(room); |
| 52 | + |
| 53 | + // Reset the state of the store |
| 54 | + RoomViewStore.instance.reset(); |
| 55 | + }); |
| 56 | + |
| 57 | + it('can be used to view a room by ID and join', async () => { |
| 58 | + dispatch({ action: Action.ViewRoom, room_id: '!randomcharacters:aser.ver' }); |
| 59 | + dispatch({ action: 'join_room' }); |
| 60 | + await flushPromises(); |
| 61 | + expect(mockClient.joinRoom).toHaveBeenCalledWith('!randomcharacters:aser.ver', { viaServers: [] }); |
| 62 | + expect(RoomViewStore.instance.isJoining()).toBe(true); |
| 63 | + }); |
| 64 | + |
| 65 | + it('can be used to view a room by alias and join', async () => { |
| 66 | + const roomId = "!randomcharacters:aser.ver"; |
| 67 | + const alias = "#somealias2:aser.ver"; |
| 68 | + |
| 69 | + mockClient.getRoomIdForAlias.mockResolvedValue({ room_id: roomId, servers: [] }); |
| 70 | + |
| 71 | + dispatch({ action: Action.ViewRoom, room_alias: alias }); |
| 72 | + await flushPromises(); |
| 73 | + await flushPromises(); |
| 74 | + |
| 75 | + // roomId is set to id of the room alias |
| 76 | + expect(RoomViewStore.instance.getRoomId()).toBe(roomId); |
| 77 | + |
| 78 | + // join the room |
| 79 | + dispatch({ action: 'join_room' }); |
| 80 | + |
| 81 | + expect(RoomViewStore.instance.isJoining()).toBeTruthy(); |
| 82 | + await flushPromises(); |
| 83 | + |
| 84 | + expect(mockClient.joinRoom).toHaveBeenCalledWith(alias, { viaServers: [] }); |
| 85 | + }); |
| 86 | +}); |
0 commit comments