2021-10-14 21:27:35 +08:00
|
|
|
/*
|
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2023-01-12 18:54:03 +08:00
|
|
|
import { fireEvent, render, screen, waitFor } from "@testing-library/react";
|
2022-12-12 19:24:14 +08:00
|
|
|
import "focus-visible"; // to fix context menus
|
2023-01-12 18:54:03 +08:00
|
|
|
import { mocked } from "jest-mock";
|
|
|
|
import { MatrixClient, MatrixEvent, PendingEventOrdering, Room } from "matrix-js-sdk/src/matrix";
|
|
|
|
import { FeatureSupport, Thread } from "matrix-js-sdk/src/models/thread";
|
|
|
|
import React from "react";
|
2021-10-14 21:27:35 +08:00
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
import ThreadPanel, { ThreadFilterType, ThreadPanelHeader } from "../../../src/components/structures/ThreadPanel";
|
2023-01-12 18:54:03 +08:00
|
|
|
import MatrixClientContext from "../../../src/contexts/MatrixClientContext";
|
|
|
|
import RoomContext from "../../../src/contexts/RoomContext";
|
2022-12-12 19:24:14 +08:00
|
|
|
import { _t } from "../../../src/languageHandler";
|
2023-01-12 18:54:03 +08:00
|
|
|
import { MatrixClientPeg } from "../../../src/MatrixClientPeg";
|
2022-09-22 22:08:14 +08:00
|
|
|
import { shouldShowFeedback } from "../../../src/utils/Feedback";
|
2023-01-12 18:54:03 +08:00
|
|
|
import { RoomPermalinkCreator } from "../../../src/utils/permalinks/Permalinks";
|
|
|
|
import ResizeNotifier from "../../../src/utils/ResizeNotifier";
|
|
|
|
import { createTestClient, getRoomContext, mkStubRoom, mockPlatformPeg, stubClient } from "../../test-utils";
|
|
|
|
import { mkThread } from "../../test-utils/threads";
|
2022-09-22 22:08:14 +08:00
|
|
|
|
|
|
|
jest.mock("../../../src/utils/Feedback");
|
2021-10-14 21:27:35 +08:00
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
describe("ThreadPanel", () => {
|
2022-09-22 22:08:14 +08:00
|
|
|
describe("Feedback prompt", () => {
|
|
|
|
const cli = createTestClient();
|
|
|
|
const room = mkStubRoom("!room:server", "room", cli);
|
|
|
|
mocked(cli.getRoom).mockReturnValue(room);
|
|
|
|
|
|
|
|
it("should show feedback prompt if feedback is enabled", () => {
|
|
|
|
mocked(shouldShowFeedback).mockReturnValue(true);
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
render(
|
|
|
|
<MatrixClientContext.Provider value={cli}>
|
|
|
|
<ThreadPanel
|
|
|
|
roomId="!room:server"
|
|
|
|
onClose={jest.fn()}
|
|
|
|
resizeNotifier={new ResizeNotifier()}
|
|
|
|
permalinkCreator={new RoomPermalinkCreator(room)}
|
|
|
|
/>
|
|
|
|
</MatrixClientContext.Provider>,
|
|
|
|
);
|
2022-09-22 22:08:14 +08:00
|
|
|
expect(screen.queryByText("Give feedback")).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should hide feedback prompt if feedback is disabled", () => {
|
|
|
|
mocked(shouldShowFeedback).mockReturnValue(false);
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
render(
|
|
|
|
<MatrixClientContext.Provider value={cli}>
|
|
|
|
<ThreadPanel
|
|
|
|
roomId="!room:server"
|
|
|
|
onClose={jest.fn()}
|
|
|
|
resizeNotifier={new ResizeNotifier()}
|
|
|
|
permalinkCreator={new RoomPermalinkCreator(room)}
|
|
|
|
/>
|
|
|
|
</MatrixClientContext.Provider>,
|
|
|
|
);
|
2022-09-22 22:08:14 +08:00
|
|
|
expect(screen.queryByText("Give feedback")).toBeFalsy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
describe("Header", () => {
|
|
|
|
it("expect that All filter for ThreadPanelHeader properly renders Show: All threads", () => {
|
2022-09-22 22:08:14 +08:00
|
|
|
const { asFragment } = render(
|
2021-10-14 21:27:35 +08:00
|
|
|
<ThreadPanelHeader
|
2022-02-24 16:44:34 +08:00
|
|
|
empty={false}
|
2021-10-14 21:27:35 +08:00
|
|
|
filterOption={ThreadFilterType.All}
|
2022-12-12 19:24:14 +08:00
|
|
|
setFilterOption={() => undefined}
|
|
|
|
/>,
|
2021-10-14 21:27:35 +08:00
|
|
|
);
|
2022-09-22 22:08:14 +08:00
|
|
|
expect(asFragment()).toMatchSnapshot();
|
2021-10-14 21:27:35 +08:00
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("expect that My filter for ThreadPanelHeader properly renders Show: My threads", () => {
|
2022-09-22 22:08:14 +08:00
|
|
|
const { asFragment } = render(
|
2021-10-14 21:27:35 +08:00
|
|
|
<ThreadPanelHeader
|
2022-02-24 16:44:34 +08:00
|
|
|
empty={false}
|
2021-10-14 21:27:35 +08:00
|
|
|
filterOption={ThreadFilterType.My}
|
2022-12-12 19:24:14 +08:00
|
|
|
setFilterOption={() => undefined}
|
|
|
|
/>,
|
2021-10-14 21:27:35 +08:00
|
|
|
);
|
2022-09-22 22:08:14 +08:00
|
|
|
expect(asFragment()).toMatchSnapshot();
|
2021-10-14 21:27:35 +08:00
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("expect that ThreadPanelHeader properly opens a context menu when clicked on the button", () => {
|
2022-09-22 22:08:14 +08:00
|
|
|
const { container } = render(
|
2021-10-14 21:27:35 +08:00
|
|
|
<ThreadPanelHeader
|
2022-02-24 16:44:34 +08:00
|
|
|
empty={false}
|
2021-10-14 21:27:35 +08:00
|
|
|
filterOption={ThreadFilterType.All}
|
2022-12-12 19:24:14 +08:00
|
|
|
setFilterOption={() => undefined}
|
|
|
|
/>,
|
2021-10-14 21:27:35 +08:00
|
|
|
);
|
2022-09-22 22:08:14 +08:00
|
|
|
const found = container.querySelector(".mx_ThreadPanel_dropdown");
|
|
|
|
expect(found).toBeTruthy();
|
|
|
|
expect(screen.queryByRole("menu")).toBeFalsy();
|
|
|
|
fireEvent.click(found);
|
|
|
|
expect(screen.queryByRole("menu")).toBeTruthy();
|
2021-10-14 21:27:35 +08:00
|
|
|
});
|
|
|
|
|
2022-12-12 19:24:14 +08:00
|
|
|
it("expect that ThreadPanelHeader has the correct option selected in the context menu", () => {
|
2022-09-22 22:08:14 +08:00
|
|
|
const { container } = render(
|
2021-10-14 21:27:35 +08:00
|
|
|
<ThreadPanelHeader
|
2022-02-24 16:44:34 +08:00
|
|
|
empty={false}
|
2021-10-14 21:27:35 +08:00
|
|
|
filterOption={ThreadFilterType.All}
|
2022-12-12 19:24:14 +08:00
|
|
|
setFilterOption={() => undefined}
|
|
|
|
/>,
|
2021-10-14 21:27:35 +08:00
|
|
|
);
|
2022-09-22 22:08:14 +08:00
|
|
|
fireEvent.click(container.querySelector(".mx_ThreadPanel_dropdown"));
|
|
|
|
const found = screen.queryAllByRole("menuitemradio");
|
|
|
|
expect(found).toHaveLength(2);
|
|
|
|
const foundButton = screen.queryByRole("menuitemradio", { checked: true });
|
2022-12-12 19:24:14 +08:00
|
|
|
expect(foundButton.textContent).toEqual(`${_t("All threads")}${_t("Shows all threads from current room")}`);
|
2021-10-14 21:27:35 +08:00
|
|
|
expect(foundButton).toMatchSnapshot();
|
|
|
|
});
|
|
|
|
});
|
2023-01-12 18:54:03 +08:00
|
|
|
|
|
|
|
describe("Filtering", () => {
|
|
|
|
const ROOM_ID = "!roomId:example.org";
|
|
|
|
const SENDER = "@alice:example.org";
|
|
|
|
|
|
|
|
let mockClient: MatrixClient;
|
|
|
|
let room: Room;
|
|
|
|
|
|
|
|
const TestThreadPanel = () => (
|
|
|
|
<MatrixClientContext.Provider value={mockClient}>
|
|
|
|
<RoomContext.Provider
|
|
|
|
value={getRoomContext(room, {
|
|
|
|
canSendMessages: true,
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
<ThreadPanel
|
|
|
|
roomId={ROOM_ID}
|
|
|
|
onClose={jest.fn()}
|
|
|
|
resizeNotifier={new ResizeNotifier()}
|
|
|
|
permalinkCreator={new RoomPermalinkCreator(room)}
|
|
|
|
/>
|
|
|
|
</RoomContext.Provider>
|
|
|
|
</MatrixClientContext.Provider>
|
|
|
|
);
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
jest.clearAllMocks();
|
|
|
|
|
|
|
|
stubClient();
|
|
|
|
mockPlatformPeg();
|
|
|
|
mockClient = mocked(MatrixClientPeg.get());
|
|
|
|
Thread.setServerSideSupport(FeatureSupport.Stable);
|
|
|
|
Thread.setServerSideListSupport(FeatureSupport.Stable);
|
|
|
|
Thread.setServerSideFwdPaginationSupport(FeatureSupport.Stable);
|
|
|
|
jest.spyOn(mockClient, "supportsExperimentalThreads").mockReturnValue(true);
|
|
|
|
|
|
|
|
room = new Room(ROOM_ID, mockClient, mockClient.getUserId() ?? "", {
|
|
|
|
pendingEventOrdering: PendingEventOrdering.Detached,
|
|
|
|
});
|
|
|
|
jest.spyOn(room, "fetchRoomThreads").mockReturnValue(Promise.resolve());
|
|
|
|
jest.spyOn(mockClient, "getRoom").mockReturnValue(room);
|
|
|
|
await room.createThreadsTimelineSets();
|
|
|
|
const [allThreads, myThreads] = room.threadsTimelineSets;
|
|
|
|
jest.spyOn(room, "createThreadsTimelineSets").mockReturnValue(Promise.resolve([allThreads, myThreads]));
|
|
|
|
});
|
|
|
|
|
|
|
|
function toggleThreadFilter(container: HTMLElement, newFilter: ThreadFilterType) {
|
|
|
|
fireEvent.click(container.querySelector(".mx_ThreadPanel_dropdown")!);
|
|
|
|
const found = screen.queryAllByRole("menuitemradio");
|
|
|
|
expect(found).toHaveLength(2);
|
|
|
|
|
|
|
|
const allThreadsContent = `${_t("All threads")}${_t("Shows all threads from current room")}`;
|
|
|
|
const myThreadsContent = `${_t("My threads")}${_t("Shows all threads you've participated in")}`;
|
|
|
|
|
|
|
|
const allThreadsOption = found.find((it) => it.textContent === allThreadsContent);
|
|
|
|
const myThreadsOption = found.find((it) => it.textContent === myThreadsContent);
|
|
|
|
expect(allThreadsOption).toBeTruthy();
|
|
|
|
expect(myThreadsOption).toBeTruthy();
|
|
|
|
|
|
|
|
const toSelect = newFilter === ThreadFilterType.My ? myThreadsOption : allThreadsOption;
|
|
|
|
fireEvent.click(toSelect!);
|
|
|
|
}
|
|
|
|
|
|
|
|
type EventData = { sender: string | null; content: string | null };
|
|
|
|
|
|
|
|
function findEvents(container: HTMLElement): EventData[] {
|
|
|
|
return Array.from(container.querySelectorAll(".mx_EventTile")).map((el) => {
|
|
|
|
const sender = el.querySelector(".mx_DisambiguatedProfile_displayName")?.textContent ?? null;
|
|
|
|
const content = el.querySelector(".mx_EventTile_body")?.textContent ?? null;
|
|
|
|
return { sender, content };
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function toEventData(event: MatrixEvent): EventData {
|
|
|
|
return { sender: event.event.sender ?? null, content: event.event.content?.body ?? null };
|
|
|
|
}
|
|
|
|
|
|
|
|
it("correctly filters Thread List with multiple threads", async () => {
|
|
|
|
const otherThread = mkThread({
|
|
|
|
room,
|
|
|
|
client: mockClient,
|
|
|
|
authorId: SENDER,
|
|
|
|
participantUserIds: [mockClient.getUserId()!],
|
|
|
|
});
|
|
|
|
|
|
|
|
const mixedThread = mkThread({
|
|
|
|
room,
|
|
|
|
client: mockClient,
|
|
|
|
authorId: SENDER,
|
|
|
|
participantUserIds: [SENDER, mockClient.getUserId()!],
|
|
|
|
});
|
|
|
|
|
|
|
|
const ownThread = mkThread({
|
|
|
|
room,
|
|
|
|
client: mockClient,
|
|
|
|
authorId: mockClient.getUserId()!,
|
|
|
|
participantUserIds: [mockClient.getUserId()!],
|
|
|
|
});
|
|
|
|
|
|
|
|
const threadRoots = [otherThread.rootEvent, mixedThread.rootEvent, ownThread.rootEvent];
|
|
|
|
jest.spyOn(mockClient, "fetchRoomEvent").mockImplementation((_, eventId) => {
|
|
|
|
const event = threadRoots.find((it) => it.getId() === eventId)?.event;
|
|
|
|
return event ? Promise.resolve(event) : Promise.reject();
|
|
|
|
});
|
|
|
|
const [allThreads, myThreads] = room.threadsTimelineSets;
|
|
|
|
allThreads.addLiveEvent(otherThread.rootEvent);
|
|
|
|
allThreads.addLiveEvent(mixedThread.rootEvent);
|
|
|
|
allThreads.addLiveEvent(ownThread.rootEvent);
|
|
|
|
myThreads.addLiveEvent(mixedThread.rootEvent);
|
|
|
|
myThreads.addLiveEvent(ownThread.rootEvent);
|
|
|
|
|
|
|
|
let events: EventData[] = [];
|
|
|
|
const renderResult = render(<TestThreadPanel />);
|
|
|
|
await waitFor(() => expect(renderResult.container.querySelector(".mx_AutoHideScrollbar")).toBeFalsy());
|
|
|
|
await waitFor(() => {
|
|
|
|
events = findEvents(renderResult.container);
|
|
|
|
expect(findEvents(renderResult.container)).toHaveLength(3);
|
|
|
|
});
|
|
|
|
expect(events[0]).toEqual(toEventData(otherThread.rootEvent));
|
|
|
|
expect(events[1]).toEqual(toEventData(mixedThread.rootEvent));
|
|
|
|
expect(events[2]).toEqual(toEventData(ownThread.rootEvent));
|
|
|
|
await waitFor(() => expect(renderResult.container.querySelector(".mx_ThreadPanel_dropdown")).toBeTruthy());
|
|
|
|
toggleThreadFilter(renderResult.container, ThreadFilterType.My);
|
|
|
|
await waitFor(() => expect(renderResult.container.querySelector(".mx_AutoHideScrollbar")).toBeFalsy());
|
|
|
|
await waitFor(() => {
|
|
|
|
events = findEvents(renderResult.container);
|
|
|
|
expect(findEvents(renderResult.container)).toHaveLength(2);
|
|
|
|
});
|
|
|
|
expect(events[0]).toEqual(toEventData(mixedThread.rootEvent));
|
|
|
|
expect(events[1]).toEqual(toEventData(ownThread.rootEvent));
|
|
|
|
toggleThreadFilter(renderResult.container, ThreadFilterType.All);
|
|
|
|
await waitFor(() => expect(renderResult.container.querySelector(".mx_AutoHideScrollbar")).toBeFalsy());
|
|
|
|
await waitFor(() => {
|
|
|
|
events = findEvents(renderResult.container);
|
|
|
|
expect(findEvents(renderResult.container)).toHaveLength(3);
|
|
|
|
});
|
|
|
|
expect(events[0]).toEqual(toEventData(otherThread.rootEvent));
|
|
|
|
expect(events[1]).toEqual(toEventData(mixedThread.rootEvent));
|
|
|
|
expect(events[2]).toEqual(toEventData(ownThread.rootEvent));
|
|
|
|
});
|
|
|
|
|
|
|
|
it("correctly filters Thread List with a single, unparticipated thread", async () => {
|
|
|
|
const otherThread = mkThread({
|
|
|
|
room,
|
|
|
|
client: mockClient,
|
|
|
|
authorId: SENDER,
|
|
|
|
participantUserIds: [mockClient.getUserId()!],
|
|
|
|
});
|
|
|
|
|
|
|
|
const threadRoots = [otherThread.rootEvent];
|
|
|
|
jest.spyOn(mockClient, "fetchRoomEvent").mockImplementation((_, eventId) => {
|
|
|
|
const event = threadRoots.find((it) => it.getId() === eventId)?.event;
|
|
|
|
return event ? Promise.resolve(event) : Promise.reject();
|
|
|
|
});
|
|
|
|
const [allThreads] = room.threadsTimelineSets;
|
|
|
|
allThreads.addLiveEvent(otherThread.rootEvent);
|
|
|
|
|
|
|
|
let events: EventData[] = [];
|
|
|
|
const renderResult = render(<TestThreadPanel />);
|
|
|
|
await waitFor(() => expect(renderResult.container.querySelector(".mx_AutoHideScrollbar")).toBeFalsy());
|
|
|
|
await waitFor(() => {
|
|
|
|
events = findEvents(renderResult.container);
|
|
|
|
expect(findEvents(renderResult.container)).toHaveLength(1);
|
|
|
|
});
|
|
|
|
expect(events[0]).toEqual(toEventData(otherThread.rootEvent));
|
|
|
|
await waitFor(() => expect(renderResult.container.querySelector(".mx_ThreadPanel_dropdown")).toBeTruthy());
|
|
|
|
toggleThreadFilter(renderResult.container, ThreadFilterType.My);
|
|
|
|
await waitFor(() => expect(renderResult.container.querySelector(".mx_AutoHideScrollbar")).toBeFalsy());
|
|
|
|
await waitFor(() => {
|
|
|
|
events = findEvents(renderResult.container);
|
|
|
|
expect(findEvents(renderResult.container)).toHaveLength(0);
|
|
|
|
});
|
|
|
|
toggleThreadFilter(renderResult.container, ThreadFilterType.All);
|
|
|
|
await waitFor(() => expect(renderResult.container.querySelector(".mx_AutoHideScrollbar")).toBeFalsy());
|
|
|
|
await waitFor(() => {
|
|
|
|
events = findEvents(renderResult.container);
|
|
|
|
expect(findEvents(renderResult.container)).toHaveLength(1);
|
|
|
|
});
|
|
|
|
expect(events[0]).toEqual(toEventData(otherThread.rootEvent));
|
|
|
|
});
|
|
|
|
});
|
2021-10-14 21:27:35 +08:00
|
|
|
});
|