mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
fix: unit-tests
Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
parent
bce2eb7f96
commit
250a2d4d2c
@ -1,4 +1,4 @@
|
|||||||
import { getExtensionPageUrl, globalPageRegistry } from "../page-registry";
|
import { getExtensionPageUrl, globalPageRegistry, PageTargetParams } from "../page-registry";
|
||||||
import { LensExtension } from "../../lens-extension";
|
import { LensExtension } from "../../lens-extension";
|
||||||
import React from "react";
|
import React from "react";
|
||||||
|
|
||||||
@ -17,6 +17,16 @@ describe("getPageUrl", () => {
|
|||||||
isBundled: false,
|
isBundled: false,
|
||||||
isEnabled: true
|
isEnabled: true
|
||||||
});
|
});
|
||||||
|
globalPageRegistry.add({
|
||||||
|
id: "page-with-params",
|
||||||
|
components: {
|
||||||
|
Page: () => React.createElement("Page with params")
|
||||||
|
},
|
||||||
|
params: {
|
||||||
|
test1: "test1-default",
|
||||||
|
test2: "" // no default value, just declaration
|
||||||
|
},
|
||||||
|
}, ext);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("returns a page url for extension", () => {
|
it("returns a page url for extension", () => {
|
||||||
@ -34,6 +44,24 @@ describe("getPageUrl", () => {
|
|||||||
it("adds / prefix", () => {
|
it("adds / prefix", () => {
|
||||||
expect(getExtensionPageUrl({ extensionId: ext.name, pageId: "test" })).toBe("/extension/foo-bar/test");
|
expect(getExtensionPageUrl({ extensionId: ext.name, pageId: "test" })).toBe("/extension/foo-bar/test");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("normalize possible multi-slashes in page.id", () => {
|
||||||
|
expect(getExtensionPageUrl({ extensionId: ext.name, pageId: "//test/" })).toBe("/extension/foo-bar/test");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("gets page url with custom params", () => {
|
||||||
|
const params: PageTargetParams<string> = { test1: "one", test2: "2" };
|
||||||
|
const searchParams = new URLSearchParams(params);
|
||||||
|
const pageUrl = getExtensionPageUrl({ extensionId: ext.name, pageId: "page-with-params", params });
|
||||||
|
|
||||||
|
expect(pageUrl).toBe(`/extension/foo-bar/page-with-params?${searchParams}`);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("gets page url with default custom params", () => {
|
||||||
|
const defaultPageUrl = getExtensionPageUrl({ extensionId: ext.name, pageId: "page-with-params", });
|
||||||
|
|
||||||
|
expect(defaultPageUrl).toBe(`/extension/foo-bar/page-with-params?test1=test1-default`);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("globalPageRegistry", () => {
|
describe("globalPageRegistry", () => {
|
||||||
|
|||||||
@ -41,22 +41,29 @@ export interface RegisteredPage extends PageRegistration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function getExtensionPageUrl(target: PageTarget): string {
|
export function getExtensionPageUrl(target: PageTarget): string {
|
||||||
const { extensionId, pageId = "", params: targetPageParams = {} } = target;
|
const { extensionId, pageId = "", params: targetParams = {} } = target;
|
||||||
|
|
||||||
|
const pagePath = ["/extension", sanitizeExtensionName(extensionId), pageId]
|
||||||
|
.filter(Boolean)
|
||||||
|
.join("/").replace(/\/+/g, "/").replace(/\/$/, ""); // normalize multi-slashes (e.g. coming from page.id)
|
||||||
|
|
||||||
const pagePath = ["/extension", sanitizeExtensionName(extensionId), pageId].join("/");
|
|
||||||
const pageUrl = new URL(pagePath, `http://localhost`);
|
const pageUrl = new URL(pagePath, `http://localhost`);
|
||||||
|
|
||||||
// stringify params to matched target page
|
// stringify params to matched target page
|
||||||
const targetPage = globalPageRegistry.getByPageTarget(target) || clusterPageRegistry.getByPageTarget(target);
|
const registeredPage = globalPageRegistry.getByPageTarget(target) || clusterPageRegistry.getByPageTarget(target);
|
||||||
|
|
||||||
if (targetPage?.params) {
|
if (registeredPage?.params) {
|
||||||
Object.entries(targetPage.params).forEach(([name, param]) => {
|
Object.entries(registeredPage.params).forEach(([name, param]) => {
|
||||||
const paramValue = targetPageParams[name];
|
const targetParamValue = targetParams[name];
|
||||||
|
|
||||||
if (param instanceof UrlParam) {
|
if (param instanceof UrlParam) {
|
||||||
pageUrl.searchParams.set(name, param.stringify(paramValue));
|
pageUrl.searchParams.set(name, param.stringify(targetParamValue));
|
||||||
} else {
|
} else {
|
||||||
pageUrl.searchParams.set(name, String(paramValue ?? param));
|
const value = String(targetParamValue ?? param);
|
||||||
|
|
||||||
|
if (value) {
|
||||||
|
pageUrl.searchParams.set(name, value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user