Files
element-call/src/UrlParams.test.ts
T

199 lines
5.2 KiB
TypeScript
Raw Normal View History

2023-07-03 14:59:26 +02:00
/*
Copyright 2023, 2024 New Vector Ltd.
2023-07-03 14:59:26 +02:00
SPDX-License-Identifier: AGPL-3.0-only
Please see LICENSE in the repository root for full details.
2023-07-03 14:59:26 +02:00
*/
import { describe, expect, it } from "vitest";
2023-09-18 17:49:10 +01:00
import { getRoomIdentifierFromUrl, getUrlParams } from "../src/UrlParams";
2023-07-03 14:59:26 +02:00
const ROOM_NAME = "roomNameHere";
2023-07-29 20:46:45 +02:00
const ROOM_ID = "!d45f138fsd";
2023-07-03 14:59:26 +02:00
const ORIGIN = "https://call.element.io";
const HOMESERVER = "localhost";
2023-07-03 14:59:26 +02:00
describe("UrlParams", () => {
describe("handles URL with /room/", () => {
it("and nothing else", () => {
2023-09-18 17:49:10 +01:00
expect(
2023-10-11 10:42:04 -04:00
getRoomIdentifierFromUrl(`/room/${ROOM_NAME}`, "", "").roomAlias,
2023-09-18 17:49:10 +01:00
).toBe(`#${ROOM_NAME}:${HOMESERVER}`);
2023-07-03 14:59:26 +02:00
});
it("and #", () => {
expect(
2023-09-18 17:49:10 +01:00
getRoomIdentifierFromUrl("", `${ORIGIN}/room/`, `#${ROOM_NAME}`)
2023-10-11 10:42:04 -04:00
.roomAlias,
2023-07-03 14:59:26 +02:00
).toBe(`#${ROOM_NAME}:${HOMESERVER}`);
});
it("and # and server part", () => {
expect(
2023-09-18 17:49:10 +01:00
getRoomIdentifierFromUrl("", `/room/`, `#${ROOM_NAME}:${HOMESERVER}`)
2023-10-11 10:42:04 -04:00
.roomAlias,
2023-07-03 14:59:26 +02:00
).toBe(`#${ROOM_NAME}:${HOMESERVER}`);
});
it("and server part", () => {
expect(
2023-09-18 17:49:10 +01:00
getRoomIdentifierFromUrl(`/room/${ROOM_NAME}:${HOMESERVER}`, "", "")
2023-10-11 10:42:04 -04:00
.roomAlias,
2023-07-03 14:59:26 +02:00
).toBe(`#${ROOM_NAME}:${HOMESERVER}`);
});
});
describe("handles URL without /room/", () => {
it("and nothing else", () => {
2023-09-18 17:49:10 +01:00
expect(getRoomIdentifierFromUrl(`/${ROOM_NAME}`, "", "").roomAlias).toBe(
2023-10-11 10:42:04 -04:00
`#${ROOM_NAME}:${HOMESERVER}`,
2023-07-03 20:05:08 +02:00
);
2023-07-03 14:59:26 +02:00
});
it("and with #", () => {
2023-09-18 17:49:10 +01:00
expect(getRoomIdentifierFromUrl("", "", `#${ROOM_NAME}`).roomAlias).toBe(
2023-10-11 10:42:04 -04:00
`#${ROOM_NAME}:${HOMESERVER}`,
2023-07-03 20:05:08 +02:00
);
2023-07-03 14:59:26 +02:00
});
it("and with # and server part", () => {
expect(
2023-09-18 17:49:10 +01:00
getRoomIdentifierFromUrl("", "", `#${ROOM_NAME}:${HOMESERVER}`)
2023-10-11 10:42:04 -04:00
.roomAlias,
2023-07-03 14:59:26 +02:00
).toBe(`#${ROOM_NAME}:${HOMESERVER}`);
});
it("and with server part", () => {
expect(
2023-09-18 17:49:10 +01:00
getRoomIdentifierFromUrl(`/${ROOM_NAME}:${HOMESERVER}`, "", "")
2023-10-11 10:42:04 -04:00
.roomAlias,
2023-07-03 14:59:26 +02:00
).toBe(`#${ROOM_NAME}:${HOMESERVER}`);
});
});
describe("handles search params", () => {
it("(roomId)", () => {
2023-09-18 17:49:10 +01:00
expect(
2023-10-11 10:42:04 -04:00
getRoomIdentifierFromUrl("", `?roomId=${ROOM_ID}`, "").roomId,
2023-09-18 17:49:10 +01:00
).toBe(ROOM_ID);
2023-07-03 14:59:26 +02:00
});
});
it("ignores room alias", () => {
expect(
2023-09-18 17:49:10 +01:00
getRoomIdentifierFromUrl("", `/room/${ROOM_NAME}:${HOMESERVER}`, "")
2023-10-11 10:42:04 -04:00
.roomAlias,
2023-07-03 14:59:26 +02:00
).toBeFalsy();
});
describe("preload", () => {
it("defaults to false", () => {
expect(getUrlParams().preload).toBe(false);
});
it("ignored in SPA mode", () => {
expect(getUrlParams("?preload=true").preload).toBe(false);
});
it("respected in widget mode", () => {
expect(
getUrlParams(
"?preload=true&widgetId=12345&parentUrl=https%3A%2F%2Flocalhost%2Ffoo",
).preload,
).toBe(true);
});
});
describe("returnToLobby", () => {
it("is true in SPA mode", () => {
expect(getUrlParams("?returnToLobby=false").returnToLobby).toBe(true);
});
it("defaults to false in widget mode", () => {
expect(
getUrlParams("?widgetId=12345&parentUrl=https%3A%2F%2Flocalhost%2Ffoo")
.returnToLobby,
).toBe(false);
});
it("respected in widget mode", () => {
expect(
getUrlParams(
"?returnToLobby=true&widgetId=12345&parentUrl=https%3A%2F%2Flocalhost%2Ffoo",
).returnToLobby,
).toBe(true);
});
});
describe("userId", () => {
it("is ignored in SPA mode", () => {
expect(getUrlParams("?userId=asd").userId).toBe(null);
});
it("is parsed in widget mode", () => {
expect(
getUrlParams(
"?userId=asd&widgetId=12345&parentUrl=https%3A%2F%2Flocalhost%2Ffoo",
).userId,
).toBe("asd");
});
});
describe("deviceId", () => {
it("is ignored in SPA mode", () => {
expect(getUrlParams("?deviceId=asd").deviceId).toBe(null);
});
it("is parsed in widget mode", () => {
expect(
getUrlParams(
"?deviceId=asd&widgetId=12345&parentUrl=https%3A%2F%2Flocalhost%2Ffoo",
).deviceId,
).toBe("asd");
});
});
describe("baseUrl", () => {
it("is ignored in SPA mode", () => {
expect(getUrlParams("?baseUrl=asd").baseUrl).toBe(null);
});
it("is parsed in widget mode", () => {
expect(
getUrlParams(
"?baseUrl=asd&widgetId=12345&parentUrl=https%3A%2F%2Flocalhost%2Ffoo",
).baseUrl,
).toBe("asd");
});
});
describe("viaServers", () => {
it("is ignored in widget mode", () => {
expect(
getUrlParams(
"?viaServers=asd&widgetId=12345&parentUrl=https%3A%2F%2Flocalhost%2Ffoo",
).viaServers,
).toBe(null);
});
it("is parsed in SPA mode", () => {
expect(getUrlParams("?viaServers=asd").viaServers).toBe("asd");
});
});
describe("homeserver", () => {
it("is ignored in widget mode", () => {
expect(
getUrlParams(
"?homeserver=asd&widgetId=12345&parentUrl=https%3A%2F%2Flocalhost%2Ffoo",
).homeserver,
).toBe(null);
});
it("is parsed in SPA mode", () => {
expect(getUrlParams("?homeserver=asd").homeserver).toBe("asd");
});
});
2023-07-03 14:59:26 +02:00
});