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

Commit 364c453

Browse files
authored
Tests for RoomCreate (#9997)
* Tests for RoomCreate tile * Prefer screen instead of holding the return from render * use userEvent instead of fireEvent
1 parent d84509d commit 364c453

File tree

3 files changed

+116
-1
lines changed

3 files changed

+116
-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: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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 { act, render, screen, waitFor } from "@testing-library/react";
19+
import userEvent from "@testing-library/user-event";
20+
import { mocked } from "jest-mock";
21+
import { EventType, MatrixEvent } from "matrix-js-sdk/src/matrix";
22+
23+
import dis from "../../../../src/dispatcher/dispatcher";
24+
import SettingsStore from "../../../../src/settings/SettingsStore";
25+
import RoomCreate from "../../../../src/components/views/messages/RoomCreate";
26+
import { stubClient } from "../../../test-utils/test-utils";
27+
import { Action } from "../../../../src/dispatcher/actions";
28+
29+
jest.mock("../../../../src/dispatcher/dispatcher");
30+
31+
describe("<RoomCreate />", () => {
32+
const userId = "@alice:server.org";
33+
const roomId = "!room:server.org";
34+
const createEvent = new MatrixEvent({
35+
type: EventType.RoomCreate,
36+
sender: userId,
37+
room_id: roomId,
38+
content: {
39+
predecessor: { room_id: "old_room_id", event_id: "tombstone_event_id" },
40+
},
41+
event_id: "$create",
42+
});
43+
44+
beforeEach(() => {
45+
jest.clearAllMocks();
46+
mocked(dis.dispatch).mockReset();
47+
jest.spyOn(SettingsStore, "getValue").mockReturnValue(false);
48+
jest.spyOn(SettingsStore, "setValue").mockResolvedValue(undefined);
49+
stubClient();
50+
});
51+
52+
afterAll(() => {
53+
jest.spyOn(SettingsStore, "getValue").mockRestore();
54+
jest.spyOn(SettingsStore, "setValue").mockRestore();
55+
});
56+
57+
it("Renders as expected", () => {
58+
const roomCreate = render(<RoomCreate mxEvent={createEvent} />);
59+
expect(roomCreate.asFragment()).toMatchSnapshot();
60+
});
61+
62+
it("Links to the old version of the room", () => {
63+
render(<RoomCreate mxEvent={createEvent} />);
64+
expect(screen.getByText("Click here to see older messages.")).toHaveAttribute(
65+
"href",
66+
"https://matrix.to/#/old_room_id/tombstone_event_id",
67+
);
68+
});
69+
70+
it("Opens the old room on click", async () => {
71+
render(<RoomCreate mxEvent={createEvent} />);
72+
const link = screen.getByText("Click here to see older messages.");
73+
74+
await act(() => userEvent.click(link));
75+
76+
await waitFor(() =>
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+
});
87+
});
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)