|
| 1 | +/* |
| 2 | +Copyright 2023 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 { waitFor } from "@testing-library/react"; |
| 18 | +import { renderHook, act } from "@testing-library/react-hooks/dom"; |
| 19 | +import { MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix"; |
| 20 | + |
| 21 | +import { MatrixClientPeg } from "../../src/MatrixClientPeg"; |
| 22 | +import { stubClient } from "../test-utils"; |
| 23 | +import { useMyRoomMembership, useRoomMemberCount, useRoomMembers } from "../../src/hooks/useRoomMembers"; |
| 24 | + |
| 25 | +describe("useRoomMembers", () => { |
| 26 | + function render(room: Room) { |
| 27 | + return renderHook(() => useRoomMembers(room)); |
| 28 | + } |
| 29 | + |
| 30 | + let cli: MatrixClient; |
| 31 | + let room: Room; |
| 32 | + |
| 33 | + beforeEach(() => { |
| 34 | + stubClient(); |
| 35 | + cli = MatrixClientPeg.safeGet(); |
| 36 | + room = new Room("!room:server", cli, cli.getSafeUserId()); |
| 37 | + }); |
| 38 | + |
| 39 | + it("should update on RoomState.Members events", async () => { |
| 40 | + const { result } = render(room); |
| 41 | + |
| 42 | + expect(result.current).toHaveLength(0); |
| 43 | + |
| 44 | + act(() => { |
| 45 | + room.currentState.markOutOfBandMembersStarted(); |
| 46 | + room.currentState.setOutOfBandMembers([ |
| 47 | + new MatrixEvent({ |
| 48 | + type: "m.room.member", |
| 49 | + state_key: "!user:server", |
| 50 | + room_id: room.roomId, |
| 51 | + content: { |
| 52 | + membership: "join", |
| 53 | + }, |
| 54 | + }), |
| 55 | + ]); |
| 56 | + }); |
| 57 | + await waitFor(() => expect(result.current).toHaveLength(1)); |
| 58 | + }); |
| 59 | +}); |
| 60 | + |
| 61 | +describe("useRoomMemberCount", () => { |
| 62 | + function render(room: Room) { |
| 63 | + return renderHook(() => useRoomMemberCount(room)); |
| 64 | + } |
| 65 | + |
| 66 | + let cli: MatrixClient; |
| 67 | + let room: Room; |
| 68 | + |
| 69 | + beforeEach(() => { |
| 70 | + stubClient(); |
| 71 | + cli = MatrixClientPeg.safeGet(); |
| 72 | + room = new Room("!room:server", cli, cli.getSafeUserId()); |
| 73 | + }); |
| 74 | + |
| 75 | + it("should update on RoomState.Members events", async () => { |
| 76 | + const { result } = render(room); |
| 77 | + |
| 78 | + expect(result.current).toBe(0); |
| 79 | + |
| 80 | + act(() => { |
| 81 | + room.currentState.markOutOfBandMembersStarted(); |
| 82 | + room.currentState.setOutOfBandMembers([ |
| 83 | + new MatrixEvent({ |
| 84 | + type: "m.room.member", |
| 85 | + state_key: "!user:server", |
| 86 | + room_id: room.roomId, |
| 87 | + content: { |
| 88 | + membership: "join", |
| 89 | + }, |
| 90 | + }), |
| 91 | + ]); |
| 92 | + }); |
| 93 | + await waitFor(() => expect(result.current).toBe(1)); |
| 94 | + }); |
| 95 | +}); |
| 96 | + |
| 97 | +describe("useMyRoomMembership", () => { |
| 98 | + function render(room: Room) { |
| 99 | + return renderHook(() => useMyRoomMembership(room)); |
| 100 | + } |
| 101 | + |
| 102 | + let cli: MatrixClient; |
| 103 | + let room: Room; |
| 104 | + |
| 105 | + beforeEach(() => { |
| 106 | + stubClient(); |
| 107 | + cli = MatrixClientPeg.safeGet(); |
| 108 | + room = new Room("!room:server", cli, cli.getSafeUserId()); |
| 109 | + }); |
| 110 | + |
| 111 | + it("should update on RoomState.Members events", async () => { |
| 112 | + room.updateMyMembership("join"); |
| 113 | + const { result } = render(room); |
| 114 | + |
| 115 | + expect(result.current).toBe("join"); |
| 116 | + |
| 117 | + act(() => { |
| 118 | + room.updateMyMembership("leave"); |
| 119 | + }); |
| 120 | + await waitFor(() => expect(result.current).toBe("leave")); |
| 121 | + }); |
| 122 | +}); |
0 commit comments