@@ -203,28 +203,86 @@ describe('parseUrl', () => {
203
203
} ) ;
204
204
205
205
describe ( 'parseStringToURLObject' , ( ) => {
206
- it ( 'returns undefined for invalid URLs' , ( ) => {
207
- expect ( parseStringToURLObject ( 'invalid-url' ) ) . toBeUndefined ( ) ;
208
- } ) ;
209
-
210
- it ( 'returns a URL object for valid URLs' , ( ) => {
211
- expect ( parseStringToURLObject ( 'https://somedomain.com' ) ) . toBeInstanceOf ( URL ) ;
212
- } ) ;
213
-
214
- it ( 'returns a URL object for valid URLs with a base URL' , ( ) => {
215
- expect ( parseStringToURLObject ( 'https://somedomain.com' , 'https://base.com' ) ) . toBeInstanceOf ( URL ) ;
216
- } ) ;
217
-
218
- it ( 'returns a relative URL object for relative URLs' , ( ) => {
219
- expect ( parseStringToURLObject ( '/path/to/happiness' ) ) . toEqual ( {
220
- isRelative : true ,
221
- pathname : '/path/to/happiness' ,
222
- search : '' ,
223
- hash : '' ,
224
- } ) ;
206
+ it . each ( [
207
+ [
208
+ 'invalid URL' ,
209
+ 'invalid-url' ,
210
+ {
211
+ isRelative : true ,
212
+ pathname : '/invalid-url' ,
213
+ search : '' ,
214
+ hash : '' ,
215
+ } ,
216
+ ] ,
217
+ [ 'valid absolute URL' , 'https://somedomain.com' , expect . any ( URL ) ] ,
218
+ [ 'valid absolute URL with base' , 'https://somedomain.com' , expect . any ( URL ) , 'https://base.com' ] ,
219
+ [
220
+ 'relative URL' ,
221
+ '/path/to/happiness' ,
222
+ {
223
+ isRelative : true ,
224
+ pathname : '/path/to/happiness' ,
225
+ search : '' ,
226
+ hash : '' ,
227
+ } ,
228
+ ] ,
229
+ [
230
+ 'relative URL with query' ,
231
+ '/path/to/happiness?q=1' ,
232
+ {
233
+ isRelative : true ,
234
+ pathname : '/path/to/happiness' ,
235
+ search : '?q=1' ,
236
+ hash : '' ,
237
+ } ,
238
+ ] ,
239
+ [
240
+ 'relative URL with hash' ,
241
+ '/path/to/happiness#section' ,
242
+ {
243
+ isRelative : true ,
244
+ pathname : '/path/to/happiness' ,
245
+ search : '' ,
246
+ hash : '#section' ,
247
+ } ,
248
+ ] ,
249
+ [
250
+ 'relative URL with query and hash' ,
251
+ '/path/to/happiness?q=1#section' ,
252
+ {
253
+ isRelative : true ,
254
+ pathname : '/path/to/happiness' ,
255
+ search : '?q=1' ,
256
+ hash : '#section' ,
257
+ } ,
258
+ ] ,
259
+ [ 'URL with port' , 'https://somedomain.com:8080/path' , expect . any ( URL ) ] ,
260
+ [ 'URL with auth' , 'https://user:[email protected] ' , expect . any ( URL ) ] ,
261
+ [ 'URL with special chars' , 'https://somedomain.com/path/with spaces/and/special@chars' , expect . any ( URL ) ] ,
262
+ [ 'URL with unicode' , 'https://somedomain.com/path/with/unicode/测试' , expect . any ( URL ) ] ,
263
+ [ 'URL with multiple query params' , 'https://somedomain.com/path?q1=1&q2=2&q3=3' , expect . any ( URL ) ] ,
264
+ [ 'URL with encoded chars' , 'https://somedomain.com/path/%20%2F%3F%23' , expect . any ( URL ) ] ,
265
+ [ 'URL with IPv4' , 'https://192.168.1.1/path' , expect . any ( URL ) ] ,
266
+ [ 'URL with IPv6' , 'https://[2001:db8::1]/path' , expect . any ( URL ) ] ,
267
+ [ 'URL with subdomain' , 'https://sub.somedomain.com/path' , expect . any ( URL ) ] ,
268
+ [ 'URL with multiple subdomains' , 'https://sub1.sub2.somedomain.com/path' , expect . any ( URL ) ] ,
269
+ [ 'URL with trailing slash' , 'https://somedomain.com/path/' , expect . any ( URL ) ] ,
270
+ [ 'URL with empty path' , 'https://somedomain.com' , expect . any ( URL ) ] ,
271
+ [ 'URL with root path' , 'https://somedomain.com/' , expect . any ( URL ) ] ,
272
+ [ 'URL with file extension' , 'https://somedomain.com/path/file.html' , expect . any ( URL ) ] ,
273
+ [ 'URL with custom protocol' , 'custom://somedomain.com/path' , expect . any ( URL ) ] ,
274
+ [ 'URL with query containing special chars' , 'https://somedomain.com/path?q=hello+world&x=1/2' , expect . any ( URL ) ] ,
275
+ [ 'URL with hash containing special chars' , 'https://somedomain.com/path#section/1/2' , expect . any ( URL ) ] ,
276
+ [
277
+ 'URL with all components' ,
278
+ 'https://user:[email protected] :8080/path/file.html?q=1#section' ,
279
+ expect . any ( URL ) ,
280
+ ] ,
281
+ ] ) ( 'handles %s' , ( _ , url : string , expected : any , base ?: string ) => {
282
+ expect ( parseStringToURLObject ( url , base ) ) . toEqual ( expected ) ;
225
283
} ) ;
226
284
227
- it ( 'does not throw an error if URl .canParse is not defined' , ( ) => {
285
+ it ( 'does not throw an error if URL .canParse is not defined' , ( ) => {
228
286
const canParse = ( URL as any ) . canParse ;
229
287
delete ( URL as any ) . canParse ;
230
288
expect ( parseStringToURLObject ( 'https://somedomain.com' ) ) . toBeInstanceOf ( URL ) ;
0 commit comments