diff --git a/site/src/modules/dashboard/Navbar/MobileMenu.test.tsx b/site/src/modules/dashboard/Navbar/MobileMenu.test.tsx new file mode 100644 index 0000000000000..ce8a29df78fc4 --- /dev/null +++ b/site/src/modules/dashboard/Navbar/MobileMenu.test.tsx @@ -0,0 +1,22 @@ +import { includeOrigin } from "./MobileMenu"; + +const mockOrigin = "https://example.com"; + +describe("support link", () => { + it("should include origin if target starts with '/'", () => { + (window as unknown as { location: Partial }).location = { + origin: mockOrigin, + }; // Mock the location origin + + expect(includeOrigin("/test")).toBe(`${mockOrigin}/test`); + expect(includeOrigin("/path/to/resource")).toBe( + `${mockOrigin}/path/to/resource`, + ); + }); + + it("should return the target unchanged if it does not start with '/'", () => { + expect(includeOrigin(`${mockOrigin}/page`)).toBe(`${mockOrigin}/page`); + expect(includeOrigin("../relative/path")).toBe("../relative/path"); + expect(includeOrigin("relative/path")).toBe("relative/path"); + }); +}); diff --git a/site/src/modules/dashboard/Navbar/MobileMenu.tsx b/site/src/modules/dashboard/Navbar/MobileMenu.tsx index f24755a5c4c17..20058335eb8e5 100644 --- a/site/src/modules/dashboard/Navbar/MobileMenu.tsx +++ b/site/src/modules/dashboard/Navbar/MobileMenu.tsx @@ -307,7 +307,11 @@ const UserSettingsSub: FC = ({ asChild className={cn(itemStyles.default, itemStyles.sub)} > - + {l.name} @@ -318,3 +322,11 @@ const UserSettingsSub: FC = ({ ); }; + +export const includeOrigin = (target: string): string => { + if (target.startsWith("/")) { + const baseUrl = window.location.origin; + return `${baseUrl}${target}`; + } + return target; +}; diff --git a/site/src/testHelpers/entities.ts b/site/src/testHelpers/entities.ts index a607df6bb87c9..c866c64f15b4e 100644 --- a/site/src/testHelpers/entities.ts +++ b/site/src/testHelpers/entities.ts @@ -246,6 +246,11 @@ export const MockSupportLinks: TypesGen.LinkConfig[] = [ "https://github.com/coder/coder/issues/new?labels=needs+grooming&body={CODER_BUILD_INFO}", icon: "", }, + { + name: "Fourth link", + target: "/icons", + icon: "", + }, ]; export const MockUpdateCheck: TypesGen.UpdateCheckResponse = {