Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 9c7c5a4

Browse files
committed
Tests for RoomCreate tile
1 parent 172c666 commit 9c7c5a4

File tree

3 files changed

+115
-1
lines changed

3 files changed

+115
-1
lines changed

src/components/views/messages/RoomCreate.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,15 @@ import EventTileBubble from "./EventTileBubble";
2727
import { ViewRoomPayload } from "../../../dispatcher/payloads/ViewRoomPayload";
2828

2929
interface IProps {
30-
/* the MatrixEvent to show */
30+
/** The m.room.create MatrixEvent that this tile represents */
3131
mxEvent: MatrixEvent;
3232
timestamp?: JSX.Element;
3333
}
3434

35+
/**
36+
* A message tile showing that this room was created as an upgrade of a previous
37+
* room.
38+
*/
3539
export default class RoomCreate extends React.Component<IProps> {
3640
private onLinkClicked = (e: React.MouseEvent): void => {
3741
e.preventDefault();
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
});
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`<RoomCreate /> Renders as expected 1`] = `
4+
<DocumentFragment>
5+
<div
6+
class="mx_EventTileBubble mx_CreateEvent"
7+
>
8+
<div
9+
class="mx_EventTileBubble_title"
10+
>
11+
This room is a continuation of another conversation.
12+
</div>
13+
<div
14+
class="mx_EventTileBubble_subtitle"
15+
>
16+
<a
17+
href="https://matrix.to/#/old_room_id/tombstone_event_id"
18+
>
19+
Click here to see older messages.
20+
</a>
21+
</div>
22+
</div>
23+
</DocumentFragment>
24+
`;

0 commit comments

Comments
 (0)