1
0
mirror of https://github.com/matrix-org/matrix-react-sdk.git synced 2025-08-07 21:23:00 +03:00

Conform more of the code base to strict null checking (#10147)

* Conform more of the code base to strict null checking

* More strict fixes

* More strict work

* Fix missing optional type

* Iterate
This commit is contained in:
Michael Telatynski
2023-02-13 17:01:43 +00:00
committed by GitHub
parent fa036a5080
commit da7aa4055e
380 changed files with 682 additions and 694 deletions

View File

@@ -432,7 +432,7 @@ describe("EventUtils", () => {
stubClient();
client = MatrixClientPeg.get();
room = new Room(ROOM_ID, client, client.getUserId(), {
room = new Room(ROOM_ID, client, client.getUserId()!, {
pendingEventOrdering: PendingEventOrdering.Detached,
});

View File

@@ -22,7 +22,7 @@ import type * as MegolmExportEncryptionExport from "../../src/utils/MegolmExport
const webCrypto = new Crypto();
function getRandomValues<T extends ArrayBufferView>(buf: T): T {
function getRandomValues<T extends ArrayBufferView | null>(buf: T): T {
// @ts-ignore fussy generics
return nodeCrypto.randomFillSync(buf);
}
@@ -133,7 +133,7 @@ cissyYBxjsfsAn
// TODO find a subtlecrypto shim which doesn't break this test
it.skip("should decrypt a range of inputs", function () {
function next(i: number): unknown {
function next(i: number): Promise<string | undefined> | undefined {
if (i >= TEST_VECTORS.length) {
return;
}
@@ -144,7 +144,7 @@ cissyYBxjsfsAn
return next(i + 1);
});
}
return next(0);
next(0);
});
});

View File

@@ -47,7 +47,7 @@ jest.mock("../../src/settings/SettingsStore", () => ({
const mockPromptBeforeInviteUnknownUsers = (value: boolean) => {
mocked(SettingsStore.getValue).mockImplementation(
(settingName: string, roomId: string = null, _excludeDefault = false): any => {
(settingName: string, roomId: string, _excludeDefault = false): any => {
if (settingName === "promptBeforeInviteUnknownUsers" && roomId === ROOMID) {
return value;
}
@@ -57,7 +57,7 @@ const mockPromptBeforeInviteUnknownUsers = (value: boolean) => {
const mockCreateTrackedDialog = (callbackName: "onInviteAnyways" | "onGiveUp") => {
mocked(Modal.createDialog).mockImplementation((...rest: Parameters<ModalManager["createDialog"]>): any => {
rest[1][callbackName]();
rest[1]![callbackName]();
});
};

View File

@@ -22,7 +22,7 @@ describe("Singleflight", () => {
});
it("should throw for bad context variables", () => {
const permutations: [Object, string][] = [
const permutations: [Object | null, string | null][] = [
[null, null],
[{}, null],
[null, "test"],

View File

@@ -44,9 +44,9 @@ function assertLocalRoom(room: LocalRoom, targets: Member[], encrypted: boolean)
expect(roomCreateEvent.getContent()["room_version"]).toBe(KNOWN_SAFE_ROOM_VERSION);
// check that the user and all targets are joined
expect(room.getMember("@userId:matrix.org").membership).toBe("join");
expect(room.getMember("@userId:matrix.org")?.membership).toBe("join");
targets.forEach((target: Member) => {
expect(room.getMember(target.userId).membership).toBe("join");
expect(room.getMember(target.userId)?.membership).toBe("join");
});
if (encrypted) {

View File

@@ -104,7 +104,7 @@ describe("HTMLExport", () => {
/** set a mock fetch response for an MXC */
function mockMxc(mxc: string, body: string) {
const media = mediaFromMxc(mxc, client);
fetchMock.get(media.srcHttp, body);
fetchMock.get(media.srcHttp!, body);
}
it("should have an SDK-branded destination file name", () => {
@@ -286,7 +286,7 @@ describe("HTMLExport", () => {
// Ensure that the attachment is present
const files = getFiles(exporter);
const file = files[Object.keys(files).find((k) => k.endsWith(".txt"))];
const file = files[Object.keys(files).find((k) => k.endsWith(".txt"))!];
expect(file).not.toBeUndefined();
// Ensure that the attachment has the expected content

View File

@@ -26,12 +26,8 @@ describe("isLocalRoom", () => {
beforeEach(() => {
const client = createTestClient();
room = new Room("!room:example.com", client, client.getUserId());
localRoom = new LocalRoom(LOCAL_ROOM_ID_PREFIX + "test", client, client.getUserId());
});
it("should return false for null", () => {
expect(isLocalRoom(null)).toBe(false);
room = new Room("!room:example.com", client, client.getUserId()!);
localRoom = new LocalRoom(LOCAL_ROOM_ID_PREFIX + "test", client, client.getUserId()!);
});
it("should return false for a Room", () => {

View File

@@ -59,6 +59,7 @@ describe("isRoomReady", () => {
beforeEach(() => {
mocked(client.getRoom).mockImplementation((roomId: string) => {
if (roomId === room1.roomId) return room1;
return null;
});
});

View File

@@ -53,7 +53,7 @@ describe("notifications", () => {
}),
});
accountDataStore = {};
accountDataEventKey = getLocalNotificationAccountDataEventType(mockClient.deviceId);
accountDataEventKey = getLocalNotificationAccountDataEventType(mockClient.deviceId!);
mocked(SettingsStore).getValue.mockReturnValue(false);
});

View File

@@ -74,7 +74,7 @@ describe("Permalinks", function () {
jest.spyOn(room, "getCanonicalAlias").mockReturnValue(null);
jest.spyOn(room, "getJoinedMembers").mockReturnValue(members);
jest.spyOn(room, "getMember").mockImplementation((userId) => members.find((m) => m.userId === userId));
jest.spyOn(room, "getMember").mockImplementation((userId) => members.find((m) => m.userId === userId) || null);
return room;
}
@@ -369,22 +369,22 @@ describe("Permalinks", function () {
it("should correctly parse room permalinks with a via argument", () => {
const result = parsePermalink("https://matrix.to/#/!room_id:server?via=some.org");
expect(result.roomIdOrAlias).toBe("!room_id:server");
expect(result.viaServers).toEqual(["some.org"]);
expect(result?.roomIdOrAlias).toBe("!room_id:server");
expect(result?.viaServers).toEqual(["some.org"]);
});
it("should correctly parse room permalink via arguments", () => {
const result = parsePermalink("https://matrix.to/#/!room_id:server?via=foo.bar&via=bar.foo");
expect(result.roomIdOrAlias).toBe("!room_id:server");
expect(result.viaServers).toEqual(["foo.bar", "bar.foo"]);
expect(result?.roomIdOrAlias).toBe("!room_id:server");
expect(result?.viaServers).toEqual(["foo.bar", "bar.foo"]);
});
it("should correctly parse event permalink via arguments", () => {
const result = parsePermalink(
"https://matrix.to/#/!room_id:server/$event_id/some_thing_here/foobar" + "?via=m1.org&via=m2.org",
);
expect(result.eventId).toBe("$event_id/some_thing_here/foobar");
expect(result.roomIdOrAlias).toBe("!room_id:server");
expect(result.viaServers).toEqual(["m1.org", "m2.org"]);
expect(result?.eventId).toBe("$event_id/some_thing_here/foobar");
expect(result?.roomIdOrAlias).toBe("!room_id:server");
expect(result?.viaServers).toEqual(["m1.org", "m2.org"]);
});
});

View File

@@ -37,8 +37,8 @@ describe("pillify", () => {
beforeEach(() => {
stubClient();
const cli = MatrixClientPeg.get();
(cli.getRoom as jest.Mock).mockReturnValue(new Room(roomId, cli, cli.getUserId()));
cli.pushRules.global = {
(cli.getRoom as jest.Mock).mockReturnValue(new Room(roomId, cli, cli.getUserId()!));
cli.pushRules!.global = {
override: [
{
rule_id: ".m.rule.roomnotif",
@@ -79,7 +79,7 @@ describe("pillify", () => {
const containers: Element[] = [];
pillifyLinks([container], event, containers);
expect(containers).toHaveLength(1);
expect(container.querySelector(".mx_Pill.mx_AtRoomPill").textContent).toBe("!@room");
expect(container.querySelector(".mx_Pill.mx_AtRoomPill")?.textContent).toBe("!@room");
});
it("should not double up pillification on repeated calls", () => {
@@ -90,6 +90,6 @@ describe("pillify", () => {
pillifyLinks([container], event, containers);
pillifyLinks([container], event, containers);
expect(containers).toHaveLength(1);
expect(container.querySelector(".mx_Pill.mx_AtRoomPill").textContent).toBe("!@room");
expect(container.querySelector(".mx_Pill.mx_AtRoomPill")?.textContent).toBe("!@room");
});
});

View File

@@ -21,7 +21,7 @@ import { createTestClient, mkEvent } from "../../test-utils";
describe("getRoomFunctionalMembers", () => {
const client = createTestClient();
const room = new Room("!room:example.com", client, client.getUserId());
const room = new Room("!room:example.com", client, client.getUserId()!);
it("should return an empty array if no functional members state event exists", () => {
expect(getFunctionalMembers(room)).toHaveLength(0);

View File

@@ -44,7 +44,7 @@ describe("tooltipify", () => {
expect(containers).toHaveLength(1);
const anchor = root.querySelector("a");
expect(anchor?.getAttribute("href")).toEqual("/foo");
const tooltip = anchor.querySelector(".mx_TextWithTooltip_target");
const tooltip = anchor!.querySelector(".mx_TextWithTooltip_target");
expect(tooltip).toBeDefined();
});
@@ -75,7 +75,7 @@ describe("tooltipify", () => {
expect(containers).toHaveLength(1);
const anchor = root.querySelector("a");
expect(anchor?.getAttribute("href")).toEqual("/foo");
const tooltip = anchor.querySelector(".mx_TextWithTooltip_target");
const tooltip = anchor!.querySelector(".mx_TextWithTooltip_target");
expect(tooltip).toBeDefined();
});
});