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

Commit 7f34aef

Browse files
committed
Support dynamic room predecessor in SpaceProvider
1 parent 6141cca commit 7f34aef

File tree

3 files changed

+146
-3
lines changed

3 files changed

+146
-3
lines changed

src/autocomplete/SpaceProvider.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ import React from "react";
1919

2020
import { _t } from "../languageHandler";
2121
import { MatrixClientPeg } from "../MatrixClientPeg";
22+
import SettingsStore from "../settings/SettingsStore";
2223
import RoomProvider from "./RoomProvider";
2324

2425
export default class SpaceProvider extends RoomProvider {
2526
protected getRooms(): Room[] {
2627
return MatrixClientPeg.get()
27-
.getVisibleRooms()
28+
.getVisibleRooms(SettingsStore.getValue("feature_dynamic_room_predecessors"))
2829
.filter((r) => r.isSpaceRoom());
2930
}
3031

test/autocomplete/RoomProvider-test.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { MatrixClient, Room } from "matrix-js-sdk/src/matrix";
1919

2020
import RoomProvider from "../../src/autocomplete/RoomProvider";
2121
import SettingsStore from "../../src/settings/SettingsStore";
22-
import { mkRoom, stubClient } from "../test-utils";
22+
import { mkRoom, mkSpace, stubClient } from "../test-utils";
2323

2424
describe("RoomProvider", () => {
2525
it("suggests a room whose alias matches a prefix", async () => {
@@ -52,7 +52,8 @@ describe("RoomProvider", () => {
5252
const room1 = makeRoom(client, "room1:e.com");
5353
const room2 = makeRoom(client, "room2:e.com");
5454
const other = makeRoom(client, "other:e.com");
55-
mocked(client.getVisibleRooms).mockReturnValue([room1, room2, other]);
55+
const space = makeSpace(client, "room3:e.com");
56+
mocked(client.getVisibleRooms).mockReturnValue([room1, room2, other, space]);
5657

5758
// When we search for rooms starting with a prefix
5859
const roomProvider = new RoomProvider(room1);
@@ -121,6 +122,12 @@ describe("RoomProvider", () => {
121122
});
122123
});
123124

125+
function makeSpace(client: MatrixClient, name: string): Room {
126+
const space = mkSpace(client, `!${name}`);
127+
space.getCanonicalAlias.mockReturnValue(`#${name}`);
128+
return space;
129+
}
130+
124131
function makeRoom(client: MatrixClient, name: string): Room {
125132
const room = mkRoom(client, `!${name}`);
126133
room.getCanonicalAlias.mockReturnValue(`#${name}`);
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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 { mocked } from "jest-mock";
18+
import { MatrixClient, Room } from "matrix-js-sdk/src/matrix";
19+
20+
import SpaceProvider from "../../src/autocomplete/SpaceProvider";
21+
import SettingsStore from "../../src/settings/SettingsStore";
22+
import { mkRoom, mkSpace, stubClient } from "../test-utils";
23+
24+
describe("SpaceProvider", () => {
25+
it("suggests a space whose alias matches a prefix", async () => {
26+
// Given a space
27+
const client = stubClient();
28+
const space = makeSpace(client, "space:e.com");
29+
mocked(client.getVisibleRooms).mockReturnValue([space]);
30+
31+
// When we search for spaces starting with its prefix
32+
const spaceProvider = new SpaceProvider(space);
33+
const completions = await spaceProvider.getCompletions("#sp", { beginning: true, start: 0, end: 3 });
34+
35+
// Then we find it
36+
expect(completions).toStrictEqual([
37+
{
38+
type: "room",
39+
completion: space.getCanonicalAlias(),
40+
completionId: space.roomId,
41+
component: expect.anything(),
42+
href: "https://matrix.to/#/#space:e.com",
43+
range: { start: 0, end: 3 },
44+
suffix: " ",
45+
},
46+
]);
47+
});
48+
49+
it("suggests only spaces matching a prefix", async () => {
50+
// Given some spaces with different names
51+
const client = stubClient();
52+
const space1 = makeSpace(client, "space1:e.com");
53+
const space2 = makeSpace(client, "space2:e.com");
54+
const other = makeSpace(client, "other:e.com");
55+
const room = makeRoom(client, "space3:e.com");
56+
mocked(client.getVisibleRooms).mockReturnValue([space1, space2, other, room]);
57+
58+
// When we search for spaces starting with a prefix
59+
const spaceProvider = new SpaceProvider(space1);
60+
const completions = await spaceProvider.getCompletions("#sp", { beginning: true, start: 0, end: 3 });
61+
62+
// Then we find the two spaces with that prefix, but not the other one
63+
expect(completions).toStrictEqual([
64+
{
65+
type: "room",
66+
completion: space1.getCanonicalAlias(),
67+
completionId: space1.roomId,
68+
component: expect.anything(),
69+
href: "https://matrix.to/#/#space1:e.com",
70+
range: { start: 0, end: 3 },
71+
suffix: " ",
72+
},
73+
{
74+
type: "room",
75+
completion: space2.getCanonicalAlias(),
76+
completionId: space2.roomId,
77+
component: expect.anything(),
78+
href: "https://matrix.to/#/#space2:e.com",
79+
range: { start: 0, end: 3 },
80+
suffix: " ",
81+
},
82+
]);
83+
});
84+
85+
describe("If the feature_dynamic_room_predecessors is not enabled", () => {
86+
beforeEach(() => {
87+
jest.spyOn(SettingsStore, "getValue").mockReturnValue(false);
88+
});
89+
90+
it("Passes through the dynamic predecessor setting", async () => {
91+
const client = stubClient();
92+
const space = makeSpace(client, "space:e.com");
93+
mocked(client.getVisibleRooms).mockReturnValue([space]);
94+
mocked(client.getVisibleRooms).mockClear();
95+
96+
const spaceProvider = new SpaceProvider(space);
97+
await spaceProvider.getCompletions("#ro", { beginning: true, start: 0, end: 3 });
98+
99+
expect(client.getVisibleRooms).toHaveBeenCalledWith(false);
100+
});
101+
});
102+
103+
describe("If the feature_dynamic_room_predecessors is enabled", () => {
104+
beforeEach(() => {
105+
// Turn on feature_dynamic_space_predecessors setting
106+
jest.spyOn(SettingsStore, "getValue").mockImplementation(
107+
(settingName) => settingName === "feature_dynamic_room_predecessors",
108+
);
109+
});
110+
111+
it("Passes through the dynamic predecessor setting", async () => {
112+
const client = stubClient();
113+
const space = makeSpace(client, "space:e.com");
114+
mocked(client.getVisibleRooms).mockReturnValue([space]);
115+
mocked(client.getVisibleRooms).mockClear();
116+
117+
const spaceProvider = new SpaceProvider(space);
118+
await spaceProvider.getCompletions("#ro", { beginning: true, start: 0, end: 3 });
119+
120+
expect(client.getVisibleRooms).toHaveBeenCalledWith(true);
121+
});
122+
});
123+
});
124+
125+
function makeSpace(client: MatrixClient, name: string): Room {
126+
const space = mkSpace(client, `!${name}`);
127+
space.getCanonicalAlias.mockReturnValue(`#${name}`);
128+
return space;
129+
}
130+
131+
function makeRoom(client: MatrixClient, name: string): Room {
132+
const room = mkRoom(client, `!${name}`);
133+
room.getCanonicalAlias.mockReturnValue(`#${name}`);
134+
return room;
135+
}

0 commit comments

Comments
 (0)