Skip to content

Commit b3fde1d

Browse files
committed
feat(experimental): use current location on string location
1 parent fbe0ac2 commit b3fde1d

File tree

3 files changed

+94
-16
lines changed

3 files changed

+94
-16
lines changed

packages/router/__tests__/router.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,10 @@ describe('Router', () => {
142142
await router.push('/search')
143143
vi.spyOn(history, 'replace')
144144
vi.spyOn(history, 'push')
145+
router.beforeEach(to => {
146+
if (to.fullPath !== '/') return '/'
147+
return
148+
})
145149
await router.replace('/home-before')
146150
expect(history.push).toHaveBeenCalledTimes(0)
147151
expect(history.replace).toHaveBeenCalledTimes(1)

packages/router/src/experimental/router.spec.ts

Lines changed: 86 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import {
4444
createWebHistory,
4545
createWebHashHistory,
4646
RouteLocationRaw,
47+
loadRouteLocation,
4748
} from '../index'
4849
import { NavigationFailureType } from '../errors'
4950
import { createDom, components, tick } from '../../__tests__/utils'
@@ -226,9 +227,41 @@ describe('Experimental Router', () => {
226227
)
227228
})
228229

229-
it.skip('replaces if a guard redirects', async () => {})
230+
it('replaces if a guard redirects', async () => {
231+
const history = createMemoryHistory()
232+
const { router } = await newRouter({ history })
233+
// move somewhere else
234+
await router.push('/search')
235+
vi.spyOn(history, 'replace')
236+
vi.spyOn(history, 'push')
237+
router.beforeEach(to => {
238+
if (to.fullPath !== '/') return '/'
239+
return
240+
})
241+
await router.replace('/home-before')
242+
expect(history.push).toHaveBeenCalledTimes(0)
243+
expect(history.replace).toHaveBeenCalledTimes(1)
244+
expect(history.replace).toHaveBeenCalledWith('/', expect.anything())
245+
})
230246

231-
it.skip('replaces if a guard redirect replaces', async () => {})
247+
it('replaces if a guard redirect replaces', async () => {
248+
const history = createMemoryHistory()
249+
const { router } = await newRouter({ history })
250+
// move somewhere else
251+
router.beforeEach(to => {
252+
if (to.name !== 'Foo') {
253+
return { name: 'Foo', replace: true }
254+
}
255+
return // no warn
256+
})
257+
vi.spyOn(history, 'replace')
258+
vi.spyOn(history, 'push')
259+
await router.push('/search')
260+
expect(history.location).toBe('/foo')
261+
expect(history.push).toHaveBeenCalledTimes(0)
262+
expect(history.replace).toHaveBeenCalledTimes(1)
263+
expect(history.replace).toHaveBeenCalledWith('/foo', expect.anything())
264+
})
232265

233266
it.skip('allows to customize parseQuery', async () => {})
234267

@@ -314,7 +347,13 @@ describe('Experimental Router', () => {
314347
})
315348
})
316349

317-
it.skip('can pass replace option to push', async () => {})
350+
it('can pass replace option to push', async () => {
351+
const { router, history } = await newRouter()
352+
vi.spyOn(history, 'replace')
353+
await router.push({ path: '/foo', replace: true })
354+
expect(history.replace).toHaveBeenCalledTimes(1)
355+
expect(history.replace).toHaveBeenCalledWith('/foo', expect.anything())
356+
})
318357

319358
it('can replaces current location with a string location', async () => {
320359
const { router, history } = await newRouter()
@@ -348,15 +387,26 @@ describe('Experimental Router', () => {
348387
expect('No match found').toHaveBeenWarnedTimes(2)
349388
})
350389

351-
it.skip('casts number params to string', async () => {})
352-
353-
it.skip('removes null/undefined params', async () => {})
354-
355-
it.skip('handles undefined path', async () => {})
356-
357-
it.skip('warns on undefined location during dev', async () => {})
390+
it('casts number params to string', async () => {
391+
const { router } = await newRouter()
392+
await router.push({ name: 'Param', params: { p: 0 } })
393+
expect(router.currentRoute.value).toMatchObject({ params: { p: '0' } })
394+
})
358395

359-
it.skip('warns on null location during dev', async () => {})
396+
it('handles undefined path in relative navigations', async () => {
397+
const { router } = await newRouter()
398+
await router.push({ name: 'Param', params: { p: 'a' } })
399+
400+
const route1 = router.resolve(
401+
{
402+
path: undefined,
403+
params: { p: 'b' },
404+
},
405+
router.currentRoute.value
406+
)
407+
expect(route1.params).toEqual({ p: 'b' })
408+
expect(route1.path).toBe('/p/b')
409+
})
360410

361411
it('can pass an optional param', async () => {
362412
const { router } = await newRouter()
@@ -529,11 +579,32 @@ describe('Experimental Router', () => {
529579
})
530580
})
531581

532-
it.skip('can pass a currentLocation to resolve', async () => {})
582+
it('can pass a currentLocation to resolve', async () => {
583+
const { router } = await newRouter()
584+
expect(
585+
router.resolve(
586+
{ params: { p: 1 } },
587+
await loadRouteLocation(
588+
router.resolve({ name: 'Param', params: { p: 2 } })
589+
)
590+
)
591+
).toMatchObject({
592+
name: 'Param',
593+
params: { p: '1' },
594+
})
595+
})
533596

534-
it.skip('resolves relative locations', async () => {})
597+
it('resolves relative string locations', async () => {
598+
const { router } = await newRouter()
599+
await router.push('/users/posva')
600+
await router.push('add')
601+
expect(router.currentRoute.value.path).toBe('/users/add')
602+
await router.push('/users/posva')
603+
await router.push('./add')
604+
expect(router.currentRoute.value.path).toBe('/users/add')
605+
})
535606

536-
it('resolves parent relative locations', async () => {
607+
it('resolves parent relative string locations', async () => {
537608
const { router } = await newRouter()
538609
await router.push('/users/posva')
539610
await router.push('../add')
@@ -577,7 +648,7 @@ describe('Experimental Router', () => {
577648
const history = createMemoryHistory()
578649
const resolver = createFixedResolver(experimentalRoutes)
579650
const router = experimental_createRouter({ history, resolver })
580-
router.beforeEach(async (to, from) => {
651+
router.beforeEach(async to => {
581652
if (to.name !== 'Param') return
582653
// the first navigation gets passed target
583654
if (to.params.p === 'a') {

packages/router/src/experimental/router.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,10 @@ export function experimental_createRouter(
659659
// @ts-expect-error FIXME: incompatible types
660660
to,
661661
// FIXME: incompatible `matched` requires casting
662-
currentLocation
662+
currentLocation ??
663+
// relative string locations are always valid
664+
// so this is more of a convenience default
665+
(typeof to === 'string' ? currentRoute.value : undefined)
663666
)
664667
const href = routerHistory.createHref(matchedRoute.fullPath)
665668

0 commit comments

Comments
 (0)