element-call-Github/test/UrlParams-test.ts

106 lines
2.9 KiB
TypeScript
Raw Normal View History

/*
Copyright 2023 New Vector Ltd
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { vi } from "vitest";
import { getRoomIdentifierFromUrl } from "../src/UrlParams";
import { Config } from "../src/config/Config";
const ROOM_NAME = "roomNameHere";
const ROOM_ID = "!d45f138fsd";
const ORIGIN = "https://call.element.io";
const HOMESERVER = "call.ems.host";
vi.mock("../src/config/Config");
describe("UrlParams", () => {
beforeAll(() => {
vi.mocked(Config.defaultServerName).mockReturnValue("call.ems.host");
});
describe("handles URL with /room/", () => {
it("and nothing else", () => {
expect(
2023-10-11 22:42:04 +08:00
getRoomIdentifierFromUrl(`/room/${ROOM_NAME}`, "", "").roomAlias,
).toBe(`#${ROOM_NAME}:${HOMESERVER}`);
});
it("and #", () => {
expect(
getRoomIdentifierFromUrl("", `${ORIGIN}/room/`, `#${ROOM_NAME}`)
2023-10-11 22:42:04 +08:00
.roomAlias,
).toBe(`#${ROOM_NAME}:${HOMESERVER}`);
});
it("and # and server part", () => {
expect(
getRoomIdentifierFromUrl("", `/room/`, `#${ROOM_NAME}:${HOMESERVER}`)
2023-10-11 22:42:04 +08:00
.roomAlias,
).toBe(`#${ROOM_NAME}:${HOMESERVER}`);
});
it("and server part", () => {
expect(
getRoomIdentifierFromUrl(`/room/${ROOM_NAME}:${HOMESERVER}`, "", "")
2023-10-11 22:42:04 +08:00
.roomAlias,
).toBe(`#${ROOM_NAME}:${HOMESERVER}`);
});
});
describe("handles URL without /room/", () => {
it("and nothing else", () => {
expect(getRoomIdentifierFromUrl(`/${ROOM_NAME}`, "", "").roomAlias).toBe(
2023-10-11 22:42:04 +08:00
`#${ROOM_NAME}:${HOMESERVER}`,
);
});
it("and with #", () => {
expect(getRoomIdentifierFromUrl("", "", `#${ROOM_NAME}`).roomAlias).toBe(
2023-10-11 22:42:04 +08:00
`#${ROOM_NAME}:${HOMESERVER}`,
);
});
it("and with # and server part", () => {
expect(
getRoomIdentifierFromUrl("", "", `#${ROOM_NAME}:${HOMESERVER}`)
2023-10-11 22:42:04 +08:00
.roomAlias,
).toBe(`#${ROOM_NAME}:${HOMESERVER}`);
});
it("and with server part", () => {
expect(
getRoomIdentifierFromUrl(`/${ROOM_NAME}:${HOMESERVER}`, "", "")
2023-10-11 22:42:04 +08:00
.roomAlias,
).toBe(`#${ROOM_NAME}:${HOMESERVER}`);
});
});
describe("handles search params", () => {
it("(roomId)", () => {
expect(
2023-10-11 22:42:04 +08:00
getRoomIdentifierFromUrl("", `?roomId=${ROOM_ID}`, "").roomId,
).toBe(ROOM_ID);
});
});
it("ignores room alias", () => {
expect(
getRoomIdentifierFromUrl("", `/room/${ROOM_NAME}:${HOMESERVER}`, "")
2023-10-11 22:42:04 +08:00
.roomAlias,
).toBeFalsy();
});
});