You've already forked matrix-react-sdk
mirror of
https://github.com/matrix-org/matrix-react-sdk.git
synced 2025-07-31 13:44:28 +03:00
Apply strictNullChecks
to src/utils/*!exportUtils
(#10455
* Apply `strictNullChecks` to `src/utils/exportUtils` * strict fix * fix strictNullChecks issues in some utils * fix error message * test coverage * lint * more strictNullChecks * small optimisation for getUniqueRoomsWithIndividuals * tidy * test coverage
This commit is contained in:
@ -16,7 +16,7 @@ limitations under the License.
|
||||
|
||||
import { mocked, Mocked } from "jest-mock";
|
||||
import { logger } from "matrix-js-sdk/src/logger";
|
||||
import { ClientEvent, EventType, IContent, MatrixClient, MatrixEvent } from "matrix-js-sdk/src/matrix";
|
||||
import { ClientEvent, EventType, IContent, MatrixClient, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
|
||||
|
||||
import DMRoomMap from "../../src/utils/DMRoomMap";
|
||||
import { mkEvent, stubClient } from "../test-utils";
|
||||
@ -137,4 +137,56 @@ describe("DMRoomMap", () => {
|
||||
expect(dmRoomMap.getRoomIds()).toEqual(new Set([roomId1, roomId2, roomId4]));
|
||||
});
|
||||
});
|
||||
|
||||
describe("getUniqueRoomsWithIndividuals()", () => {
|
||||
const bigRoom = {
|
||||
roomId: "!bigRoom:server.org",
|
||||
getInvitedAndJoinedMemberCount: jest.fn().mockReturnValue(5000),
|
||||
} as unknown as Room;
|
||||
const dmWithBob = {
|
||||
roomId: "!dmWithBob:server.org",
|
||||
getInvitedAndJoinedMemberCount: jest.fn().mockReturnValue(2),
|
||||
} as unknown as Room;
|
||||
const dmWithCharlie = {
|
||||
roomId: "!dmWithCharlie:server.org",
|
||||
getInvitedAndJoinedMemberCount: jest.fn().mockReturnValue(2),
|
||||
} as unknown as Room;
|
||||
const smallRoom = {
|
||||
roomId: "!smallRoom:server.org",
|
||||
getInvitedAndJoinedMemberCount: jest.fn().mockReturnValue(3),
|
||||
} as unknown as Room;
|
||||
|
||||
const mDirectContent = {
|
||||
"@bob:server.org": [bigRoom.roomId, dmWithBob.roomId, smallRoom.roomId],
|
||||
"@charlie:server.org": [dmWithCharlie.roomId, smallRoom.roomId],
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
client.getAccountData.mockReturnValue(mkMDirectEvent(mDirectContent));
|
||||
client.getRoom.mockImplementation((roomId: string) =>
|
||||
[bigRoom, smallRoom, dmWithCharlie, dmWithBob].find((room) => room.roomId === roomId),
|
||||
);
|
||||
});
|
||||
|
||||
it("returns an empty object when room map has not been populated", () => {
|
||||
const instance = new DMRoomMap(client);
|
||||
expect(instance.getUniqueRoomsWithIndividuals()).toEqual({});
|
||||
});
|
||||
|
||||
it("returns map of users to rooms with 2 members", () => {
|
||||
const dmRoomMap = new DMRoomMap(client);
|
||||
dmRoomMap.start();
|
||||
expect(dmRoomMap.getUniqueRoomsWithIndividuals()).toEqual({
|
||||
"@bob:server.org": dmWithBob,
|
||||
"@charlie:server.org": dmWithCharlie,
|
||||
});
|
||||
});
|
||||
|
||||
it("excludes rooms that are not found by matrixClient", () => {
|
||||
client.getRoom.mockReset().mockReturnValue(undefined);
|
||||
const dmRoomMap = new DMRoomMap(client);
|
||||
dmRoomMap.start();
|
||||
expect(dmRoomMap.getUniqueRoomsWithIndividuals()).toEqual({});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -31,15 +31,23 @@ describe("createMapSiteLinkFromEvent", () => {
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it("returns OpenStreetMap link if event contains m.location", () => {
|
||||
it("returns OpenStreetMap link if event contains m.location with valid uri", () => {
|
||||
expect(createMapSiteLinkFromEvent(makeLocationEvent("geo:51.5076,-0.1276"))).toEqual(
|
||||
"https://www.openstreetmap.org/" + "?mlat=51.5076&mlon=-0.1276" + "#map=16/51.5076/-0.1276",
|
||||
);
|
||||
});
|
||||
|
||||
it("returns null if event contains m.location with invalid uri", () => {
|
||||
expect(createMapSiteLinkFromEvent(makeLocationEvent("123 Sesame St"))).toBeNull();
|
||||
});
|
||||
|
||||
it("returns OpenStreetMap link if event contains geo_uri", () => {
|
||||
expect(createMapSiteLinkFromEvent(makeLegacyLocationEvent("geo:51.5076,-0.1276"))).toEqual(
|
||||
"https://www.openstreetmap.org/" + "?mlat=51.5076&mlon=-0.1276" + "#map=16/51.5076/-0.1276",
|
||||
);
|
||||
});
|
||||
|
||||
it("returns null if event contains an invalid geo_uri", () => {
|
||||
expect(createMapSiteLinkFromEvent(makeLegacyLocationEvent("123 Sesame St"))).toBeNull();
|
||||
});
|
||||
});
|
||||
|
@ -21,6 +21,14 @@ describe("parseGeoUri", () => {
|
||||
expect(parseGeoUri("")).toBeFalsy();
|
||||
});
|
||||
|
||||
it("returns undefined if latitude is not a number", () => {
|
||||
expect(parseGeoUri("geo:ABCD,16.3695,183")).toBeUndefined();
|
||||
});
|
||||
|
||||
it("returns undefined if longitude is not a number", () => {
|
||||
expect(parseGeoUri("geo:48.2010,EFGH,183")).toBeUndefined();
|
||||
});
|
||||
|
||||
// We use some examples from the spec, but don't check semantics
|
||||
// like two textually-different URIs being equal, since we are
|
||||
// just a humble parser.
|
||||
|
Reference in New Issue
Block a user