File tree 3 files changed +40
-1
lines changed
3 files changed +40
-1
lines changed Original file line number Diff line number Diff line change
1
+ import { includeOrigin } from "./MobileMenu" ;
2
+
3
+ const mockOrigin = "https://example.com" ;
4
+
5
+ describe ( "support link" , ( ) => {
6
+ it ( "should include origin if target starts with '/'" , ( ) => {
7
+ ( window as unknown as { location : Partial < Location > } ) . location = {
8
+ origin : mockOrigin ,
9
+ } ; // Mock the location origin
10
+
11
+ expect ( includeOrigin ( "/test" ) ) . toBe ( `${ mockOrigin } /test` ) ;
12
+ expect ( includeOrigin ( "/path/to/resource" ) ) . toBe (
13
+ `${ mockOrigin } /path/to/resource` ,
14
+ ) ;
15
+ } ) ;
16
+
17
+ it ( "should return the target unchanged if it does not start with '/'" , ( ) => {
18
+ expect ( includeOrigin ( `${ mockOrigin } /page` ) ) . toBe ( `${ mockOrigin } /page` ) ;
19
+ expect ( includeOrigin ( "../relative/path" ) ) . toBe ( "../relative/path" ) ;
20
+ expect ( includeOrigin ( "relative/path" ) ) . toBe ( "relative/path" ) ;
21
+ } ) ;
22
+ } ) ;
Original file line number Diff line number Diff line change @@ -307,7 +307,11 @@ const UserSettingsSub: FC<UserSettingsSubProps> = ({
307
307
asChild
308
308
className = { cn ( itemStyles . default , itemStyles . sub ) }
309
309
>
310
- < a href = { l . target } target = "_blank" rel = "noreferrer" >
310
+ < a
311
+ href = { includeOrigin ( l . target ) }
312
+ target = "_blank"
313
+ rel = "noreferrer"
314
+ >
311
315
{ l . name }
312
316
</ a >
313
317
</ DropdownMenuItem >
@@ -318,3 +322,11 @@ const UserSettingsSub: FC<UserSettingsSubProps> = ({
318
322
</ Collapsible >
319
323
) ;
320
324
} ;
325
+
326
+ export const includeOrigin = ( target : string ) : string => {
327
+ if ( target . startsWith ( "/" ) ) {
328
+ const baseUrl = window . location . origin ;
329
+ return `${ baseUrl } ${ target } ` ;
330
+ }
331
+ return target ;
332
+ } ;
Original file line number Diff line number Diff line change @@ -246,6 +246,11 @@ export const MockSupportLinks: TypesGen.LinkConfig[] = [
246
246
"https://github.com/coder/coder/issues/new?labels=needs+grooming&body={CODER_BUILD_INFO}" ,
247
247
icon : "" ,
248
248
} ,
249
+ {
250
+ name : "Fourth link" ,
251
+ target : "/icons" ,
252
+ icon : "" ,
253
+ } ,
249
254
] ;
250
255
251
256
export const MockUpdateCheck : TypesGen . UpdateCheckResponse = {
You can’t perform that action at this time.
0 commit comments