Refactor SlashCommands to not use MatrixClientPeg (#10905)
This commit is contained in:
parent
192e6f6c3d
commit
796ed35e75
7 changed files with 244 additions and 170 deletions
|
@ -19,11 +19,13 @@ import { mocked } from "jest-mock";
|
|||
|
||||
import { Command, Commands, getCommand } from "../src/SlashCommands";
|
||||
import { createTestClient } from "./test-utils";
|
||||
import { MatrixClientPeg } from "../src/MatrixClientPeg";
|
||||
import { LocalRoom, LOCAL_ROOM_ID_PREFIX } from "../src/models/LocalRoom";
|
||||
import SettingsStore from "../src/settings/SettingsStore";
|
||||
import LegacyCallHandler from "../src/LegacyCallHandler";
|
||||
import { SdkContextClass } from "../src/contexts/SDKContext";
|
||||
import Modal from "../src/Modal";
|
||||
import WidgetUtils from "../src/utils/WidgetUtils";
|
||||
import { WidgetType } from "../src/widgets/WidgetType";
|
||||
|
||||
describe("SlashCommands", () => {
|
||||
let client: MatrixClient;
|
||||
|
@ -57,7 +59,6 @@ describe("SlashCommands", () => {
|
|||
jest.clearAllMocks();
|
||||
|
||||
client = createTestClient();
|
||||
jest.spyOn(MatrixClientPeg, "get").mockReturnValue(client);
|
||||
|
||||
room = new Room(roomId, client, client.getUserId()!);
|
||||
localRoom = new LocalRoom(localRoomId, client, client.getUserId()!);
|
||||
|
@ -70,9 +71,16 @@ describe("SlashCommands", () => {
|
|||
const command = getCommand("/topic pizza");
|
||||
expect(command.cmd).toBeDefined();
|
||||
expect(command.args).toBeDefined();
|
||||
await command.cmd!.run("room-id", null, command.args);
|
||||
await command.cmd!.run(client, "room-id", null, command.args);
|
||||
expect(client.setRoomTopic).toHaveBeenCalledWith("room-id", "pizza", undefined);
|
||||
});
|
||||
|
||||
it("should show topic modal if no args passed", async () => {
|
||||
const spy = jest.spyOn(Modal, "createDialog");
|
||||
const command = getCommand("/topic")!;
|
||||
await command.cmd!.run(client, roomId, null);
|
||||
expect(spy).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe.each([
|
||||
|
@ -104,12 +112,12 @@ describe("SlashCommands", () => {
|
|||
describe("isEnabled", () => {
|
||||
it("should return true for Room", () => {
|
||||
setCurrentRoom();
|
||||
expect(command.isEnabled()).toBe(true);
|
||||
expect(command.isEnabled(client)).toBe(true);
|
||||
});
|
||||
|
||||
it("should return false for LocalRoom", () => {
|
||||
setCurrentLocalRoon();
|
||||
expect(command.isEnabled()).toBe(false);
|
||||
expect(command.isEnabled(client)).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -127,12 +135,12 @@ describe("SlashCommands", () => {
|
|||
|
||||
it("should return true for Room", () => {
|
||||
setCurrentRoom();
|
||||
expect(command.isEnabled()).toBe(true);
|
||||
expect(command.isEnabled(client)).toBe(true);
|
||||
});
|
||||
|
||||
it("should return false for LocalRoom", () => {
|
||||
setCurrentLocalRoon();
|
||||
expect(command.isEnabled()).toBe(false);
|
||||
expect(command.isEnabled(client)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -143,12 +151,12 @@ describe("SlashCommands", () => {
|
|||
|
||||
it("should return false for Room", () => {
|
||||
setCurrentRoom();
|
||||
expect(command.isEnabled()).toBe(false);
|
||||
expect(command.isEnabled(client)).toBe(false);
|
||||
});
|
||||
|
||||
it("should return false for LocalRoom", () => {
|
||||
setCurrentLocalRoon();
|
||||
expect(command.isEnabled()).toBe(false);
|
||||
expect(command.isEnabled(client)).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -169,12 +177,12 @@ describe("SlashCommands", () => {
|
|||
|
||||
it("should return true for Room", () => {
|
||||
setCurrentRoom();
|
||||
expect(command.isEnabled()).toBe(true);
|
||||
expect(command.isEnabled(client)).toBe(true);
|
||||
});
|
||||
|
||||
it("should return false for LocalRoom", () => {
|
||||
setCurrentLocalRoon();
|
||||
expect(command.isEnabled()).toBe(false);
|
||||
expect(command.isEnabled(client)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -187,12 +195,12 @@ describe("SlashCommands", () => {
|
|||
|
||||
it("should return false for Room", () => {
|
||||
setCurrentRoom();
|
||||
expect(command.isEnabled()).toBe(false);
|
||||
expect(command.isEnabled(client)).toBe(false);
|
||||
});
|
||||
|
||||
it("should return false for LocalRoom", () => {
|
||||
setCurrentLocalRoon();
|
||||
expect(command.isEnabled()).toBe(false);
|
||||
expect(command.isEnabled(client)).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -209,7 +217,7 @@ describe("SlashCommands", () => {
|
|||
const command = getCommand("/part #foo:bar");
|
||||
expect(command.cmd).toBeDefined();
|
||||
expect(command.args).toBeDefined();
|
||||
await command.cmd!.run("room-id", null, command.args);
|
||||
await command.cmd!.run(client, "room-id", null, command.args);
|
||||
expect(client.leaveRoomChain).toHaveBeenCalledWith("room-id", expect.anything());
|
||||
});
|
||||
|
||||
|
@ -223,7 +231,7 @@ describe("SlashCommands", () => {
|
|||
const command = getCommand("/part #foo:bar");
|
||||
expect(command.cmd).toBeDefined();
|
||||
expect(command.args).toBeDefined();
|
||||
await command.cmd!.run("room-id", null, command.args!);
|
||||
await command.cmd!.run(client, "room-id", null, command.args!);
|
||||
expect(client.leaveRoomChain).toHaveBeenCalledWith("room-id", expect.anything());
|
||||
});
|
||||
});
|
||||
|
@ -232,11 +240,45 @@ describe("SlashCommands", () => {
|
|||
const command = findCommand(commandName)!;
|
||||
|
||||
it("should return usage if no args", () => {
|
||||
expect(command.run(roomId, null, undefined).error).toBe(command.getUsage());
|
||||
expect(command.run(client, roomId, null, undefined).error).toBe(command.getUsage());
|
||||
});
|
||||
|
||||
it("should make things rainbowy", () => {
|
||||
return expect(command.run(roomId, null, "this is a test message").promise).resolves.toMatchSnapshot();
|
||||
return expect(
|
||||
command.run(client, roomId, null, "this is a test message").promise,
|
||||
).resolves.toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
|
||||
describe.each(["shrug", "tableflip", "unflip", "lenny"])("/%s", (commandName: string) => {
|
||||
const command = findCommand(commandName)!;
|
||||
|
||||
it("should match snapshot with no args", () => {
|
||||
return expect(command.run(client, roomId, null).promise).resolves.toMatchSnapshot();
|
||||
});
|
||||
|
||||
it("should match snapshot with args", () => {
|
||||
return expect(
|
||||
command.run(client, roomId, null, "this is a test message").promise,
|
||||
).resolves.toMatchSnapshot();
|
||||
});
|
||||
});
|
||||
|
||||
describe("/addwidget", () => {
|
||||
it("should parse html iframe snippets", async () => {
|
||||
jest.spyOn(WidgetUtils, "canUserModifyWidgets").mockReturnValue(true);
|
||||
const spy = jest.spyOn(WidgetUtils, "setRoomWidget");
|
||||
const command = findCommand("addwidget")!;
|
||||
await command.run(client, roomId, null, '<iframe src="https://element.io"></iframe>');
|
||||
expect(spy).toHaveBeenCalledWith(
|
||||
client,
|
||||
roomId,
|
||||
expect.any(String),
|
||||
WidgetType.CUSTOM,
|
||||
"https://element.io",
|
||||
"Custom",
|
||||
{},
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue