From 0ccbc1e9af07a30a149ab14c007f63cbc35a8126 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Fri, 28 Feb 2020 16:32:36 +0100 Subject: [PATCH] fix(link): non active repeatable params --- __tests__/RouterLink.spec.ts | 46 ++++++++++++++++++++++++++++++ __tests__/router.spec.ts | 46 ++++++++++++++++++++++++++++++ playground/App.vue | 9 ++++++ src/components/Link.ts | 54 +++++++++++++++--------------------- src/router.ts | 8 ++++-- src/utils/index.ts | 13 ++++++++- src/utils/query.ts | 4 ++- 7 files changed, 144 insertions(+), 36 deletions(-) diff --git a/__tests__/RouterLink.spec.ts b/__tests__/RouterLink.spec.ts index 52d88ac3..24c5f230 100644 --- a/__tests__/RouterLink.spec.ts +++ b/__tests__/RouterLink.spec.ts @@ -71,6 +71,34 @@ const locations: Record< name: undefined, }, }, + repeatedParams2: { + string: '/p/1/2', + normalized: { + fullPath: '/p/1/2', + path: '/p/1/2', + params: { p: ['1', '2'] }, + meta: {}, + query: {}, + hash: '', + matched: [records.home], + redirectedFrom: undefined, + name: undefined, + }, + }, + repeatedParams3: { + string: '/p/1/2/3', + normalized: { + fullPath: '/p/1/2/3', + path: '/p/1/2/3', + params: { p: ['1', '2', '3'] }, + meta: {}, + query: {}, + hash: '', + matched: [records.home], + redirectedFrom: undefined, + name: undefined, + }, + }, } describe('RouterLink', () => { @@ -143,6 +171,24 @@ describe('RouterLink', () => { expect(el.querySelector('a')!.className).toContain('router-link-active') }) + it('is not active with more repeated params', () => { + const { el } = factory( + locations.repeatedParams2.normalized, + { to: locations.repeatedParams3.string }, + locations.repeatedParams3.normalized + ) + expect(el.querySelector('a')!.className).toBe('') + }) + + it('is not active with partial repeated params', () => { + const { el } = factory( + locations.repeatedParams3.normalized, + { to: locations.repeatedParams2.string }, + locations.repeatedParams2.normalized + ) + expect(el.querySelector('a')!.className).toBe('') + }) + it.todo('can be active as an alias') it.todo('can be exact-active as an alias') it.todo('is active when a child is active') diff --git a/__tests__/router.spec.ts b/__tests__/router.spec.ts index 06424f1f..1f03a15a 100644 --- a/__tests__/router.spec.ts +++ b/__tests__/router.spec.ts @@ -382,6 +382,52 @@ describe('Router', () => { }) }) + it('can reroute to a replaced route with the same component', async () => { + const { router } = await newRouter() + router.addRoute({ + path: '/new/foo', + component: components.Foo, + name: 'new', + }) + // navigate to the route we just added + await router.replace({ name: 'new' }) + // replace it + router.addRoute({ + path: '/new/bar', + component: components.Foo, + name: 'new', + }) + // navigate again + await router.replace({ name: 'new' }) + expect(router.currentRoute.value).toMatchObject({ + path: '/new/bar', + name: 'new', + }) + }) + + it('can reroute to child', async () => { + const { router } = await newRouter() + router.addRoute({ + path: '/new', + component: components.Foo, + children: [], + name: 'new', + }) + // navigate to the route we just added + await router.replace('/new/child') + // replace it + router.addRoute('new', { + path: 'child', + component: components.Bar, + name: 'new-child', + }) + // navigate again + await router.replace('/new/child') + expect(router.currentRoute.value).toMatchObject({ + name: 'new-child', + }) + }) + it('can reroute when adding a new route', async () => { const { router } = await newRouter() await router.push('/p/p') diff --git a/playground/App.vue b/playground/App.vue index a09d5990..fe0866d1 100644 --- a/playground/App.vue +++ b/playground/App.vue @@ -106,6 +106,15 @@ >/docs/é +
  • + /rep +
  • +
  • + /rep/a +
  • +
  • + /rep/a/b +