2022-06-15 14:14:05 +00:00
|
|
|
/*
|
2024-09-09 13:57:16 +00:00
|
|
|
Copyright 2024 New Vector Ltd.
|
|
|
|
Copyright 2022, 2023 The Matrix.org Foundation C.I.C.
|
2022-06-15 14:14:05 +00:00
|
|
|
|
2024-09-09 13:57:16 +00:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2022-06-15 14:14:05 +00:00
|
|
|
*/
|
|
|
|
|
2023-02-21 08:06:43 +00:00
|
|
|
import React from "react";
|
2022-07-20 07:26:25 +00:00
|
|
|
import { mocked } from "jest-mock";
|
2023-08-08 14:08:17 +00:00
|
|
|
import {
|
|
|
|
ConnectionError,
|
|
|
|
IProtocol,
|
|
|
|
IPublicRoomsChunkRoom,
|
2023-08-30 06:54:26 +00:00
|
|
|
JoinRule,
|
2023-08-08 14:08:17 +00:00
|
|
|
MatrixClient,
|
|
|
|
Room,
|
|
|
|
RoomMember,
|
|
|
|
} from "matrix-js-sdk/src/matrix";
|
2024-03-18 14:40:52 +00:00
|
|
|
import { KnownMembership } from "matrix-js-sdk/src/types";
|
2022-06-15 14:14:05 +00:00
|
|
|
import sanitizeHtml from "sanitize-html";
|
2024-10-14 16:11:58 +00:00
|
|
|
import { fireEvent, render, screen } from "jest-matrix-react";
|
2022-06-15 14:14:05 +00:00
|
|
|
|
2024-10-15 13:57:26 +00:00
|
|
|
import SpotlightDialog from "../../../../../src/components/views/dialogs/spotlight/SpotlightDialog";
|
|
|
|
import { Filter } from "../../../../../src/components/views/dialogs/spotlight/Filter";
|
|
|
|
import { MatrixClientPeg } from "../../../../../src/MatrixClientPeg";
|
|
|
|
import { LocalRoom, LOCAL_ROOM_ID_PREFIX } from "../../../../../src/models/LocalRoom";
|
|
|
|
import { DirectoryMember, startDmOnFirstMessage } from "../../../../../src/utils/direct-messages";
|
|
|
|
import DMRoomMap from "../../../../../src/utils/DMRoomMap";
|
|
|
|
import { flushPromisesWithFakeTimers, mkRoom, stubClient } from "../../../../test-utils";
|
|
|
|
import SettingsStore from "../../../../../src/settings/SettingsStore";
|
|
|
|
import { SettingLevel } from "../../../../../src/settings/SettingLevel";
|
|
|
|
import defaultDispatcher from "../../../../../src/dispatcher/dispatcher";
|
|
|
|
import SdkConfig from "../../../../../src/SdkConfig";
|
|
|
|
import { Action } from "../../../../../src/dispatcher/actions";
|
2022-09-22 14:08:14 +00:00
|
|
|
|
2023-02-21 08:06:43 +00:00
|
|
|
jest.useFakeTimers();
|
|
|
|
|
2024-10-15 13:57:26 +00:00
|
|
|
jest.mock("../../../../../src/utils/Feedback");
|
2022-07-20 07:26:25 +00:00
|
|
|
|
2024-10-15 13:57:26 +00:00
|
|
|
jest.mock("../../../../../src/utils/direct-messages", () => ({
|
2022-07-20 07:26:25 +00:00
|
|
|
// @ts-ignore
|
2024-10-15 13:57:26 +00:00
|
|
|
...jest.requireActual("../../../../../src/utils/direct-messages"),
|
2022-07-20 07:26:25 +00:00
|
|
|
startDmOnFirstMessage: jest.fn(),
|
|
|
|
}));
|
2022-06-15 14:14:05 +00:00
|
|
|
|
2024-10-15 13:57:26 +00:00
|
|
|
jest.mock("../../../../../src/dispatcher/dispatcher", () => ({
|
2023-03-27 07:39:34 +00:00
|
|
|
register: jest.fn(),
|
|
|
|
dispatch: jest.fn(),
|
|
|
|
}));
|
|
|
|
|
2022-06-15 14:14:05 +00:00
|
|
|
interface IUserChunkMember {
|
|
|
|
user_id: string;
|
|
|
|
display_name?: string;
|
|
|
|
avatar_url?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface MockClientOptions {
|
|
|
|
userId?: string;
|
|
|
|
homeserver?: string;
|
|
|
|
thirdPartyProtocols?: Record<string, IProtocol>;
|
|
|
|
rooms?: IPublicRoomsChunkRoom[];
|
|
|
|
members?: RoomMember[];
|
|
|
|
users?: IUserChunkMember[];
|
|
|
|
}
|
|
|
|
|
|
|
|
function mockClient({
|
|
|
|
userId = "testuser",
|
|
|
|
homeserver = "example.tld",
|
|
|
|
thirdPartyProtocols = {},
|
|
|
|
rooms = [],
|
|
|
|
members = [],
|
|
|
|
users = [],
|
|
|
|
}: MockClientOptions = {}): MatrixClient {
|
|
|
|
stubClient();
|
2023-06-05 17:12:23 +00:00
|
|
|
const cli = MatrixClientPeg.safeGet();
|
2022-06-15 14:14:05 +00:00
|
|
|
cli.getUserId = jest.fn(() => userId);
|
2024-05-28 07:41:20 +00:00
|
|
|
cli.getDomain = jest.fn(() => homeserver);
|
2022-06-15 14:14:05 +00:00
|
|
|
cli.getHomeserverUrl = jest.fn(() => homeserver);
|
|
|
|
cli.getThirdpartyProtocols = jest.fn(() => Promise.resolve(thirdPartyProtocols));
|
|
|
|
cli.publicRooms = jest.fn((options) => {
|
|
|
|
const searchTerm = options?.filter?.generic_search_term?.toLowerCase();
|
|
|
|
const chunk = rooms.filter(
|
|
|
|
(it) =>
|
|
|
|
!searchTerm ||
|
|
|
|
it.room_id.toLowerCase().includes(searchTerm) ||
|
|
|
|
it.name?.toLowerCase().includes(searchTerm) ||
|
2023-02-21 08:06:43 +00:00
|
|
|
sanitizeHtml(it?.topic || "", { allowedTags: [] })
|
|
|
|
.toLowerCase()
|
|
|
|
.includes(searchTerm) ||
|
2022-06-15 14:14:05 +00:00
|
|
|
it.canonical_alias?.toLowerCase().includes(searchTerm) ||
|
|
|
|
it.aliases?.find((alias) => alias.toLowerCase().includes(searchTerm)),
|
|
|
|
);
|
|
|
|
return Promise.resolve({
|
|
|
|
chunk,
|
|
|
|
total_room_count_estimate: chunk.length,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
cli.searchUserDirectory = jest.fn(({ term, limit }) => {
|
|
|
|
const searchTerm = term?.toLowerCase();
|
|
|
|
const results = users.filter(
|
|
|
|
(it) =>
|
|
|
|
!searchTerm ||
|
|
|
|
it.user_id.toLowerCase().includes(searchTerm) ||
|
2023-02-15 13:36:22 +00:00
|
|
|
it.display_name?.toLowerCase().includes(searchTerm),
|
2022-06-15 14:14:05 +00:00
|
|
|
);
|
|
|
|
return Promise.resolve({
|
|
|
|
results: results.slice(0, limit ?? +Infinity),
|
2023-02-24 15:28:40 +00:00
|
|
|
limited: !!limit && limit < results.length,
|
2022-06-15 14:14:05 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
cli.getProfileInfo = jest.fn(async (userId) => {
|
|
|
|
const member = members.find((it) => it.userId === userId);
|
|
|
|
if (member) {
|
|
|
|
return Promise.resolve({
|
|
|
|
displayname: member.rawDisplayName,
|
|
|
|
avatar_url: member.getMxcAvatarUrl(),
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return Promise.reject();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return cli;
|
|
|
|
}
|
|
|
|
|
|
|
|
describe("Spotlight Dialog", () => {
|
|
|
|
const testPerson: IUserChunkMember = {
|
|
|
|
user_id: "@janedoe:matrix.org",
|
|
|
|
display_name: "Jane Doe",
|
|
|
|
avatar_url: undefined,
|
|
|
|
};
|
|
|
|
|
|
|
|
const testPublicRoom: IPublicRoomsChunkRoom = {
|
2023-03-27 07:39:34 +00:00
|
|
|
room_id: "!room247:matrix.org",
|
2022-06-15 14:14:05 +00:00
|
|
|
name: "Room #247",
|
|
|
|
topic: "We hope you'll have a <b>shining</b> experience!",
|
|
|
|
world_readable: false,
|
|
|
|
num_joined_members: 1,
|
|
|
|
guest_can_join: false,
|
|
|
|
};
|
|
|
|
|
2023-08-10 17:27:24 +00:00
|
|
|
const testDMRoomId = "!testDM:example.com";
|
|
|
|
const testDMUserId = "@alice:matrix.org";
|
|
|
|
|
2022-07-20 07:26:25 +00:00
|
|
|
let testRoom: Room;
|
2023-08-10 17:27:24 +00:00
|
|
|
let testDM: Room;
|
2022-07-20 07:26:25 +00:00
|
|
|
let testLocalRoom: LocalRoom;
|
|
|
|
|
|
|
|
let mockedClient: MatrixClient;
|
|
|
|
|
2022-06-15 14:14:05 +00:00
|
|
|
beforeEach(() => {
|
2022-07-20 07:26:25 +00:00
|
|
|
mockedClient = mockClient({ rooms: [testPublicRoom], users: [testPerson] });
|
|
|
|
testRoom = mkRoom(mockedClient, "!test23:example.com");
|
2024-03-12 14:52:54 +00:00
|
|
|
mocked(testRoom.getMyMembership).mockReturnValue(KnownMembership.Join);
|
2023-02-15 13:36:22 +00:00
|
|
|
testLocalRoom = new LocalRoom(LOCAL_ROOM_ID_PREFIX + "test23", mockedClient, mockedClient.getUserId()!);
|
2024-03-12 14:52:54 +00:00
|
|
|
testLocalRoom.updateMyMembership(KnownMembership.Join);
|
2022-07-20 07:26:25 +00:00
|
|
|
mocked(mockedClient.getVisibleRooms).mockReturnValue([testRoom, testLocalRoom]);
|
|
|
|
|
|
|
|
jest.spyOn(DMRoomMap, "shared").mockReturnValue({
|
|
|
|
getUserIdForRoomId: jest.fn(),
|
|
|
|
} as unknown as DMRoomMap);
|
2023-08-10 17:27:24 +00:00
|
|
|
|
|
|
|
testDM = mkRoom(mockedClient, testDMRoomId);
|
|
|
|
testDM.name = "Chat with Alice";
|
2024-03-12 14:52:54 +00:00
|
|
|
mocked(testDM.getMyMembership).mockReturnValue(KnownMembership.Join);
|
2023-08-10 17:27:24 +00:00
|
|
|
|
|
|
|
mocked(DMRoomMap.shared().getUserIdForRoomId).mockImplementation((roomId: string) => {
|
|
|
|
if (roomId === testDMRoomId) {
|
|
|
|
return testDMUserId;
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
});
|
|
|
|
|
|
|
|
mocked(mockedClient.getVisibleRooms).mockReturnValue([testRoom, testLocalRoom, testDM]);
|
2022-06-15 14:14:05 +00:00
|
|
|
});
|
2022-09-22 14:08:14 +00:00
|
|
|
|
2022-06-15 14:14:05 +00:00
|
|
|
describe("should apply filters supplied via props", () => {
|
|
|
|
it("without filter", async () => {
|
2023-02-21 08:06:43 +00:00
|
|
|
render(<SpotlightDialog onFinished={() => null} />);
|
2022-06-15 14:14:05 +00:00
|
|
|
|
2023-02-21 08:06:43 +00:00
|
|
|
const filterChip = document.querySelector("div.mx_SpotlightDialog_filter");
|
|
|
|
expect(filterChip).not.toBeInTheDocument();
|
2022-06-15 14:14:05 +00:00
|
|
|
});
|
2023-02-21 08:06:43 +00:00
|
|
|
|
2022-06-15 14:14:05 +00:00
|
|
|
it("with public room filter", async () => {
|
2023-02-21 08:06:43 +00:00
|
|
|
render(<SpotlightDialog initialFilter={Filter.PublicRooms} onFinished={() => null} />);
|
2022-06-15 14:14:05 +00:00
|
|
|
|
2023-02-21 08:06:43 +00:00
|
|
|
// search is debounced
|
|
|
|
jest.advanceTimersByTime(200);
|
|
|
|
await flushPromisesWithFakeTimers();
|
2022-06-15 14:14:05 +00:00
|
|
|
|
2023-02-21 08:06:43 +00:00
|
|
|
const filterChip = document.querySelector("div.mx_SpotlightDialog_filter")!;
|
|
|
|
expect(filterChip).toBeInTheDocument();
|
|
|
|
expect(filterChip.innerHTML).toContain("Public rooms");
|
2022-06-15 14:14:05 +00:00
|
|
|
|
2023-02-21 08:06:43 +00:00
|
|
|
const content = document.querySelector("#mx_SpotlightDialog_content")!;
|
2023-04-20 17:13:30 +00:00
|
|
|
const options = content.querySelectorAll("li.mx_SpotlightDialog_option");
|
2023-02-21 08:06:43 +00:00
|
|
|
expect(options.length).toBe(1);
|
|
|
|
expect(options[0].innerHTML).toContain(testPublicRoom.name);
|
2022-06-15 14:14:05 +00:00
|
|
|
});
|
2023-02-21 08:06:43 +00:00
|
|
|
|
2022-06-15 14:14:05 +00:00
|
|
|
it("with people filter", async () => {
|
2023-02-21 08:06:43 +00:00
|
|
|
render(
|
2022-06-15 14:14:05 +00:00
|
|
|
<SpotlightDialog
|
|
|
|
initialFilter={Filter.People}
|
|
|
|
initialText={testPerson.display_name}
|
|
|
|
onFinished={() => null}
|
|
|
|
/>,
|
|
|
|
);
|
2023-02-21 08:06:43 +00:00
|
|
|
// search is debounced
|
|
|
|
jest.advanceTimersByTime(200);
|
|
|
|
await flushPromisesWithFakeTimers();
|
2022-06-15 14:14:05 +00:00
|
|
|
|
2023-02-21 08:06:43 +00:00
|
|
|
const filterChip = document.querySelector("div.mx_SpotlightDialog_filter")!;
|
|
|
|
expect(filterChip).toBeInTheDocument();
|
|
|
|
expect(filterChip.innerHTML).toContain("People");
|
2022-06-15 14:14:05 +00:00
|
|
|
|
2023-02-21 08:06:43 +00:00
|
|
|
const content = document.querySelector("#mx_SpotlightDialog_content")!;
|
2023-04-20 17:13:30 +00:00
|
|
|
const options = content.querySelectorAll("li.mx_SpotlightDialog_option");
|
2022-06-15 14:14:05 +00:00
|
|
|
expect(options.length).toBeGreaterThanOrEqual(1);
|
2023-02-21 08:06:43 +00:00
|
|
|
expect(options[0]!.innerHTML).toContain(testPerson.display_name);
|
2022-06-15 14:14:05 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-02-03 14:47:07 +00:00
|
|
|
describe("when MSC3946 dynamic room predecessors is enabled", () => {
|
|
|
|
beforeEach(() => {
|
2023-03-08 14:18:03 +00:00
|
|
|
jest.spyOn(SettingsStore, "getValue").mockImplementation((settingName, roomId, excludeDefault) => {
|
|
|
|
if (settingName === "feature_dynamic_room_predecessors") {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return []; // SpotlightSearch.recentSearches
|
|
|
|
}
|
|
|
|
});
|
2023-02-03 14:47:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
2023-03-08 14:18:03 +00:00
|
|
|
jest.restoreAllMocks();
|
2023-02-03 14:47:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should call getVisibleRooms with MSC3946 dynamic room predecessors", async () => {
|
2023-02-21 08:06:43 +00:00
|
|
|
render(<SpotlightDialog onFinished={() => null} />);
|
|
|
|
jest.advanceTimersByTime(200);
|
|
|
|
await flushPromisesWithFakeTimers();
|
2023-02-03 14:47:07 +00:00
|
|
|
expect(mockedClient.getVisibleRooms).toHaveBeenCalledWith(true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-06-15 14:14:05 +00:00
|
|
|
describe("should apply manually selected filter", () => {
|
|
|
|
it("with public rooms", async () => {
|
2023-02-21 08:06:43 +00:00
|
|
|
render(<SpotlightDialog onFinished={() => null} />);
|
|
|
|
jest.advanceTimersByTime(200);
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
fireEvent.click(screen.getByText("Public rooms"));
|
|
|
|
// wrapper.find("#mx_SpotlightDialog_button_explorePublicRooms").first().simulate("click");
|
|
|
|
// search is debounced
|
|
|
|
jest.advanceTimersByTime(200);
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
const filterChip = document.querySelector("div.mx_SpotlightDialog_filter")!;
|
|
|
|
expect(filterChip).toBeInTheDocument();
|
|
|
|
expect(filterChip.innerHTML).toContain("Public rooms");
|
|
|
|
|
|
|
|
const content = document.querySelector("#mx_SpotlightDialog_content")!;
|
2023-04-20 17:13:30 +00:00
|
|
|
const options = content.querySelectorAll("li.mx_SpotlightDialog_option");
|
2022-06-15 14:14:05 +00:00
|
|
|
expect(options.length).toBe(1);
|
2023-02-21 08:06:43 +00:00
|
|
|
expect(options[0]!.innerHTML).toContain(testPublicRoom.name);
|
2022-06-15 14:14:05 +00:00
|
|
|
|
2023-02-03 14:47:07 +00:00
|
|
|
// assert that getVisibleRooms is called without MSC3946 dynamic room predecessors
|
|
|
|
expect(mockedClient.getVisibleRooms).toHaveBeenCalledWith(false);
|
2022-06-15 14:14:05 +00:00
|
|
|
});
|
|
|
|
it("with people", async () => {
|
2023-02-21 08:06:43 +00:00
|
|
|
render(<SpotlightDialog initialText={testPerson.display_name} onFinished={() => null} />);
|
|
|
|
jest.advanceTimersByTime(200);
|
|
|
|
await flushPromisesWithFakeTimers();
|
2022-06-15 14:14:05 +00:00
|
|
|
|
2023-02-21 08:06:43 +00:00
|
|
|
fireEvent.click(screen.getByText("People"));
|
2022-06-15 14:14:05 +00:00
|
|
|
|
2023-02-21 08:06:43 +00:00
|
|
|
// search is debounced
|
|
|
|
jest.advanceTimersByTime(200);
|
|
|
|
await flushPromisesWithFakeTimers();
|
2022-06-15 14:14:05 +00:00
|
|
|
|
2023-02-21 08:06:43 +00:00
|
|
|
const filterChip = document.querySelector("div.mx_SpotlightDialog_filter")!;
|
|
|
|
expect(filterChip).toBeInTheDocument();
|
|
|
|
expect(filterChip.innerHTML).toContain("People");
|
|
|
|
|
|
|
|
const content = document.querySelector("#mx_SpotlightDialog_content")!;
|
2023-04-20 17:13:30 +00:00
|
|
|
const options = content.querySelectorAll("li.mx_SpotlightDialog_option");
|
2023-02-21 08:06:43 +00:00
|
|
|
expect(options.length).toBeGreaterThanOrEqual(1);
|
|
|
|
expect(options[0]!.innerHTML).toContain(testPerson.display_name);
|
2022-06-15 14:14:05 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("should allow clearing filter manually", () => {
|
|
|
|
it("with public room filter", async () => {
|
2023-02-21 08:06:43 +00:00
|
|
|
render(<SpotlightDialog initialFilter={Filter.PublicRooms} onFinished={() => null} />);
|
|
|
|
// search is debounced
|
|
|
|
jest.advanceTimersByTime(200);
|
|
|
|
await flushPromisesWithFakeTimers();
|
2022-06-15 14:14:05 +00:00
|
|
|
|
2023-02-21 08:06:43 +00:00
|
|
|
let filterChip = document.querySelector("div.mx_SpotlightDialog_filter")!;
|
|
|
|
expect(filterChip).toBeInTheDocument();
|
|
|
|
expect(filterChip.innerHTML).toContain("Public rooms");
|
2022-06-15 14:14:05 +00:00
|
|
|
|
2023-02-21 08:06:43 +00:00
|
|
|
fireEvent.click(filterChip.querySelector("div.mx_SpotlightDialog_filter--close")!);
|
|
|
|
jest.advanceTimersByTime(200);
|
|
|
|
await flushPromisesWithFakeTimers();
|
2022-06-15 14:14:05 +00:00
|
|
|
|
2023-02-21 08:06:43 +00:00
|
|
|
filterChip = document.querySelector("div.mx_SpotlightDialog_filter")!;
|
|
|
|
expect(filterChip).not.toBeInTheDocument();
|
2022-06-15 14:14:05 +00:00
|
|
|
});
|
|
|
|
it("with people filter", async () => {
|
2023-02-21 08:06:43 +00:00
|
|
|
render(
|
2022-06-15 14:14:05 +00:00
|
|
|
<SpotlightDialog
|
|
|
|
initialFilter={Filter.People}
|
|
|
|
initialText={testPerson.display_name}
|
|
|
|
onFinished={() => null}
|
|
|
|
/>,
|
|
|
|
);
|
2023-02-21 08:06:43 +00:00
|
|
|
// search is debounced
|
|
|
|
jest.advanceTimersByTime(200);
|
|
|
|
await flushPromisesWithFakeTimers();
|
2022-06-15 14:14:05 +00:00
|
|
|
|
2023-02-21 08:06:43 +00:00
|
|
|
let filterChip = document.querySelector("div.mx_SpotlightDialog_filter");
|
|
|
|
expect(filterChip).toBeInTheDocument();
|
|
|
|
expect(filterChip!.innerHTML).toContain("People");
|
2022-06-15 14:14:05 +00:00
|
|
|
|
2023-02-21 08:06:43 +00:00
|
|
|
fireEvent.click(filterChip!.querySelector("div.mx_SpotlightDialog_filter--close")!);
|
|
|
|
jest.advanceTimersByTime(1);
|
|
|
|
await flushPromisesWithFakeTimers();
|
2022-06-15 14:14:05 +00:00
|
|
|
|
2023-02-21 08:06:43 +00:00
|
|
|
filterChip = document.querySelector("div.mx_SpotlightDialog_filter");
|
|
|
|
expect(filterChip).not.toBeInTheDocument();
|
2022-06-15 14:14:05 +00:00
|
|
|
});
|
|
|
|
});
|
2022-07-20 07:26:25 +00:00
|
|
|
|
|
|
|
describe("searching for rooms", () => {
|
2023-02-21 08:06:43 +00:00
|
|
|
let options: NodeListOf<Element>;
|
2022-07-20 07:26:25 +00:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
2023-02-21 08:06:43 +00:00
|
|
|
render(<SpotlightDialog initialText="test23" onFinished={() => null} />);
|
|
|
|
// search is debounced
|
|
|
|
jest.advanceTimersByTime(200);
|
|
|
|
await flushPromisesWithFakeTimers();
|
2022-07-20 07:26:25 +00:00
|
|
|
|
2023-02-21 08:06:43 +00:00
|
|
|
const content = document.querySelector("#mx_SpotlightDialog_content")!;
|
2023-04-20 17:13:30 +00:00
|
|
|
options = content.querySelectorAll("li.mx_SpotlightDialog_option");
|
2022-07-20 07:26:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should find Rooms", () => {
|
2023-08-21 09:39:20 +00:00
|
|
|
expect(options).toHaveLength(4);
|
2023-02-21 08:06:43 +00:00
|
|
|
expect(options[0]!.innerHTML).toContain(testRoom.name);
|
2022-07-20 07:26:25 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should not find LocalRooms", () => {
|
2023-08-21 09:39:20 +00:00
|
|
|
expect(options).toHaveLength(4);
|
2023-02-21 08:06:43 +00:00
|
|
|
expect(options[0]!.innerHTML).not.toContain(testLocalRoom.name);
|
2022-07-20 07:26:25 +00:00
|
|
|
});
|
|
|
|
});
|
2022-08-04 06:19:52 +00:00
|
|
|
|
2023-07-25 08:05:54 +00:00
|
|
|
it("should not filter out users sent by the server", async () => {
|
|
|
|
mocked(mockedClient.searchUserDirectory).mockResolvedValue({
|
|
|
|
results: [
|
|
|
|
{ user_id: "@user1:server", display_name: "User Alpha", avatar_url: "mxc://1/avatar" },
|
|
|
|
{ user_id: "@user2:server", display_name: "User Beta", avatar_url: "mxc://2/avatar" },
|
|
|
|
],
|
|
|
|
limited: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
render(<SpotlightDialog initialFilter={Filter.People} initialText="Alpha" onFinished={() => null} />);
|
|
|
|
// search is debounced
|
|
|
|
jest.advanceTimersByTime(200);
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
const content = document.querySelector("#mx_SpotlightDialog_content")!;
|
|
|
|
const options = content.querySelectorAll("li.mx_SpotlightDialog_option");
|
|
|
|
expect(options.length).toBeGreaterThanOrEqual(2);
|
|
|
|
expect(options[0]).toHaveTextContent("User Alpha");
|
|
|
|
expect(options[1]).toHaveTextContent("User Beta");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not filter out users sent by the server even if a local suggestion gets filtered out", async () => {
|
|
|
|
const member = new RoomMember(testRoom.roomId, testPerson.user_id);
|
|
|
|
member.name = member.rawDisplayName = testPerson.display_name!;
|
|
|
|
member.getMxcAvatarUrl = jest.fn().mockReturnValue("mxc://0/avatar");
|
|
|
|
mocked(testRoom.getJoinedMembers).mockReturnValue([member]);
|
|
|
|
mocked(mockedClient.searchUserDirectory).mockResolvedValue({
|
|
|
|
results: [
|
|
|
|
{ user_id: "@janedoe:matrix.org", display_name: "User Alpha", avatar_url: "mxc://1/avatar" },
|
|
|
|
{ user_id: "@johndoe:matrix.org", display_name: "User Beta", avatar_url: "mxc://2/avatar" },
|
|
|
|
],
|
|
|
|
limited: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
render(<SpotlightDialog initialFilter={Filter.People} initialText="Beta" onFinished={() => null} />);
|
|
|
|
// search is debounced
|
|
|
|
jest.advanceTimersByTime(200);
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
const content = document.querySelector("#mx_SpotlightDialog_content")!;
|
|
|
|
const options = content.querySelectorAll("li.mx_SpotlightDialog_option");
|
|
|
|
expect(options.length).toBeGreaterThanOrEqual(2);
|
|
|
|
expect(options[0]).toHaveTextContent(testPerson.display_name!);
|
|
|
|
expect(options[1]).toHaveTextContent("User Beta");
|
|
|
|
});
|
|
|
|
|
2023-08-10 17:27:24 +00:00
|
|
|
it("show non-matching query members with DMs if they are present in the server search results", async () => {
|
|
|
|
mocked(mockedClient.searchUserDirectory).mockResolvedValue({
|
|
|
|
results: [
|
|
|
|
{ user_id: testDMUserId, display_name: "Alice Wonder", avatar_url: "mxc://1/avatar" },
|
|
|
|
{ user_id: "@bob:matrix.org", display_name: "Bob Wonder", avatar_url: "mxc://2/avatar" },
|
|
|
|
],
|
|
|
|
limited: false,
|
|
|
|
});
|
|
|
|
render(
|
|
|
|
<SpotlightDialog initialFilter={Filter.People} initialText="Something Wonder" onFinished={() => null} />,
|
|
|
|
);
|
|
|
|
// search is debounced
|
|
|
|
jest.advanceTimersByTime(200);
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
const content = document.querySelector("#mx_SpotlightDialog_content")!;
|
|
|
|
const options = content.querySelectorAll("li.mx_SpotlightDialog_option");
|
|
|
|
expect(options.length).toBeGreaterThanOrEqual(2);
|
|
|
|
expect(options[0]).toHaveTextContent(testDMUserId);
|
|
|
|
expect(options[1]).toHaveTextContent("Bob Wonder");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("don't sort the order of users sent by the server", async () => {
|
|
|
|
const serverList = [
|
|
|
|
{ user_id: "@user2:server", display_name: "User Beta", avatar_url: "mxc://2/avatar" },
|
|
|
|
{ user_id: "@user1:server", display_name: "User Alpha", avatar_url: "mxc://1/avatar" },
|
|
|
|
];
|
|
|
|
mocked(mockedClient.searchUserDirectory).mockResolvedValue({
|
|
|
|
results: serverList,
|
|
|
|
limited: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
render(<SpotlightDialog initialFilter={Filter.People} initialText="User" onFinished={() => null} />);
|
|
|
|
// search is debounced
|
|
|
|
jest.advanceTimersByTime(200);
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
const content = document.querySelector("#mx_SpotlightDialog_content")!;
|
|
|
|
const options = content.querySelectorAll("li.mx_SpotlightDialog_option");
|
|
|
|
expect(options.length).toBeGreaterThanOrEqual(2);
|
|
|
|
expect(options[0]).toHaveTextContent("User Beta");
|
|
|
|
expect(options[1]).toHaveTextContent("User Alpha");
|
|
|
|
});
|
|
|
|
|
2022-08-04 06:19:52 +00:00
|
|
|
it("should start a DM when clicking a person", async () => {
|
2023-02-21 08:06:43 +00:00
|
|
|
render(
|
2022-08-04 06:19:52 +00:00
|
|
|
<SpotlightDialog
|
|
|
|
initialFilter={Filter.People}
|
|
|
|
initialText={testPerson.display_name}
|
|
|
|
onFinished={() => null}
|
|
|
|
/>,
|
|
|
|
);
|
|
|
|
|
2023-02-21 08:06:43 +00:00
|
|
|
jest.advanceTimersByTime(200);
|
|
|
|
await flushPromisesWithFakeTimers();
|
2022-08-04 06:19:52 +00:00
|
|
|
|
2023-04-20 17:13:30 +00:00
|
|
|
const options = document.querySelectorAll("li.mx_SpotlightDialog_option");
|
2022-08-04 06:19:52 +00:00
|
|
|
expect(options.length).toBeGreaterThanOrEqual(1);
|
2023-02-21 08:06:43 +00:00
|
|
|
expect(options[0]!.innerHTML).toContain(testPerson.display_name);
|
2022-08-04 06:19:52 +00:00
|
|
|
|
2023-02-21 08:06:43 +00:00
|
|
|
fireEvent.click(options[0]!);
|
2022-08-04 06:19:52 +00:00
|
|
|
expect(startDmOnFirstMessage).toHaveBeenCalledWith(mockedClient, [new DirectoryMember(testPerson)]);
|
|
|
|
});
|
2022-09-22 14:08:14 +00:00
|
|
|
|
2023-03-27 07:39:34 +00:00
|
|
|
it("should pass via of the server being explored when joining room from directory", async () => {
|
|
|
|
SdkConfig.put({
|
|
|
|
room_directory: {
|
|
|
|
servers: ["example.tld"],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
localStorage.setItem("mx_last_room_directory_server", "example.tld");
|
|
|
|
|
|
|
|
render(<SpotlightDialog initialFilter={Filter.PublicRooms} onFinished={() => null} />);
|
|
|
|
|
|
|
|
jest.advanceTimersByTime(200);
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
const content = document.querySelector("#mx_SpotlightDialog_content")!;
|
2023-04-20 17:13:30 +00:00
|
|
|
const options = content.querySelectorAll("li.mx_SpotlightDialog_option");
|
2023-03-27 07:39:34 +00:00
|
|
|
expect(options.length).toBe(1);
|
|
|
|
expect(options[0].innerHTML).toContain(testPublicRoom.name);
|
|
|
|
|
2023-04-06 14:02:18 +00:00
|
|
|
fireEvent.click(options[0].querySelector("[role='button']")!);
|
|
|
|
expect(defaultDispatcher.dispatch).toHaveBeenCalledTimes(1);
|
2023-03-27 07:39:34 +00:00
|
|
|
expect(defaultDispatcher.dispatch).toHaveBeenCalledWith(
|
|
|
|
expect.objectContaining({
|
|
|
|
action: "view_room",
|
|
|
|
room_id: testPublicRoom.room_id,
|
|
|
|
via_servers: ["example.tld"],
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2023-02-22 10:23:52 +00:00
|
|
|
describe("nsfw public rooms filter", () => {
|
|
|
|
const nsfwNameRoom: IPublicRoomsChunkRoom = {
|
|
|
|
room_id: "@room1:matrix.org",
|
|
|
|
name: "Room 1 [NSFW]",
|
|
|
|
topic: undefined,
|
|
|
|
world_readable: false,
|
|
|
|
num_joined_members: 1,
|
|
|
|
guest_can_join: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
const nsfwTopicRoom: IPublicRoomsChunkRoom = {
|
|
|
|
room_id: "@room2:matrix.org",
|
|
|
|
name: "Room 2",
|
|
|
|
topic: "A room with a topic that includes nsfw",
|
|
|
|
world_readable: false,
|
|
|
|
num_joined_members: 1,
|
|
|
|
guest_can_join: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
const potatoRoom: IPublicRoomsChunkRoom = {
|
|
|
|
room_id: "@room3:matrix.org",
|
|
|
|
name: "Potato Room 3",
|
|
|
|
topic: "Room where we discuss potatoes",
|
|
|
|
world_readable: false,
|
|
|
|
num_joined_members: 1,
|
|
|
|
guest_can_join: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
mockedClient = mockClient({ rooms: [nsfwNameRoom, nsfwTopicRoom, potatoRoom], users: [testPerson] });
|
|
|
|
SettingsStore.setValue("SpotlightSearch.showNsfwPublicRooms", null, SettingLevel.DEVICE, false);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
SettingsStore.setValue("SpotlightSearch.showNsfwPublicRooms", null, SettingLevel.DEVICE, false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("does not display rooms with nsfw keywords in results when showNsfwPublicRooms is falsy", async () => {
|
|
|
|
render(<SpotlightDialog initialFilter={Filter.PublicRooms} onFinished={() => null} />);
|
|
|
|
|
|
|
|
// search is debounced
|
|
|
|
jest.advanceTimersByTime(200);
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
2023-03-07 13:19:18 +00:00
|
|
|
expect(screen.getByText(potatoRoom.name!)).toBeInTheDocument();
|
|
|
|
expect(screen.queryByText(nsfwTopicRoom.name!)).not.toBeInTheDocument();
|
|
|
|
expect(screen.queryByText(nsfwTopicRoom.name!)).not.toBeInTheDocument();
|
2023-02-22 10:23:52 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("displays rooms with nsfw keywords in results when showNsfwPublicRooms is truthy", async () => {
|
|
|
|
SettingsStore.setValue("SpotlightSearch.showNsfwPublicRooms", null, SettingLevel.DEVICE, true);
|
|
|
|
render(<SpotlightDialog initialFilter={Filter.PublicRooms} onFinished={() => null} />);
|
|
|
|
|
|
|
|
// search is debounced
|
|
|
|
jest.advanceTimersByTime(200);
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
2023-03-07 13:19:18 +00:00
|
|
|
expect(screen.getByText(nsfwTopicRoom.name!)).toBeInTheDocument();
|
|
|
|
expect(screen.getByText(nsfwNameRoom.name!)).toBeInTheDocument();
|
|
|
|
expect(screen.getByText(potatoRoom.name!)).toBeInTheDocument();
|
2023-02-22 10:23:52 +00:00
|
|
|
});
|
|
|
|
});
|
2023-08-08 14:08:17 +00:00
|
|
|
|
|
|
|
it("should show error if /publicRooms API failed", async () => {
|
|
|
|
mocked(mockedClient.publicRooms).mockRejectedValue(new ConnectionError("Failed to fetch"));
|
|
|
|
render(<SpotlightDialog initialFilter={Filter.PublicRooms} onFinished={() => null} />);
|
|
|
|
|
|
|
|
jest.advanceTimersByTime(200);
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
expect(screen.getByText("Failed to query public rooms")).toBeInTheDocument();
|
|
|
|
});
|
2023-08-30 06:54:26 +00:00
|
|
|
|
|
|
|
describe("knock rooms", () => {
|
|
|
|
const knockRoom: IPublicRoomsChunkRoom = {
|
|
|
|
guest_can_join: false,
|
|
|
|
join_rule: JoinRule.Knock,
|
|
|
|
num_joined_members: 0,
|
|
|
|
room_id: "some-room-id",
|
|
|
|
world_readable: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
const viewRoomParams = {
|
|
|
|
action: Action.ViewRoom,
|
|
|
|
metricsTrigger: "WebUnifiedSearch",
|
|
|
|
metricsViaKeyboard: false,
|
|
|
|
room_alias: undefined,
|
|
|
|
room_id: knockRoom.room_id,
|
|
|
|
should_peek: false,
|
|
|
|
via_servers: ["example.tld"],
|
|
|
|
};
|
|
|
|
|
|
|
|
beforeEach(() => (mockedClient = mockClient({ rooms: [knockRoom] })));
|
|
|
|
|
|
|
|
describe("when disabling feature", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockImplementation((setting) =>
|
|
|
|
setting === "feature_ask_to_join" ? false : [],
|
|
|
|
);
|
|
|
|
|
|
|
|
render(<SpotlightDialog initialFilter={Filter.PublicRooms} onFinished={() => {}} />);
|
|
|
|
|
|
|
|
// search is debounced
|
|
|
|
jest.advanceTimersByTime(200);
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
fireEvent.click(screen.getByRole("button", { name: "View" }));
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not skip to auto join", async () => {
|
|
|
|
expect(defaultDispatcher.dispatch).toHaveBeenCalledWith({ ...viewRoomParams, auto_join: true });
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not prompt ask to join", async () => {
|
|
|
|
expect(defaultDispatcher.dispatch).not.toHaveBeenCalledWith({ action: Action.PromptAskToJoin });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("when enabling feature", () => {
|
|
|
|
beforeEach(async () => {
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockImplementation((setting) =>
|
|
|
|
setting === "feature_ask_to_join" ? true : [],
|
|
|
|
);
|
|
|
|
jest.spyOn(mockedClient, "getRoom").mockReturnValue(null);
|
|
|
|
|
|
|
|
render(<SpotlightDialog initialFilter={Filter.PublicRooms} onFinished={() => {}} />);
|
|
|
|
|
|
|
|
// search is debounced
|
|
|
|
jest.advanceTimersByTime(200);
|
|
|
|
await flushPromisesWithFakeTimers();
|
|
|
|
|
|
|
|
fireEvent.click(screen.getByRole("button", { name: "Ask to join" }));
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should skip to auto join", async () => {
|
|
|
|
expect(defaultDispatcher.dispatch).toHaveBeenCalledWith({ ...viewRoomParams, auto_join: false });
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should prompt ask to join", async () => {
|
|
|
|
expect(defaultDispatcher.dispatch).toHaveBeenCalledWith({ action: Action.PromptAskToJoin });
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2022-06-15 14:14:05 +00:00
|
|
|
});
|