Skip to content

Commit 796ed35

Browse files
authored
Refactor SlashCommands to not use MatrixClientPeg (matrix-org#10905)
1 parent 192e6f6 commit 796ed35

File tree

7 files changed

+244
-170
lines changed

7 files changed

+244
-170
lines changed

src/SlashCommands.tsx

Lines changed: 117 additions & 150 deletions
Large diffs are not rendered by default.

src/components/views/rooms/EditMessageComposer.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,13 @@ class EditMessageComposer extends React.Component<IEditMessageComposerProps, ISt
341341
const [cmd, args, commandText] = getSlashCommand(this.model);
342342
if (cmd) {
343343
const threadId = editedEvent?.getThread()?.id || null;
344-
const [content, commandSuccessful] = await runSlashCommand(cmd, args, roomId, threadId);
344+
const [content, commandSuccessful] = await runSlashCommand(
345+
MatrixClientPeg.get(),
346+
cmd,
347+
args,
348+
roomId,
349+
threadId,
350+
);
345351
if (!commandSuccessful) {
346352
return; // errored
347353
}

src/components/views/rooms/SendMessageComposer.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ export class SendMessageComposer extends React.Component<ISendMessageComposerPro
474474

475475
let commandSuccessful: boolean;
476476
[content, commandSuccessful] = await runSlashCommand(
477+
MatrixClientPeg.get(),
477478
cmd,
478479
args,
479480
this.props.room.roomId,

src/components/views/rooms/wysiwyg_composer/utils/message.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export async function sendMessage(
8585
if (cmd) {
8686
const threadId = relation?.rel_type === THREAD_RELATION_TYPE.name ? relation?.event_id : null;
8787
let commandSuccessful: boolean;
88-
[content, commandSuccessful] = await runSlashCommand(cmd, args, roomId, threadId ?? null);
88+
[content, commandSuccessful] = await runSlashCommand(mxClient, cmd, args, roomId, threadId ?? null);
8989

9090
if (!commandSuccessful) {
9191
return; // errored

src/editor/commands.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
import React from "react";
1818
import { logger } from "matrix-js-sdk/src/logger";
1919
import { IContent } from "matrix-js-sdk/src/models/event";
20+
import { MatrixClient } from "matrix-js-sdk/src/matrix";
2021

2122
import EditorModel from "./model";
2223
import { Type } from "./parts";
@@ -58,12 +59,13 @@ export function getSlashCommand(model: EditorModel): [Command | undefined, strin
5859
}
5960

6061
export async function runSlashCommand(
62+
matrixClient: MatrixClient,
6163
cmd: Command,
6264
args: string | undefined,
6365
roomId: string,
6466
threadId: string | null,
6567
): Promise<[content: IContent | null, success: boolean]> {
66-
const result = cmd.run(roomId, threadId, args);
68+
const result = cmd.run(matrixClient, roomId, threadId, args);
6769
let messageContent: IContent | null = null;
6870
let error: any = result.error;
6971
if (result.promise) {

test/SlashCommands-test.tsx

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ import { mocked } from "jest-mock";
1919

2020
import { Command, Commands, getCommand } from "../src/SlashCommands";
2121
import { createTestClient } from "./test-utils";
22-
import { MatrixClientPeg } from "../src/MatrixClientPeg";
2322
import { LocalRoom, LOCAL_ROOM_ID_PREFIX } from "../src/models/LocalRoom";
2423
import SettingsStore from "../src/settings/SettingsStore";
2524
import LegacyCallHandler from "../src/LegacyCallHandler";
2625
import { SdkContextClass } from "../src/contexts/SDKContext";
26+
import Modal from "../src/Modal";
27+
import WidgetUtils from "../src/utils/WidgetUtils";
28+
import { WidgetType } from "../src/widgets/WidgetType";
2729

2830
describe("SlashCommands", () => {
2931
let client: MatrixClient;
@@ -57,7 +59,6 @@ describe("SlashCommands", () => {
5759
jest.clearAllMocks();
5860

5961
client = createTestClient();
60-
jest.spyOn(MatrixClientPeg, "get").mockReturnValue(client);
6162

6263
room = new Room(roomId, client, client.getUserId()!);
6364
localRoom = new LocalRoom(localRoomId, client, client.getUserId()!);
@@ -70,9 +71,16 @@ describe("SlashCommands", () => {
7071
const command = getCommand("/topic pizza");
7172
expect(command.cmd).toBeDefined();
7273
expect(command.args).toBeDefined();
73-
await command.cmd!.run("room-id", null, command.args);
74+
await command.cmd!.run(client, "room-id", null, command.args);
7475
expect(client.setRoomTopic).toHaveBeenCalledWith("room-id", "pizza", undefined);
7576
});
77+
78+
it("should show topic modal if no args passed", async () => {
79+
const spy = jest.spyOn(Modal, "createDialog");
80+
const command = getCommand("/topic")!;
81+
await command.cmd!.run(client, roomId, null);
82+
expect(spy).toHaveBeenCalled();
83+
});
7684
});
7785

7886
describe.each([
@@ -104,12 +112,12 @@ describe("SlashCommands", () => {
104112
describe("isEnabled", () => {
105113
it("should return true for Room", () => {
106114
setCurrentRoom();
107-
expect(command.isEnabled()).toBe(true);
115+
expect(command.isEnabled(client)).toBe(true);
108116
});
109117

110118
it("should return false for LocalRoom", () => {
111119
setCurrentLocalRoon();
112-
expect(command.isEnabled()).toBe(false);
120+
expect(command.isEnabled(client)).toBe(false);
113121
});
114122
});
115123
});
@@ -127,12 +135,12 @@ describe("SlashCommands", () => {
127135

128136
it("should return true for Room", () => {
129137
setCurrentRoom();
130-
expect(command.isEnabled()).toBe(true);
138+
expect(command.isEnabled(client)).toBe(true);
131139
});
132140

133141
it("should return false for LocalRoom", () => {
134142
setCurrentLocalRoon();
135-
expect(command.isEnabled()).toBe(false);
143+
expect(command.isEnabled(client)).toBe(false);
136144
});
137145
});
138146

@@ -143,12 +151,12 @@ describe("SlashCommands", () => {
143151

144152
it("should return false for Room", () => {
145153
setCurrentRoom();
146-
expect(command.isEnabled()).toBe(false);
154+
expect(command.isEnabled(client)).toBe(false);
147155
});
148156

149157
it("should return false for LocalRoom", () => {
150158
setCurrentLocalRoon();
151-
expect(command.isEnabled()).toBe(false);
159+
expect(command.isEnabled(client)).toBe(false);
152160
});
153161
});
154162
});
@@ -169,12 +177,12 @@ describe("SlashCommands", () => {
169177

170178
it("should return true for Room", () => {
171179
setCurrentRoom();
172-
expect(command.isEnabled()).toBe(true);
180+
expect(command.isEnabled(client)).toBe(true);
173181
});
174182

175183
it("should return false for LocalRoom", () => {
176184
setCurrentLocalRoon();
177-
expect(command.isEnabled()).toBe(false);
185+
expect(command.isEnabled(client)).toBe(false);
178186
});
179187
});
180188

@@ -187,12 +195,12 @@ describe("SlashCommands", () => {
187195

188196
it("should return false for Room", () => {
189197
setCurrentRoom();
190-
expect(command.isEnabled()).toBe(false);
198+
expect(command.isEnabled(client)).toBe(false);
191199
});
192200

193201
it("should return false for LocalRoom", () => {
194202
setCurrentLocalRoon();
195-
expect(command.isEnabled()).toBe(false);
203+
expect(command.isEnabled(client)).toBe(false);
196204
});
197205
});
198206
});
@@ -209,7 +217,7 @@ describe("SlashCommands", () => {
209217
const command = getCommand("/part #foo:bar");
210218
expect(command.cmd).toBeDefined();
211219
expect(command.args).toBeDefined();
212-
await command.cmd!.run("room-id", null, command.args);
220+
await command.cmd!.run(client, "room-id", null, command.args);
213221
expect(client.leaveRoomChain).toHaveBeenCalledWith("room-id", expect.anything());
214222
});
215223

@@ -223,7 +231,7 @@ describe("SlashCommands", () => {
223231
const command = getCommand("/part #foo:bar");
224232
expect(command.cmd).toBeDefined();
225233
expect(command.args).toBeDefined();
226-
await command.cmd!.run("room-id", null, command.args!);
234+
await command.cmd!.run(client, "room-id", null, command.args!);
227235
expect(client.leaveRoomChain).toHaveBeenCalledWith("room-id", expect.anything());
228236
});
229237
});
@@ -232,11 +240,45 @@ describe("SlashCommands", () => {
232240
const command = findCommand(commandName)!;
233241

234242
it("should return usage if no args", () => {
235-
expect(command.run(roomId, null, undefined).error).toBe(command.getUsage());
243+
expect(command.run(client, roomId, null, undefined).error).toBe(command.getUsage());
236244
});
237245

238246
it("should make things rainbowy", () => {
239-
return expect(command.run(roomId, null, "this is a test message").promise).resolves.toMatchSnapshot();
247+
return expect(
248+
command.run(client, roomId, null, "this is a test message").promise,
249+
).resolves.toMatchSnapshot();
250+
});
251+
});
252+
253+
describe.each(["shrug", "tableflip", "unflip", "lenny"])("/%s", (commandName: string) => {
254+
const command = findCommand(commandName)!;
255+
256+
it("should match snapshot with no args", () => {
257+
return expect(command.run(client, roomId, null).promise).resolves.toMatchSnapshot();
258+
});
259+
260+
it("should match snapshot with args", () => {
261+
return expect(
262+
command.run(client, roomId, null, "this is a test message").promise,
263+
).resolves.toMatchSnapshot();
264+
});
265+
});
266+
267+
describe("/addwidget", () => {
268+
it("should parse html iframe snippets", async () => {
269+
jest.spyOn(WidgetUtils, "canUserModifyWidgets").mockReturnValue(true);
270+
const spy = jest.spyOn(WidgetUtils, "setRoomWidget");
271+
const command = findCommand("addwidget")!;
272+
await command.run(client, roomId, null, '<iframe src="https://element.io"></iframe>');
273+
expect(spy).toHaveBeenCalledWith(
274+
client,
275+
roomId,
276+
expect.any(String),
277+
WidgetType.CUSTOM,
278+
"https://element.io",
279+
"Custom",
280+
{},
281+
);
240282
});
241283
});
242284
});

test/__snapshots__/SlashCommands-test.tsx.snap

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`SlashCommands /lenny should match snapshot with args 1`] = `
4+
{
5+
"body": "( ͡° ͜ʖ ͡°) this is a test message",
6+
"msgtype": "m.text",
7+
}
8+
`;
9+
10+
exports[`SlashCommands /lenny should match snapshot with no args 1`] = `
11+
{
12+
"body": "( ͡° ͜ʖ ͡°)",
13+
"msgtype": "m.text",
14+
}
15+
`;
16+
317
exports[`SlashCommands /rainbow should make things rainbowy 1`] = `
418
{
519
"body": "this is a test message",
@@ -17,3 +31,45 @@ exports[`SlashCommands /rainbowme should make things rainbowy 1`] = `
1731
"msgtype": "m.emote",
1832
}
1933
`;
34+
35+
exports[`SlashCommands /shrug should match snapshot with args 1`] = `
36+
{
37+
"body": "¯\\_(ツ)_/¯ this is a test message",
38+
"msgtype": "m.text",
39+
}
40+
`;
41+
42+
exports[`SlashCommands /shrug should match snapshot with no args 1`] = `
43+
{
44+
"body": "¯\\_(ツ)_/¯",
45+
"msgtype": "m.text",
46+
}
47+
`;
48+
49+
exports[`SlashCommands /tableflip should match snapshot with args 1`] = `
50+
{
51+
"body": "(╯°□°)╯︵ ┻━┻ this is a test message",
52+
"msgtype": "m.text",
53+
}
54+
`;
55+
56+
exports[`SlashCommands /tableflip should match snapshot with no args 1`] = `
57+
{
58+
"body": "(╯°□°)╯︵ ┻━┻",
59+
"msgtype": "m.text",
60+
}
61+
`;
62+
63+
exports[`SlashCommands /unflip should match snapshot with args 1`] = `
64+
{
65+
"body": "┬──┬ ノ( ゜-゜ノ) this is a test message",
66+
"msgtype": "m.text",
67+
}
68+
`;
69+
70+
exports[`SlashCommands /unflip should match snapshot with no args 1`] = `
71+
{
72+
"body": "┬──┬ ノ( ゜-゜ノ)",
73+
"msgtype": "m.text",
74+
}
75+
`;

0 commit comments

Comments
 (0)