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

Commit

Permalink
Add tests for RoomTopic
Browse files Browse the repository at this point in the history
  • Loading branch information
florianduros committed May 20, 2024
1 parent 91f848e commit cc6aec1
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions test/components/views/elements/RoomTopic-test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ limitations under the License.

import React from "react";
import { Room } from "matrix-js-sdk/src/matrix";
import { fireEvent, render, screen } from "@testing-library/react";
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
import userEvent from "@testing-library/user-event";

import { mkEvent, stubClient } from "../../../test-utils";
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
Expand All @@ -33,9 +34,12 @@ describe("<RoomTopic/>", () => {
window.location.href = originalHref;
});

function runClickTest(topic: string, clickText: string) {
/**
* Create a room with the given topic
* @param topic
*/
function createRoom(topic: string) {
stubClient();

const room = new Room("!pMBteVpcoJRdCJxDmn:matrix.org", MatrixClientPeg.safeGet(), "@alice:example.org");
const topicEvent = mkEvent({
type: "m.room.topic",
Expand All @@ -45,11 +49,27 @@ describe("<RoomTopic/>", () => {
ts: 123,
event: true,
});

room.addLiveEvents([topicEvent]);

return room;
}

/**
* Create a room and render it
* @param topic
*/
const renderRoom = (topic: string) => {
const room = createRoom(topic);
render(<RoomTopic room={room} />);
};

/**
* Create a room and click on the given text
* @param topic
* @param clickText
*/
function runClickTest(topic: string, clickText: string) {
renderRoom(topic);
fireEvent.click(screen.getByText(clickText));
}

Expand Down Expand Up @@ -78,4 +98,18 @@ describe("<RoomTopic/>", () => {
expect(window.location.href).toEqual(expectedHref);
expect(dis.fire).toHaveBeenCalledWith(Action.ShowRoomTopic);
});

it("should open the tooltip when hovering a text", async () => {
const topic = "room topic";
renderRoom(topic);
await userEvent.hover(screen.getByText(topic));
await waitFor(() => expect(screen.getByRole("tooltip", { name: "Click to read topic" })).toBeInTheDocument());
});

it("should not open the tooltip when hovering a link", async () => {
const topic = "https://matrix.org";
renderRoom(topic);
await userEvent.hover(screen.getByText(topic));
await waitFor(() => expect(screen.queryByRole("tooltip", { name: "Click to read topic" })).toBeNull());
});
});

0 comments on commit cc6aec1

Please sign in to comment.