import { describe, expect, it } from 'vitest'
import { createFixedResolver } from './resolver-fixed'
-import { NO_MATCH_LOCATION } from './resolver-abstract'
+import { MatcherLocationRaw, NO_MATCH_LOCATION } from './resolver-abstract'
import {
EmptyParams,
MatcherPatternHash,
})
})
+ it('keeps extra properties like state and replace from target location', () => {
+ const resolver = createFixedResolver([
+ { name: 'any-path', path: ANY_PATH_PATTERN_MATCHER },
+ ])
+
+ const currentLocation = resolver.resolve({ path: '/bar' })
+
+ // extra parameters that should be preserved
+ const extra = { state: { a: 1 }, replace: true }
+
+ for (const path of ['foo', './foo', '../foo']) {
+ expect(
+ resolver.resolve(
+ {
+ // done this way because TS accepts this kind of combination
+ path,
+ ...extra,
+ },
+ currentLocation
+ )
+ ).toMatchObject({
+ params: {},
+ path: '/foo',
+ query: {},
+ hash: {},
+ state: { a: 1 },
+ replace: true,
+ })
+ }
+ })
+
it('resolves relative object locations', () => {
const resolver = createFixedResolver([
{ name: 'any-path', path: ANY_PATH_PATTERN_MATCHER },
params: { pathMatch: '/?a=a&b=b#h' },
})
})
+
+ it('keeps extra properties like state and replace from target location', () => {
+ const resolver = createFixedResolver([
+ { name: 'any-path', path: ANY_PATH_PATTERN_MATCHER },
+ ])
+
+ // extra parameters that should be preserved
+ const extra = { state: { a: 1 }, replace: true }
+
+ expect(
+ resolver.resolve({
+ // done this way because TS accepts this kind of combination
+ path: '/foo',
+ ...extra,
+ })
+ ).toMatchObject({
+ params: {},
+ path: '/foo',
+ query: {},
+ hash: {},
+ state: { a: 1 },
+ replace: true,
+ })
+ })
})
describe('named locations', () => {
).toThrowError('Record "nonexistent" not found')
})
+ it('keeps extra properties like state and replace from target location', () => {
+ const resolver = createFixedResolver([
+ { name: 'home', path: EMPTY_PATH_PATTERN_MATCHER },
+ ])
+
+ // extra parameters that should be preserved
+ const extra = { state: { a: 1 }, replace: true }
+
+ expect(
+ resolver.resolve({
+ // done this way because TS accepts this kind of combination
+ name: 'home',
+ params: {},
+ ...extra,
+ })
+ ).toMatchObject({
+ name: 'home',
+ params: {},
+ path: '/',
+ query: {},
+ hash: {},
+ state: { a: 1 },
+ replace: true,
+ })
+ })
+
it('resolves named locations with explicit query', () => {
const resolver = createFixedResolver([
{
// @ts-expect-error: to is never
const path = to.path ?? '/'
return {
+ // type is never
+ ...(to as any),
...NO_MATCH_LOCATION,
fullPath: NEW_stringifyURL(stringifyQuery, path, query, hash),
path,
)
const url: LocationNormalized = {
+ // preserve other fields like `state` and `replace`
+ ...to,
fullPath: NEW_stringifyURL(
stringifyQuery,
path,
const query = normalizeQuery(to.query)
const path = resolveRelativePath(to.path, currentLocation?.path || '/')
url = {
+ // preserve other fields like `state` and `replace`
+ ...to,
fullPath: NEW_stringifyURL(stringifyQuery, path, query, to.hash),
path,
query,