2022-06-15 16:14:05 +02:00
|
|
|
/*
|
|
|
|
Copyright 2022 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.
|
|
|
|
*/
|
|
|
|
|
2022-08-02 15:10:43 +02:00
|
|
|
// eslint-disable-next-line deprecate/import
|
2022-07-20 09:26:25 +02:00
|
|
|
import { mount, ReactWrapper } from "enzyme";
|
|
|
|
import { mocked } from "jest-mock";
|
|
|
|
import { IProtocol, IPublicRoomsChunkRoom, MatrixClient, Room, RoomMember } from "matrix-js-sdk/src/matrix";
|
2022-06-15 16:14:05 +02:00
|
|
|
import { sleep } from "matrix-js-sdk/src/utils";
|
|
|
|
import React from "react";
|
|
|
|
import { act } from "react-dom/test-utils";
|
|
|
|
import sanitizeHtml from "sanitize-html";
|
|
|
|
|
|
|
|
import SpotlightDialog, { Filter } from "../../../../src/components/views/dialogs/spotlight/SpotlightDialog";
|
|
|
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
2022-07-20 09:26:25 +02:00
|
|
|
import { LocalRoom, LOCAL_ROOM_ID_PREFIX } from "../../../../src/models/LocalRoom";
|
2022-08-04 08:19:52 +02:00
|
|
|
import { DirectoryMember, startDmOnFirstMessage } from "../../../../src/utils/direct-messages";
|
2022-07-20 09:26:25 +02:00
|
|
|
import DMRoomMap from "../../../../src/utils/DMRoomMap";
|
|
|
|
import { mkRoom, stubClient } from "../../../test-utils";
|
2022-09-22 16:08:14 +02:00
|
|
|
import { shouldShowFeedback } from "../../../../src/utils/Feedback";
|
2023-02-03 15:47:07 +01:00
|
|
|
import SettingsStore from "../../../../src/settings/SettingsStore";
|
|
|
|
import { SettingLevel } from "../../../../src/settings/SettingLevel";
|
2022-09-22 16:08:14 +02:00
|
|
|
|
|
|
|
jest.mock("../../../../src/utils/Feedback");
|
2022-07-20 09:26:25 +02:00
|
|
|
|
|
|
|
jest.mock("../../../../src/utils/direct-messages", () => ({
|
|
|
|
// @ts-ignore
|
|
|
|
...jest.requireActual("../../../../src/utils/direct-messages"),
|
|
|
|
startDmOnFirstMessage: jest.fn(),
|
|
|
|
}));
|
2022-06-15 16:14:05 +02: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[];
|
|
|
|
}
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
function mockClient({
|
|
|
|
userId = "testuser",
|
|
|
|
homeserver = "example.tld",
|
|
|
|
thirdPartyProtocols = {},
|
|
|
|
rooms = [],
|
|
|
|
members = [],
|
|
|
|
users = [],
|
|
|
|
}: MockClientOptions = {}): MatrixClient {
|
2022-06-15 16:14:05 +02:00
|
|
|
stubClient();
|
|
|
|
const cli = MatrixClientPeg.get();
|
|
|
|
MatrixClientPeg.getHomeserverName = jest.fn(() => homeserver);
|
|
|
|
cli.getUserId = jest.fn(() => userId);
|
|
|
|
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();
|
2022-12-12 12:24:14 +01:00
|
|
|
const chunk = rooms.filter(
|
|
|
|
(it) =>
|
|
|
|
!searchTerm ||
|
|
|
|
it.room_id.toLowerCase().includes(searchTerm) ||
|
|
|
|
it.name?.toLowerCase().includes(searchTerm) ||
|
|
|
|
sanitizeHtml(it?.topic, { allowedTags: [] }).toLowerCase().includes(searchTerm) ||
|
|
|
|
it.canonical_alias?.toLowerCase().includes(searchTerm) ||
|
|
|
|
it.aliases?.find((alias) => alias.toLowerCase().includes(searchTerm)),
|
|
|
|
);
|
2022-06-15 16:14:05 +02:00
|
|
|
return Promise.resolve({
|
|
|
|
chunk,
|
|
|
|
total_room_count_estimate: chunk.length,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
cli.searchUserDirectory = jest.fn(({ term, limit }) => {
|
|
|
|
const searchTerm = term?.toLowerCase();
|
2022-12-12 12:24:14 +01:00
|
|
|
const results = users.filter(
|
|
|
|
(it) =>
|
|
|
|
!searchTerm ||
|
|
|
|
it.user_id.toLowerCase().includes(searchTerm) ||
|
|
|
|
it.display_name.toLowerCase().includes(searchTerm),
|
|
|
|
);
|
2022-06-15 16:14:05 +02:00
|
|
|
return Promise.resolve({
|
|
|
|
results: results.slice(0, limit ?? +Infinity),
|
|
|
|
limited: limit && limit < results.length,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
cli.getProfileInfo = jest.fn(async (userId) => {
|
2022-12-12 12:24:14 +01:00
|
|
|
const member = members.find((it) => it.userId === userId);
|
2022-06-15 16:14:05 +02:00
|
|
|
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 = {
|
|
|
|
room_id: "@room247:matrix.org",
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2022-07-20 09:26:25 +02:00
|
|
|
let testRoom: Room;
|
|
|
|
let testLocalRoom: LocalRoom;
|
|
|
|
|
|
|
|
let mockedClient: MatrixClient;
|
|
|
|
|
2022-06-15 16:14:05 +02:00
|
|
|
beforeEach(() => {
|
2022-07-20 09:26:25 +02:00
|
|
|
mockedClient = mockClient({ rooms: [testPublicRoom], users: [testPerson] });
|
|
|
|
testRoom = mkRoom(mockedClient, "!test23:example.com");
|
|
|
|
mocked(testRoom.getMyMembership).mockReturnValue("join");
|
|
|
|
testLocalRoom = new LocalRoom(LOCAL_ROOM_ID_PREFIX + "test23", mockedClient, mockedClient.getUserId());
|
|
|
|
testLocalRoom.updateMyMembership("join");
|
|
|
|
mocked(mockedClient.getVisibleRooms).mockReturnValue([testRoom, testLocalRoom]);
|
|
|
|
|
|
|
|
jest.spyOn(DMRoomMap, "shared").mockReturnValue({
|
|
|
|
getUserIdForRoomId: jest.fn(),
|
|
|
|
} as unknown as DMRoomMap);
|
2022-06-15 16:14:05 +02:00
|
|
|
});
|
2022-09-22 16:08:14 +02:00
|
|
|
|
2022-06-15 16:14:05 +02:00
|
|
|
describe("should apply filters supplied via props", () => {
|
|
|
|
it("without filter", async () => {
|
2022-12-12 12:24:14 +01:00
|
|
|
const wrapper = mount(<SpotlightDialog initialFilter={null} onFinished={() => null} />);
|
2022-06-15 16:14:05 +02:00
|
|
|
await act(async () => {
|
|
|
|
await sleep(200);
|
|
|
|
});
|
|
|
|
wrapper.update();
|
|
|
|
|
|
|
|
const filterChip = wrapper.find("div.mx_SpotlightDialog_filter");
|
|
|
|
expect(filterChip.exists()).toBeFalsy();
|
|
|
|
|
|
|
|
wrapper.unmount();
|
|
|
|
});
|
|
|
|
it("with public room filter", async () => {
|
2022-12-12 12:24:14 +01:00
|
|
|
const wrapper = mount(<SpotlightDialog initialFilter={Filter.PublicRooms} onFinished={() => null} />);
|
2022-06-15 16:14:05 +02:00
|
|
|
await act(async () => {
|
|
|
|
await sleep(200);
|
|
|
|
});
|
|
|
|
wrapper.update();
|
|
|
|
|
|
|
|
const filterChip = wrapper.find("div.mx_SpotlightDialog_filter");
|
|
|
|
expect(filterChip.exists()).toBeTruthy();
|
|
|
|
expect(filterChip.text()).toEqual("Public rooms");
|
|
|
|
|
|
|
|
const content = wrapper.find("#mx_SpotlightDialog_content");
|
|
|
|
const options = content.find("div.mx_SpotlightDialog_option");
|
|
|
|
expect(options.length).toBe(1);
|
|
|
|
expect(options.first().text()).toContain(testPublicRoom.name);
|
|
|
|
|
|
|
|
wrapper.unmount();
|
|
|
|
});
|
|
|
|
it("with people filter", async () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<SpotlightDialog
|
|
|
|
initialFilter={Filter.People}
|
|
|
|
initialText={testPerson.display_name}
|
2022-12-12 12:24:14 +01:00
|
|
|
onFinished={() => null}
|
|
|
|
/>,
|
2022-06-15 16:14:05 +02:00
|
|
|
);
|
|
|
|
await act(async () => {
|
|
|
|
await sleep(200);
|
|
|
|
});
|
|
|
|
wrapper.update();
|
|
|
|
|
|
|
|
const filterChip = wrapper.find("div.mx_SpotlightDialog_filter");
|
|
|
|
expect(filterChip.exists()).toBeTruthy();
|
|
|
|
expect(filterChip.text()).toEqual("People");
|
|
|
|
|
|
|
|
const content = wrapper.find("#mx_SpotlightDialog_content");
|
|
|
|
const options = content.find("div.mx_SpotlightDialog_option");
|
|
|
|
expect(options.length).toBeGreaterThanOrEqual(1);
|
|
|
|
expect(options.first().text()).toContain(testPerson.display_name);
|
|
|
|
|
|
|
|
wrapper.unmount();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-02-03 15:47:07 +01:00
|
|
|
describe("when MSC3946 dynamic room predecessors is enabled", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
SettingsStore.setValue("feature_dynamic_room_predecessors", null, SettingLevel.DEVICE, true);
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
SettingsStore.setValue("feature_dynamic_room_predecessors", null, SettingLevel.DEVICE, null);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should call getVisibleRooms with MSC3946 dynamic room predecessors", async () => {
|
|
|
|
const wrapper = mount(<SpotlightDialog onFinished={() => null} />);
|
|
|
|
await act(async () => {
|
|
|
|
await sleep(1);
|
|
|
|
});
|
|
|
|
wrapper.update();
|
|
|
|
expect(mockedClient.getVisibleRooms).toHaveBeenCalledWith(true);
|
|
|
|
wrapper.unmount();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-06-15 16:14:05 +02:00
|
|
|
describe("should apply manually selected filter", () => {
|
|
|
|
it("with public rooms", async () => {
|
2022-12-12 12:24:14 +01:00
|
|
|
const wrapper = mount(<SpotlightDialog onFinished={() => null} />);
|
2022-06-15 16:14:05 +02:00
|
|
|
await act(async () => {
|
|
|
|
await sleep(1);
|
|
|
|
});
|
|
|
|
wrapper.update();
|
|
|
|
wrapper.find("#mx_SpotlightDialog_button_explorePublicRooms").first().simulate("click");
|
|
|
|
await act(async () => {
|
|
|
|
await sleep(200);
|
|
|
|
});
|
|
|
|
wrapper.update();
|
|
|
|
|
|
|
|
const filterChip = wrapper.find("div.mx_SpotlightDialog_filter");
|
|
|
|
expect(filterChip.exists()).toBeTruthy();
|
|
|
|
expect(filterChip.text()).toEqual("Public rooms");
|
|
|
|
|
|
|
|
const content = wrapper.find("#mx_SpotlightDialog_content");
|
|
|
|
const options = content.find("div.mx_SpotlightDialog_option");
|
|
|
|
expect(options.length).toBe(1);
|
|
|
|
expect(options.first().text()).toContain(testPublicRoom.name);
|
|
|
|
|
2023-02-03 15:47:07 +01:00
|
|
|
// assert that getVisibleRooms is called without MSC3946 dynamic room predecessors
|
|
|
|
expect(mockedClient.getVisibleRooms).toHaveBeenCalledWith(false);
|
|
|
|
|
2022-06-15 16:14:05 +02:00
|
|
|
wrapper.unmount();
|
|
|
|
});
|
|
|
|
it("with people", async () => {
|
2022-12-12 12:24:14 +01:00
|
|
|
const wrapper = mount(<SpotlightDialog initialText={testPerson.display_name} onFinished={() => null} />);
|
2022-06-15 16:14:05 +02:00
|
|
|
await act(async () => {
|
|
|
|
await sleep(1);
|
|
|
|
});
|
|
|
|
wrapper.update();
|
|
|
|
wrapper.find("#mx_SpotlightDialog_button_startChat").first().simulate("click");
|
|
|
|
await act(async () => {
|
|
|
|
await sleep(200);
|
|
|
|
});
|
|
|
|
wrapper.update();
|
|
|
|
|
|
|
|
const filterChip = wrapper.find("div.mx_SpotlightDialog_filter");
|
|
|
|
expect(filterChip.exists()).toBeTruthy();
|
|
|
|
expect(filterChip.text()).toEqual("People");
|
|
|
|
|
|
|
|
const content = wrapper.find("#mx_SpotlightDialog_content");
|
|
|
|
const options = content.find("div.mx_SpotlightDialog_option");
|
|
|
|
expect(options.length).toBeGreaterThanOrEqual(1);
|
|
|
|
expect(options.first().text()).toContain(testPerson.display_name);
|
|
|
|
|
|
|
|
wrapper.unmount();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("should allow clearing filter manually", () => {
|
|
|
|
it("with public room filter", async () => {
|
2022-12-12 12:24:14 +01:00
|
|
|
const wrapper = mount(<SpotlightDialog initialFilter={Filter.PublicRooms} onFinished={() => null} />);
|
2022-06-15 16:14:05 +02:00
|
|
|
await act(async () => {
|
|
|
|
await sleep(200);
|
|
|
|
});
|
|
|
|
wrapper.update();
|
|
|
|
|
|
|
|
let filterChip = wrapper.find("div.mx_SpotlightDialog_filter");
|
|
|
|
expect(filterChip.exists()).toBeTruthy();
|
|
|
|
expect(filterChip.text()).toEqual("Public rooms");
|
|
|
|
|
|
|
|
filterChip.find("div.mx_SpotlightDialog_filter--close").simulate("click");
|
|
|
|
await act(async () => {
|
|
|
|
await sleep(1);
|
|
|
|
});
|
|
|
|
wrapper.update();
|
|
|
|
|
|
|
|
filterChip = wrapper.find("div.mx_SpotlightDialog_filter");
|
|
|
|
expect(filterChip.exists()).toBeFalsy();
|
|
|
|
|
|
|
|
wrapper.unmount();
|
|
|
|
});
|
|
|
|
it("with people filter", async () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<SpotlightDialog
|
|
|
|
initialFilter={Filter.People}
|
|
|
|
initialText={testPerson.display_name}
|
2022-12-12 12:24:14 +01:00
|
|
|
onFinished={() => null}
|
|
|
|
/>,
|
2022-06-15 16:14:05 +02:00
|
|
|
);
|
|
|
|
await act(async () => {
|
|
|
|
await sleep(200);
|
|
|
|
});
|
|
|
|
wrapper.update();
|
|
|
|
|
|
|
|
let filterChip = wrapper.find("div.mx_SpotlightDialog_filter");
|
|
|
|
expect(filterChip.exists()).toBeTruthy();
|
|
|
|
expect(filterChip.text()).toEqual("People");
|
|
|
|
|
|
|
|
filterChip.find("div.mx_SpotlightDialog_filter--close").simulate("click");
|
|
|
|
await act(async () => {
|
|
|
|
await sleep(1);
|
|
|
|
});
|
|
|
|
wrapper.update();
|
|
|
|
|
|
|
|
filterChip = wrapper.find("div.mx_SpotlightDialog_filter");
|
|
|
|
expect(filterChip.exists()).toBeFalsy();
|
|
|
|
|
|
|
|
wrapper.unmount();
|
|
|
|
});
|
|
|
|
});
|
2022-07-20 09:26:25 +02:00
|
|
|
|
|
|
|
describe("searching for rooms", () => {
|
|
|
|
let wrapper: ReactWrapper;
|
|
|
|
let options: ReactWrapper;
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
2022-12-12 12:24:14 +01:00
|
|
|
wrapper = mount(<SpotlightDialog initialText="test23" onFinished={() => null} />);
|
2022-07-20 09:26:25 +02:00
|
|
|
await act(async () => {
|
|
|
|
await sleep(200);
|
|
|
|
});
|
|
|
|
wrapper.update();
|
|
|
|
|
|
|
|
const content = wrapper.find("#mx_SpotlightDialog_content");
|
|
|
|
options = content.find("div.mx_SpotlightDialog_option");
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
wrapper.unmount();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should find Rooms", () => {
|
|
|
|
expect(options.length).toBe(3);
|
|
|
|
expect(options.first().text()).toContain(testRoom.name);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not find LocalRooms", () => {
|
|
|
|
expect(options.length).toBe(3);
|
|
|
|
expect(options.first().text()).not.toContain(testLocalRoom.name);
|
|
|
|
});
|
|
|
|
});
|
2022-08-04 08:19:52 +02:00
|
|
|
|
|
|
|
it("should start a DM when clicking a person", async () => {
|
|
|
|
const wrapper = mount(
|
|
|
|
<SpotlightDialog
|
|
|
|
initialFilter={Filter.People}
|
|
|
|
initialText={testPerson.display_name}
|
2022-12-12 12:24:14 +01:00
|
|
|
onFinished={() => null}
|
|
|
|
/>,
|
2022-08-04 08:19:52 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
await act(async () => {
|
|
|
|
await sleep(200);
|
|
|
|
});
|
|
|
|
wrapper.update();
|
|
|
|
|
|
|
|
const options = wrapper.find("div.mx_SpotlightDialog_option");
|
|
|
|
expect(options.length).toBeGreaterThanOrEqual(1);
|
|
|
|
expect(options.first().text()).toContain(testPerson.display_name);
|
|
|
|
|
|
|
|
options.first().simulate("click");
|
|
|
|
expect(startDmOnFirstMessage).toHaveBeenCalledWith(mockedClient, [new DirectoryMember(testPerson)]);
|
|
|
|
|
|
|
|
wrapper.unmount();
|
|
|
|
});
|
2022-09-22 16:08:14 +02:00
|
|
|
|
|
|
|
describe("Feedback prompt", () => {
|
|
|
|
it("should show feedback prompt if feedback is enabled", async () => {
|
|
|
|
mocked(shouldShowFeedback).mockReturnValue(true);
|
|
|
|
|
|
|
|
const wrapper = mount(<SpotlightDialog initialText="test23" onFinished={() => null} />);
|
|
|
|
await act(async () => {
|
|
|
|
await sleep(200);
|
|
|
|
});
|
|
|
|
wrapper.update();
|
|
|
|
|
|
|
|
const content = wrapper.find(".mx_SpotlightDialog_footer");
|
|
|
|
expect(content.childAt(0).text()).toBe("Results not as expected? Please give feedback.");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should hide feedback prompt if feedback is disabled", async () => {
|
|
|
|
mocked(shouldShowFeedback).mockReturnValue(false);
|
|
|
|
|
|
|
|
const wrapper = mount(<SpotlightDialog initialText="test23" onFinished={() => null} />);
|
|
|
|
await act(async () => {
|
|
|
|
await sleep(200);
|
|
|
|
});
|
|
|
|
wrapper.update();
|
|
|
|
|
|
|
|
const content = wrapper.find(".mx_SpotlightDialog_footer");
|
|
|
|
expect(content.text()).toBeFalsy();
|
|
|
|
});
|
|
|
|
});
|
2022-06-15 16:14:05 +02:00
|
|
|
});
|