1- import { describe , expect , it } from 'vitest'
1+ import { afterEach , describe , expect , it } from 'vitest'
22
33import { makeUrlAbsolute } from './make-url-absolute'
44
55/**
66 * @vitest -environment jsdom
77 */
88describe ( 'makeUrlAbsolute' , ( ) => {
9- it ( 'returns undefined for undefined input' , ( ) => {
10- expect ( makeUrlAbsolute ( undefined ) ) . toBeUndefined ( )
9+ const originalOrigin = window . location . origin
10+
11+ afterEach ( ( ) => {
12+ // Restore original window.location.origin
13+ Object . defineProperty ( window , 'location' , {
14+ value : { origin : originalOrigin } ,
15+ writable : true ,
16+ } )
1117 } )
1218
1319 it ( 'returns the same URL for absolute URLs' , ( ) => {
@@ -17,70 +23,96 @@ describe('makeUrlAbsolute', () => {
1723
1824 it ( 'converts relative URLs to absolute URLs' , ( ) => {
1925 // Mock window.location.href
20- const originalHref = window . location . href
2126 Object . defineProperty ( window , 'location' , {
2227 value : { href : 'http://example.com/path/' } ,
2328 writable : true ,
2429 } )
2530
2631 expect ( makeUrlAbsolute ( 'relative' ) ) . toBe ( 'http://example.com/path/relative' )
2732 expect ( makeUrlAbsolute ( '/absolute-path' ) ) . toBe ( 'http://example.com/absolute-path' )
28-
29- // Restore original window.location.href
30- Object . defineProperty ( window , 'location' , {
31- value : { href : originalHref } ,
32- writable : true ,
33- } )
3433 } )
3534
3635 it ( 'handles base URLs without trailing slash' , ( ) => {
3736 // Mock window.location.href
38- const originalHref = window . location . href
3937 Object . defineProperty ( window , 'location' , {
4038 value : { href : 'http://example.com/path' } ,
4139 writable : true ,
4240 } )
4341
4442 expect ( makeUrlAbsolute ( 'relative' ) ) . toBe ( 'http://example.com/relative' )
45-
46- // Restore original window.location.href
47- Object . defineProperty ( window , 'location' , {
48- value : { href : originalHref } ,
49- writable : true ,
50- } )
5143 } )
5244
5345 it ( 'ignores query parameters and hash in base URL' , ( ) => {
5446 // Mock window.location.href
55- const originalHref = window . location . href
5647 Object . defineProperty ( window , 'location' , {
5748 value : { href : 'http://example.com/path?query=1#hash' } ,
5849 writable : true ,
5950 } )
6051
6152 expect ( makeUrlAbsolute ( 'relative' ) ) . toBe ( 'http://example.com/relative' )
62-
63- // Restore original window.location.href
64- Object . defineProperty ( window , 'location' , {
65- value : { href : originalHref } ,
66- writable : true ,
67- } )
6853 } )
6954
7055 it ( 'handles parent directory paths' , ( ) => {
7156 // Mock window.location.href
72- const originalHref = window . location . href
7357 Object . defineProperty ( window , 'location' , {
7458 value : { href : 'http://example.com/path/to/current/' } ,
7559 writable : true ,
7660 } )
7761
7862 expect ( makeUrlAbsolute ( '../openapi.json' ) ) . toBe ( 'http://example.com/path/to/openapi.json' )
63+ } )
7964
80- // Restore original window.location.href
81- Object . defineProperty ( window , 'location' , {
82- value : { href : originalHref } ,
83- writable : true ,
65+ it ( 'handles base URLs with a path component' , ( ) => {
66+ expect ( makeUrlAbsolute ( 'examples/openapi.json' , { baseUrl : 'http://localhost:5173/' } ) ) . toBe (
67+ 'http://localhost:5173/examples/openapi.json' ,
68+ )
69+ expect ( makeUrlAbsolute ( 'examples/openapi.json' , { baseUrl : 'http://localhost:5173' } ) ) . toBe (
70+ 'http://localhost:5173/examples/openapi.json' ,
71+ )
72+ } )
73+
74+ describe ( 'basePath functionality' , ( ) => {
75+ it ( 'combines basePath with window.location.origin when no baseUrl provided' , ( ) => {
76+ // Mock window.location.origin
77+ Object . defineProperty ( window , 'location' , {
78+ value : { origin : 'http://example.com' } ,
79+ writable : true ,
80+ } )
81+
82+ expect ( makeUrlAbsolute ( 'api/docs' , { basePath : '/app' } ) ) . toBe ( 'http://example.com/app/api/docs' )
83+ } )
84+
85+ it ( 'combines basePath with provided baseUrl' , ( ) => {
86+ expect (
87+ makeUrlAbsolute ( 'api/docs' , {
88+ baseUrl : 'https://api.example.com' ,
89+ basePath : '/v1' ,
90+ } ) ,
91+ ) . toBe ( 'https://api.example.com/v1/api/docs' )
92+ } )
93+
94+ it ( 'handles basePath without leading slash' , ( ) => {
95+ // Mock window.location.origin
96+ Object . defineProperty ( window , 'location' , {
97+ value : { origin : 'http://example.com' } ,
98+ writable : true ,
99+ } )
100+
101+ expect ( makeUrlAbsolute ( 'api/docs' , { basePath : 'app' } ) ) . toBe ( 'http://example.com/app/api/docs' )
102+ } )
103+
104+ it ( 'handles basePath with trailing slash' , ( ) => {
105+ // Mock window.location.origin
106+ Object . defineProperty ( window , 'location' , {
107+ value : { origin : 'http://example.com' } ,
108+ writable : true ,
109+ } )
110+
111+ expect ( makeUrlAbsolute ( 'api/docs' , { basePath : '/app/' } ) ) . toBe ( 'http://example.com/app/api/docs' )
112+ } )
113+
114+ it ( 'ignores basePath for absolute URLs' , ( ) => {
115+ expect ( makeUrlAbsolute ( 'https://example.com/api' , { basePath : '/app' } ) ) . toBe ( 'https://example.com/api' )
84116 } )
85117 } )
86118} )
0 commit comments