|
| 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 React from "react"; |
| 18 | +import { Room } from "matrix-js-sdk/src/models/room"; |
| 19 | +import { fireEvent, render, screen } from "@testing-library/react"; |
| 20 | + |
| 21 | +import { mkEvent, stubClient } from "../../../test-utils"; |
| 22 | +import { MatrixClientPeg } from "../../../../src/MatrixClientPeg"; |
| 23 | +import RoomTopic from "../../../../src/components/views/elements/RoomTopic"; |
| 24 | +import dis from "../../../../src/dispatcher/dispatcher"; |
| 25 | +import { Action } from "../../../../src/dispatcher/actions"; |
| 26 | + |
| 27 | +jest.mock("../../../../src/dispatcher/dispatcher"); |
| 28 | + |
| 29 | +describe("<RoomTopic/>", () => { |
| 30 | + const originalHref = window.location.href; |
| 31 | + |
| 32 | + afterEach(() => { |
| 33 | + window.location.href = originalHref; |
| 34 | + }); |
| 35 | + |
| 36 | + function runClickTest(topic: string, clickText: string) { |
| 37 | + stubClient(); |
| 38 | + |
| 39 | + const room = new Room("!pMBteVpcoJRdCJxDmn:matrix.org", MatrixClientPeg.safeGet(), "@alice:example.org"); |
| 40 | + const topicEvent = mkEvent({ |
| 41 | + type: "m.room.topic", |
| 42 | + room: "!pMBteVpcoJRdCJxDmn:matrix.org", |
| 43 | + user: "@alice:example.org", |
| 44 | + content: { topic }, |
| 45 | + ts: 123, |
| 46 | + event: true, |
| 47 | + }); |
| 48 | + |
| 49 | + room.addLiveEvents([topicEvent]); |
| 50 | + |
| 51 | + render(<RoomTopic room={room} />); |
| 52 | + |
| 53 | + fireEvent.click(screen.getByText(clickText)); |
| 54 | + } |
| 55 | + |
| 56 | + it("should capture permalink clicks", () => { |
| 57 | + const permalink = |
| 58 | + "https://matrix.to/#/!pMBteVpcoJRdCJxDmn:matrix.org/$K4Kg0fL-GKpW1EQ6lS36bP4eUXadWJFkdK_FH73Df8A?via=matrix.org"; |
| 59 | + const expectedHref = |
| 60 | + "http://localhost/#/room/!pMBteVpcoJRdCJxDmn:matrix.org/$K4Kg0fL-GKpW1EQ6lS36bP4eUXadWJFkdK_FH73Df8A?via=matrix.org"; |
| 61 | + runClickTest(`... ${permalink} ...`, permalink); |
| 62 | + expect(window.location.href).toEqual(expectedHref); |
| 63 | + expect(dis.fire).toHaveBeenCalledTimes(0); |
| 64 | + }); |
| 65 | + |
| 66 | + it("should not capture non-permalink clicks", () => { |
| 67 | + const link = "https://matrix.org"; |
| 68 | + const expectedHref = originalHref; |
| 69 | + runClickTest(`... ${link} ...`, link); |
| 70 | + expect(window.location.href).toEqual(expectedHref); |
| 71 | + expect(dis.fire).toHaveBeenCalledTimes(0); |
| 72 | + }); |
| 73 | + |
| 74 | + it("should open topic dialog when not clicking a link", () => { |
| 75 | + const topic = "foobar"; |
| 76 | + const expectedHref = originalHref; |
| 77 | + runClickTest(topic, topic); |
| 78 | + expect(window.location.href).toEqual(expectedHref); |
| 79 | + expect(dis.fire).toHaveBeenCalledWith(Action.ShowRoomTopic); |
| 80 | + }); |
| 81 | +}); |
0 commit comments