11import { experimental_parseQuery as parseQuery } from './query'
2- import { experimental_parseURL as originalParseURL } from './location'
2+ import {
3+ experimental_parseURL as originalParseURL ,
4+ experimental_stringifyURL as stringifyURL ,
5+ } from './location'
6+ import { stringifyQuery } from '../query'
37import { vi , describe , expect , it } from 'vitest'
48
59describe ( 'parseURL' , ( ) => {
@@ -77,6 +81,15 @@ describe('parseURL', () => {
7781 } )
7882 } )
7983
84+ it ( 'works with relative query and hash and current location' , ( ) => {
85+ expect ( parseURL ( '?f=foo#hash' , '/parent/bar' ) ) . toEqual ( {
86+ fullPath : '/parent/bar?f=foo#hash' ,
87+ path : '/parent/bar' ,
88+ hash : '#hash' ,
89+ query : { f : [ 'foo' ] } ,
90+ } )
91+ } )
92+
8093 it ( 'works with relative hash and current location' , ( ) => {
8194 expect ( parseURL ( '#hash' , '/parent/bar' ) ) . toEqual ( {
8295 fullPath : '/parent/bar#hash' ,
@@ -242,6 +255,17 @@ describe('parseURL', () => {
242255 expect ( parseQuery ) . toHaveBeenCalledWith ( 'é=é&é=a' )
243256 } )
244257
258+ it ( 'uses the parseQuery result as query' , ( ) => {
259+ const query = { custom : [ 'value' ] }
260+ expect ( originalParseURL ( ( ) => query , '/foo?x' ) . query ) . toBe ( query )
261+ } )
262+
263+ it ( 'does not call parseQuery for a ? in the hash' , ( ) => {
264+ const parseQuery = vi . fn ( )
265+ expect ( originalParseURL ( parseQuery , '/foo#a?b' ) . query ) . toEqual ( { } )
266+ expect ( parseQuery ) . not . toHaveBeenCalled ( )
267+ } )
268+
245269 describe ( 'hash' , ( ) => {
246270 it ( 'keeps the hash as it appears in the URL, like location.hash' , ( ) => {
247271 expect ( parseURL ( '/foo#%22%20%25' ) ) . toMatchObject ( {
@@ -268,3 +292,22 @@ describe('parseURL', () => {
268292 } )
269293 } )
270294} )
295+
296+ describe ( 'stringifyURL' , ( ) => {
297+ it ( 'joins path, query and hash' , ( ) => {
298+ expect ( stringifyURL ( stringifyQuery , '/foo' , { a : [ '1' , '2' ] } , '#h' ) ) . toBe (
299+ '/foo?a=1&a=2#h'
300+ )
301+ } )
302+
303+ it ( 'omits the ? for an empty query' , ( ) => {
304+ expect ( stringifyURL ( stringifyQuery , '/foo' , { } ) ) . toBe ( '/foo' )
305+ expect ( stringifyURL ( stringifyQuery , '/foo' ) ) . toBe ( '/foo' )
306+ } )
307+
308+ it ( 'appends the hash as is' , ( ) => {
309+ expect ( stringifyURL ( stringifyQuery , '/foo' , { } , '#%20 a' ) ) . toBe (
310+ '/foo#%20 a'
311+ )
312+ } )
313+ } )
0 commit comments