|
| 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 { fireEvent, render, RenderResult } from "@testing-library/react"; |
| 19 | +import { mocked } from "jest-mock"; |
| 20 | +import { EventType, MatrixEvent } from "matrix-js-sdk/src/matrix"; |
| 21 | + |
| 22 | +import dis from "../../../../src/dispatcher/dispatcher"; |
| 23 | +import SettingsStore from "../../../../src/settings/SettingsStore"; |
| 24 | +import RoomCreate from "../../../../src/components/views/messages/RoomCreate"; |
| 25 | +import { stubClient } from "../../../test-utils/test-utils"; |
| 26 | +import { Action } from "../../../../src/dispatcher/actions"; |
| 27 | + |
| 28 | +jest.mock("../../../../src/dispatcher/dispatcher"); |
| 29 | + |
| 30 | +describe("<RoomCreate />", () => { |
| 31 | + const userId = "@alice:server.org"; |
| 32 | + const roomId = "!room:server.org"; |
| 33 | + const createEvent = new MatrixEvent({ |
| 34 | + type: EventType.RoomCreate, |
| 35 | + sender: userId, |
| 36 | + room_id: roomId, |
| 37 | + content: { |
| 38 | + predecessor: { room_id: "old_room_id", event_id: "tombstone_event_id" }, |
| 39 | + }, |
| 40 | + event_id: "$create", |
| 41 | + }); |
| 42 | + |
| 43 | + function getComponent(event: MatrixEvent): RenderResult { |
| 44 | + return render(<RoomCreate mxEvent={event} />); |
| 45 | + } |
| 46 | + |
| 47 | + beforeEach(() => { |
| 48 | + jest.clearAllMocks(); |
| 49 | + mocked(dis.dispatch).mockReset(); |
| 50 | + jest.spyOn(SettingsStore, "getValue").mockReturnValue(false); |
| 51 | + jest.spyOn(SettingsStore, "setValue").mockResolvedValue(undefined); |
| 52 | + stubClient(); |
| 53 | + }); |
| 54 | + |
| 55 | + afterAll(() => { |
| 56 | + jest.spyOn(SettingsStore, "getValue").mockRestore(); |
| 57 | + jest.spyOn(SettingsStore, "setValue").mockRestore(); |
| 58 | + }); |
| 59 | + |
| 60 | + it("Renders as expected", () => { |
| 61 | + const wrapper = getComponent(createEvent); |
| 62 | + expect(wrapper.asFragment()).toMatchSnapshot(); |
| 63 | + }); |
| 64 | + |
| 65 | + it("Links to the old version of the room", () => { |
| 66 | + const wrapper = getComponent(createEvent); |
| 67 | + expect(wrapper.getByText("Click here to see older messages.")).toHaveAttribute( |
| 68 | + "href", |
| 69 | + "https://matrix.to/#/old_room_id/tombstone_event_id", |
| 70 | + ); |
| 71 | + }); |
| 72 | + |
| 73 | + it("Opens the old room on click", () => { |
| 74 | + const wrapper = getComponent(createEvent); |
| 75 | + const link = wrapper.getByText("Click here to see older messages."); |
| 76 | + fireEvent.click(link); |
| 77 | + expect(dis.dispatch).toHaveBeenCalledWith({ |
| 78 | + action: Action.ViewRoom, |
| 79 | + event_id: "tombstone_event_id", |
| 80 | + highlighted: true, |
| 81 | + room_id: "old_room_id", |
| 82 | + metricsTrigger: "Predecessor", |
| 83 | + metricsViaKeyboard: false, |
| 84 | + }); |
| 85 | + }); |
| 86 | +}); |
0 commit comments