Conform more code to strict null checking (#10153)

* Conform more code to strict null checking

* Conform more code to strict null checking

* Iterate

* Iterate
This commit is contained in:
Michael Telatynski 2023-02-15 13:36:22 +00:00 committed by GitHub
parent a4ff959aa1
commit 145a5a8a8d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
89 changed files with 520 additions and 551 deletions

View file

@ -33,21 +33,23 @@ describe("SlashCommands", () => {
let localRoom: LocalRoom;
let command: Command;
const findCommand = (cmd: string): Command => {
const findCommand = (cmd: string): Command | undefined => {
return Commands.find((command: Command) => command.command === cmd);
};
const setCurrentRoom = (): void => {
mocked(SdkContextClass.instance.roomViewStore.getRoomId).mockReturnValue(roomId);
mocked(client.getRoom).mockImplementation((rId: string): Room => {
mocked(client.getRoom).mockImplementation((rId: string): Room | null => {
if (rId === roomId) return room;
return null;
});
};
const setCurrentLocalRoon = (): void => {
mocked(SdkContextClass.instance.roomViewStore.getRoomId).mockReturnValue(localRoomId);
mocked(client.getRoom).mockImplementation((rId: string): Room => {
mocked(client.getRoom).mockImplementation((rId: string): Room | null => {
if (rId === localRoomId) return localRoom;
return null;
});
};
@ -57,8 +59,8 @@ describe("SlashCommands", () => {
client = createTestClient();
jest.spyOn(MatrixClientPeg, "get").mockReturnValue(client);
room = new Room(roomId, client, client.getUserId());
localRoom = new LocalRoom(localRoomId, client, client.getUserId());
room = new Room(roomId, client, client.getUserId()!);
localRoom = new LocalRoom(localRoomId, client, client.getUserId()!);
jest.spyOn(SdkContextClass.instance.roomViewStore, "getRoomId");
});
@ -68,7 +70,7 @@ 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("room-id", null, command.args);
expect(client.setRoomTopic).toHaveBeenCalledWith("room-id", "pizza", undefined);
});
});
@ -96,7 +98,7 @@ describe("SlashCommands", () => {
["converttoroom"],
])("/%s", (commandName: string) => {
beforeEach(() => {
command = findCommand(commandName);
command = findCommand(commandName)!;
});
describe("isEnabled", () => {
@ -114,7 +116,7 @@ describe("SlashCommands", () => {
describe("/tovirtual", () => {
beforeEach(() => {
command = findCommand("tovirtual");
command = findCommand("tovirtual")!;
});
describe("isEnabled", () => {
@ -154,7 +156,7 @@ describe("SlashCommands", () => {
describe("/remakeolm", () => {
beforeEach(() => {
command = findCommand("remakeolm");
command = findCommand("remakeolm")!;
});
describe("isEnabled", () => {
@ -198,39 +200,39 @@ describe("SlashCommands", () => {
describe("/part", () => {
it("should part room matching alias if found", async () => {
const room1 = new Room("room-id", client, client.getUserId());
const room1 = new Room("room-id", client, client.getUserId()!);
room1.getCanonicalAlias = jest.fn().mockReturnValue("#foo:bar");
const room2 = new Room("other-room", client, client.getUserId());
const room2 = new Room("other-room", client, client.getUserId()!);
room2.getCanonicalAlias = jest.fn().mockReturnValue("#baz:bar");
mocked(client.getRooms).mockReturnValue([room1, room2]);
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("room-id", null, command.args);
expect(client.leaveRoomChain).toHaveBeenCalledWith("room-id", expect.anything());
});
it("should part room matching alt alias if found", async () => {
const room1 = new Room("room-id", client, client.getUserId());
const room1 = new Room("room-id", client, client.getUserId()!);
room1.getAltAliases = jest.fn().mockReturnValue(["#foo:bar"]);
const room2 = new Room("other-room", client, client.getUserId());
const room2 = new Room("other-room", client, client.getUserId()!);
room2.getAltAliases = jest.fn().mockReturnValue(["#baz:bar"]);
mocked(client.getRooms).mockReturnValue([room1, room2]);
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("room-id", null, command.args);
expect(client.leaveRoomChain).toHaveBeenCalledWith("room-id", expect.anything());
});
});
describe.each(["rainbow", "rainbowme"])("/%s", (commandName: string) => {
const command = findCommand(commandName);
const command = findCommand(commandName)!;
it("should return usage if no args", () => {
expect(command.run(roomId, null, null).error).toBe(command.getUsage());
expect(command.run(roomId, null).error).toBe(command.getUsage());
});
it("should make things rainbowy", () => {